我有一个 Days 列,其中有一串 0 和 1 代表一周中的有效天数,为了清楚起见,我需要将其格式化或翻译成一串字母(取决于语言),例如“MTWtFSs”(英语)或“LMmJVSD” (西班牙语)。
此代码有效,但似乎太长了。还有其他想法吗?
DECLARE @i INT = 0
,@DaysBits VARCHAR(7) = '110010'
,@DaysLetters VARCHAR(7) = ''
,@DaysMask VARCHAR(7) = 'MTWtFSs'
WHILE @i < 7
BEGIN
SET @i = @i + 1
IF ( SUBSTRING(@DaysBits, @i, 1) = 1 )
SET @DaysLetters = @DaysLetters + SUBSTRING(@DaysMask, @i, 1)
ELSE
SET @DaysLetters = @DaysLetters + '_'
END
SELECT @DaysBits AS 'Days Bits'
,@DaysLetters AS 'Days Letters'