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

  • 問題

  • 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
    2008年4月21日 上午 08:56

解答

  • 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
    2008年4月21日 上午 10:37

所有回覆

  • 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
    2008年4月21日 上午 10:37
  • Hi Colt

    Thanks for your information.

    Many thanks
    2008年4月21日 上午 11:37