T-SQL 根据年月日创建DateTime

    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)

转载于:https://www.cnblogs.com/happyhippy/archive/2011/05/28/2060553.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页