happyhippy

这个世界的问题在于聪明人充满疑惑,而傻子们坚信不疑。--罗素
posts - 137,comments - 596,trackbacks - 45

    T-SQL中提供了不少操作DateTime的函数,可惜竟然没有根据年月日创建DateTime类型的函数,例如DateTime(yyyy, MM, dd)这样的“构造函数”,杯具啊~~

 

   1: DECLARE @Year int, @Month int, @Day int;
   2: SELECT @Year=2011, @Month=5, @Day=8;
   3:  
   4: --法1:
   5: SELECT CAST(CAST(@Year AS varchar(4)) + '-' + CAST(@Month AS nvarchar(2)) + '-' + CAST(@Day AS nvarchar(2)) AS DateTime)
   6:  
   7: --法2:
   8: SELECT DateAdd(dd, @Day-1, DateAdd(mm, @Month -1, DateAdd(yy, @Year - 1900, '19000101')))
   9:  
  10: --法3:
  11: SELECT DateAdd(mm, (@Year - 1900) * 12 + @Month - 1 , @Day - 1)
posted on 2011-05-28 10:36 Silent Void 阅读(...) 评论(...) 编辑 收藏