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)
标签: ,