none
Convert INT to Date of Week,Month? RRS feed

  • Pergunta

  • Hi

    I am wondering if there any TSQL syntax that I can use to convert Integer to DayOfWeek and Month
    E.g.
    If I pass t integer '1', it could convert the it to "SUN"/"Sunday"
    If I pass an integer '2', It could convert it to "Mon"/"Monday"..etc

    and If I pass an integer, it would convert it to Jan/January ...etc?

    Or I have to create my own function to deal with it?

    Many thanks

    Chi
    segunda-feira, 21 de abril de 2008 08:56

Respostas

  • Hi Chi,

    I can't think of any buildin TSQL function can do this kind of conversion, but I think the logic to create such function is:

    SELECT CAST(CAST('4/21/2008' AS DATETIME) AS INT) % 7 ( = 0 = Mon)

    SELECT CAST(CAST('4/22/2008' AS DATETIME) AS INT) % 7 ( = 1 = Tue)

    SELECT CAST(CAST('4/23/2008' AS DATETIME) AS INT) % 7 ( = 2 = Wed)

    SELECT CAST(CAST('4/24/2008' AS DATETIME) AS INT) % 7 ( = 3 = Thurs)

    Similar technique for month conversion, by changing mod 7 to mod 12..

    Regards,
    Colt
    segunda-feira, 21 de abril de 2008 10:37

Todas as Respostas

  • Hi Chi,

    I can't think of any buildin TSQL function can do this kind of conversion, but I think the logic to create such function is:

    SELECT CAST(CAST('4/21/2008' AS DATETIME) AS INT) % 7 ( = 0 = Mon)

    SELECT CAST(CAST('4/22/2008' AS DATETIME) AS INT) % 7 ( = 1 = Tue)

    SELECT CAST(CAST('4/23/2008' AS DATETIME) AS INT) % 7 ( = 2 = Wed)

    SELECT CAST(CAST('4/24/2008' AS DATETIME) AS INT) % 7 ( = 3 = Thurs)

    Similar technique for month conversion, by changing mod 7 to mod 12..

    Regards,
    Colt
    segunda-feira, 21 de abril de 2008 10:37
  • Hi Colt

    Thanks for your information.

    Many thanks
    segunda-feira, 21 de abril de 2008 11:37