• savycara (7/24/2013)


    Actually 2011 is year and 4 is month which shoud be represented as 04 and date is 01 (which is to be added)

    I need this column data to be in style 101

    Hey,

    Try this....

    declare @var varchar(6) = '20112'

    DECLARE @var1 varchar(10)

    declare @month varchar(2)

    IF LEN(@var) > 5

    BEGIN

    SET @month = RIGHT(@var,2)

    SET @var1 = (select LEFT(@var,4) + REPLACE(SUBSTRING(@month,1,2),' ','0')+'01')

    select CONVERT(DATETIME,@var1,101)

    END

    ELSE

    BEGIN

    SET @month = RIGHT(@var,1)

    SET @var1 = (select LEFT(@var,4) + REPLACE(str(@month,2),' ','0')+'01')

    select CONVERT(DATETIME,@var1,101)

    END

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/