输入为二进制字符串格式时如何转换为特定值

时间:2014-11-11 09:12:37

标签: sql-server sql-server-2008 sql-server-2005 sql-server-2008-r2 sql-server-2012

我有一种情况,我会得到一个字符串,如天varchar(7),天=' 1001101' 我想生成像这样的输出 1:周日 0:无 0:无 1:wensday 1:周四 0:abset 1:星期六

最终产出:星期六星期六星期日星期日 如何通过使用T-SQL函数实现它。 (带输入输出参数的功能)

3 个答案:

答案 0 :(得分:2)

使用CHOOSE function获取结果。试试这个..

注意:这仅适用于SQL SERVER 2012+ ..如果您想在早期版本中使用它,请使用Case statements代替Choose。仅仅是我使用CHOOSE function

的代码
CREATE FUNCTION dbo.Func (@dayss VARCHAR(1000))
returns VARCHAR(1000)
  BEGIN
      DECLARE @cnt    INT =1,
              @outpt  VARCHAR(100),
              @fina   VARCHAR(100)='',
              @outpt1 VARCHAR(100)=''

      WHILE @cnt <= Len(@dayss)
        BEGIN
            SET @outpt = Substring(@dayss, @cnt, 1)

            SELECT @outpt1 = CASE
                               WHEN @outpt = 1 THEN @outpt
                                                    + Choose(@cnt, ':sunday ', ':monday ', ':tuesday ', ':wednesday ', ':thursday ', ':friday ', ':saturday ')
                               ELSE @outpt + ':absent '
                             END

            SET @fina += @outpt1
            SET @cnt+=1
        END

      RETURN @fina
  END

SELECT dbo.Func('1001101')

输出: 1:sunday 0:absent 0:absent 1:wednesday 1:thursday 0:absent 1:saturday

获取最终的输出

CREATE FUNCTION dbo.Funcfinal (@dayss VARCHAR(1000))
returns VARCHAR(1000)
  BEGIN
      DECLARE @cnt    INT =1,
              @outpt  VARCHAR(100),
              @fina   VARCHAR(100)='',
              @outpt1 VARCHAR(100)=''

      WHILE @cnt <= Len(@dayss)
        BEGIN
            SET @outpt = Substring(@dayss, @cnt, 1)

            SELECT @outpt1 = CASE
                               WHEN @outpt = 1 THEN Choose(@cnt, 'sunday ', 'monday ', 'tuesday ', 'wednesday ', 'thursday ', 'friday ', 'saturday ')
                               ELSE ''
                             END

            SET @fina += @outpt1
            SET @cnt+=1
        END

      RETURN @fina
  END

SELECT dbo.Funcfinal('1001101') 

输出: sunday wednesday thursday saturday

答案 1 :(得分:0)

使用CASE的传统方式。

WHILE ( @i <= Len(@input) )
  BEGIN
      SET @temp = Substring(@input, @i, 1)
      SET @output += ' '+(SELECT CASE
                               WHEN @temp = 1
                                    AND @i = 1 THEN 'Sunday'
                               WHEN @temp = 1
                                    AND @i = 2 THEN 'Monday'
                               WHEN @temp = 1
                                    AND @i = 3 THEN 'Tuesday'
                               WHEN @temp = 1
                                    AND @i = 4 THEN 'Wednesday'
                               WHEN @temp = 1
                                    AND @i = 5 THEN 'Thursday'
                               WHEN @temp = 1
                                    AND @i = 6 THEN 'Friday'
                               WHEN @temp = 1
                                    AND @i = 7 THEN 'Saturday'
                               ELSE ''
                             END)
      SET @i = @i + 1

  END

PRINT @output 

答案 2 :(得分:0)

create function dateconvert(@input varchar(100))
returns varchar(1000)
begin
declare 
@i int=1,
@temp varchar(1000)='',
@output1 varchar(1000)='',
@output varchar(1000)
while (@i<=len(@input))
    begin
        set @temp = substring(@input, @i, 1)
        set @output= ( select  case
                               when @temp = '1'
                                    and @i = 1 then 'sunday'
                               when @temp = '1'
                                    and @i = 2 then 'monday'
                               when @temp = '1'
                                    and @i = 3 then 'tuesday'
                               when @temp = '1'
                                    and @i = 4 then 'wednesday'
                               when @temp = '1'
                                    and @i = 5 then 'thursday'
                               when @temp = '1'
                                    and @i = 6 then 'friday'
                               when @temp = '1'
                                    and @i = 7 then 'saturday'
                               else '_'
                             end )
            set @output1=@output1+@output
            set @i = @i + 1 
    end
    return @output1
end