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)
标签: DateTime, 构造函数

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

你可能感兴趣的:(T-SQL 根据年月日创建DateTime)