将附加文本转换为日期

时间:2012-10-29 16:42:32

标签: tsql

  convert(datetime,  BIRTH_MM + '/' +  BIRTH_DD + '/' + BIRTH_YY, 103) as Birthdate

因此,连接字符串如下所示:04/05/88

试图让这个工作。

尝试了这个,关闭我现在得到一个约会,但它给了我1900年或1905年不正确

convert(datetime,  BIRTH_MM + BIRTH_DD + BIRTH_YY, 103) as Birthdate

更新

尝试了一些东西,但还没有完成:

 case when(BIRTH_DD > 0 and BIRTH_MM > 0 and BIRTH_YY > 0)
    then 
            convert(datetime, cast(BIRTH_DD as varchar(1)) + '/' + cast(_BIRTH_MM as varchar(2)) + '/' + cast(BIRTH_YY as varchar(4)), 103)
    else
            convert(datetime, '1900-01-01', 103) as Birthdate,
        end

1 个答案:

答案 0 :(得分:2)

如果没有在这些值上看到您的表结构或数据类型,您可以使用与此类似的内容:

declare @BIRTH_MM int = 04
declare @BIRTH_DD int = 05
declare @BIRTH_YY int = 1988

select convert(datetime,
               cast(@BIRTH_MM as varchar(2)) + '/' +  
               cast(@BIRTH_DD as varchar(2)) + '/' + 
               cast(@BIRTH_YY as varchar(4)), 101) as Birthdate

请参阅SQL Fiddle with Demo