获取当月第一天
select CONVERT(nvarchar(50),DateAdd(dd,-datePart(day,GetDate())+1,GETDATE()),23)
2022-08-01
SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
2022-08-01 00:00:00.000
获取当月最后一天
select DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)
2022-09-01 00:00:00.000
select DATEDIFF(m,0,getdate())+1
1472
SELECT dateadd(ms,0,DATEADD(dd, DATEDIFF(dd,0,getdate()), 0))
2022-08-04 00:00:00.000
SELECT dateadd(ms,-3,DATEADD(dd, DATEDIFF(dd,-1,getdate()), 0))
2022-08-04 23:59:59.997
SELECT EOMONTH(Date)
截取日期年份和月份(datepart函数返回的是整型数值)
截取年份:
datepart(yy,'2017-1-1')
2017
截取月份:
datepart(mm,'2017-1-1')
1
截取小时:
SELECT DATEPART(hh,'2013-08-08 13:12:11')
3
截取分钟:
SELECT DATEPART(mi,'2013-08-08 13:12:11')
12
截取秒:
SELECT DATEPART(ss,'2013-08-08 13:12:11')
11
返回字符型,那么使用datename函数
Select Datename(year,GetDate())
Datename(month,GetDate())
Datename(day,GetDate())
Datename(hour,GetDate())
Datename(minute,GetDate())
Datename(second,GetDate())
Datename(weekDay,GetDate())
Datename(week,GetDate())
使用Convert函数
select convert(char(10),GetDate(),120) as Date
100 mm dd yyyy
101 mm/dd/yyyy
102 yyyy.mm.dd
103 dd/mm/yyyy
106 dd mm yyyy
108 hh:mi:ss(时间)
111 yyyy/mm/dd
112 yyyymmdd
120 yyyy-mm-dd
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/288341.html