一. 类型转换函数
select CAST(‘121231‘ AS DATE),CAST(100 AS CHAR(3)),CONVERT(TIME,‘2012-05-01 12:11:10‘)
二. 日期函数
--1. 获取系统当前日期的函数
select GETDATE() as CurrentTime,GETUTCDATE() as UTCTIme
--2. 返回指定日期的d是一个月中的第几天、月份、年数
select DAY(‘2020-08-05 12:11:08‘)
select MONTH(‘2020-08-05 12:11:08‘)
select YEAR(‘2020-08-05 12:11:08‘)
--3. 返回指定日期的 年、月、第n天、天、第n周、星期几、小时、分钟、秒
SELECT DATENAME(year,‘2020-04-03 08:12:36‘) AS yearValue,
DATENAME(month,‘2020-04-03 08:12:36‘) AS monthValue,
DATENAME(dayofyear,‘2020-04-03 08:12:36‘) AS dayofyearValue, --一年当中的第几天
DATENAME(day,‘2020-04-03 08:12:36‘) AS dayValue,
DATENAME(week,‘2020-04-03 08:12:36‘) AS weekValue,
DATENAME(weekday,‘2020-04-03 08:12:36‘) AS weekdayValue,
DATENAME(hour,‘2020-04-03 08:12:36‘) AS hourValue,
DATENAME(minute,‘2020-04-03 08:12:36‘) AS minuteValue,
DATENAME(second,‘2020-04-03 08:12:36‘) AS secondValue
--4. 获取日期中指定部分的整数值的函数
SELECT DATEPART(year,‘2020-04-03 08:12:36‘) AS yearValue,
DATEPART(month,‘2020-04-03 08:12:36‘) AS monthValue,
DATEPART(dayofyear,‘2020-04-03 08:12:36‘) AS dayofyearValue;
--5. 日期的加运算
SELECT DATEADD(year,1,‘2020-04-03 08:12:36‘) AS yearAdd,
DATEADD(month ,2, ‘2020-04-03 08:12:36‘) AS weekdayAdd,
DATEADD(hour,3,‘2020-04-03 08:12:36‘) AS hourAdd;
!