https://blog.csdn.net/puddingpp/article/details/78889386
以上是 其他处理方式
--获取yyyy-mm-dd
CONVERT(char(10), GetDate(),126)
--请在下面的代码中选择你需要的。
--注意,将里面的 GETDATE() 代换成你要转化的日期变量。
--最常用的
Select
CONVERT
(
varchar
(100), GETDATE(), 120)
-- 2006-05-16 10:57:49
Select
CONVERT
(
varchar
(100), GETDATE(), 121)
-- 2006-05-16 10:57:49.700
Select
CONVERT
(
varchar
(100), GETDATE(), 108)
-- 10:57:49
Select
CONVERT
(
varchar
(100), GETDATE(), 114)
-- 10:57:49:547
select CONVERT(varchar(7), GETDATE(), 120) ---2019-01
--其它日期格式化式样
--日期格式化函数
Select
CONVERT
(
varchar
(100), GETDATE(), 0)
-- 05 16 2006 10:57AM
Select
CONVERT
(
varchar
(100), GETDATE(), 1)
-- 05/16/06
Select
CONVERT
(
varchar
(100), GETDATE(), 2)
-- 06.05.16
Select
CONVERT
(
varchar
(100), GETDATE(), 3)
-- 16/05/06
Select
CONVERT
(
varchar
(100), GETDATE(), 4)
-- 16.05.06
Select
CONVERT
(
varchar
(100), GETDATE(), 5)
-- 16-05-06
Select
CONVERT
(
varchar
(100), GETDATE(), 6)
-- 16 05 06
Select
CONVERT
(
varchar
(100), GETDATE(), 7)
-- 05 16, 06
Select
CONVERT
(
varchar
(100), GETDATE(), 8)
-- 10:57:46
Select
CONVERT
(
varchar
(100), GETDATE(), 9
-- 05 16 2006 10:57:46:827AM
Select
CONVERT
(
varchar
(100), GETDATE(), 10
-- 05-16-06
Select
CONVERT
(
varchar
(100), GETDATE(), 11
-- 06/05/16
Select
CONVERT
(
varchar
(100), GETDATE(), 12
-- 060516
Select
CONVERT
(
varchar
(100), GETDATE(), 13
-- 16 05 2006 10:57:46:937
Select
CONVERT
(
varchar
(100), GETDATE(), 14
-- 10:57:46:967
Select
CONVERT
(
varchar
(100), GETDATE(), 20
-- 2006-05-16 10:57:47
Select
CONVERT
(
varchar
(100), GETDATE(), 21
-- 2006-05-16 10:57:47.157
Select
CONVERT
(
varchar
(100), GETDATE(), 22
-- 05/16/06 10:57:47 AM
Select
CONVERT
(
varchar
(100), GETDATE(), 23
-- 2006-05-16
Select
CONVERT
(
varchar
(100), GETDATE(), 24
-- 10:57:47
Select
CONVERT
(
varchar
(100), GETDATE(), 25
-- 2006-05-16 10:57:47.250
Select
CONVERT
(
varchar
(100), GETDATE(), 100
-- 05 16 2006 10:57AM
Select
CONVERT
(
varchar
(100), GETDATE(), 101
-- 05/16/2006
Select
CONVERT
(
varchar
(100), GETDATE(), 102
-- 2006.05.16
Select
CONVERT
(
varchar
(100), GETDATE(), 103
-- 16/05/2006
Select
CONVERT
(
varchar
(100), GETDATE(), 104
-- 16.05.2006
Select
CONVERT
(
varchar
(100), GETDATE(), 105
-- 16-05-2006
Select
CONVERT
(
varchar
(100), GETDATE(), 106
-- 16 05 2006
Select
CONVERT
(
varchar
(100), GETDATE(), 107
-- 05 16, 2006
Select
CONVERT
(
varchar
(100), GETDATE(), 108
-- 10:57:49
Select
CONVERT
(
varchar
(100), GETDATE(), 109
-- 05 16 2006 10:57:49:437AM
Select
CONVERT
(
varchar
(100), GETDATE(), 110
-- 05-16-2006
Select
CONVERT
(
varchar
(100), GETDATE(), 111
-- 2006/05/16
Select
CONVERT
(
varchar
(100), GETDATE(), 112
-- 20060516
Select
CONVERT
(
varchar
(100), GETDATE(), 113
-- 16 05 2006 10:57:49:513
Select
CONVERT
(
varchar
(100), GETDATE(), 114
-- 10:57:49:547
Select
CONVERT
(
varchar
(100), GETDATE(), 120
-- 2006-05-16 10:57:49
Select
CONVERT
(
varchar
(100), GETDATE(), 121
-- 2006-05-16 10:57:49.700
Select
CONVERT
(
varchar
(100), GETDATE(), 126
-- 2006-05-16T10:57:49.827
Select
CONVERT
(
varchar
(100), GETDATE(), 130
-- 18 ???? ?????? 1427 10:57:49:907AM
Select
CONVERT
(
varchar
(100), GETDATE(), 131
-- 18/04/1427 10:57:49:920AM
通过系统函数操作时间
---求相差天数 select datediff(day,'2004-01-01',getdate()) --1.一个月第一天的 SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --2.本周的星期一 SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) select dateadd(wk,datediff(wk,0,getdate()),6) --3.一年的第一天 SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) --4.季度的第一天 SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0) --5.当天的半夜 SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 0) --6.上个月的最后一天 SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)) --7.去年的最后一天 SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) --8.本月的最后一天 SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) --9.本年的最后一天 SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0)) --10.本月的第一个星期一 select DATEADD(wk, DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0) --查询本周注册人数 select count(*) from [user] where datediff(week,create_day-1,getdate())=0 --上周注册人数 select count(*) from [user] where datediff(week,create_day-1,getdate())=1 --本月注册人数 select count(*) from [user] where datediff(month,create_day,getdate())=0 --上月注册人数 select count(*) from [user] where datediff(month,create_day,getdate())=1 --如果要效率,这样写查询 --查询本周注册人数 select count(*) from [user] where create_day>=dateadd(day,2-datepart(weekday,getdate()),convert(varchar,getdate(),112)) and create_day<dateadd(day,9-datepart(weekday,getdate()),convert(varchar,getdate(),112)) --上周注册人数 select count(*) from [user] where create_day>=dateadd(day,-5-datepart(weekday,getdate()),convert(varchar,getdate(),112)) and create_day<dateadd(day,2-datepart(weekday,getdate()),convert(varchar,getdate(),112)) --本月注册人数 select count(*) from [user] where create_day>=dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)) and create_day<dateadd(month,1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))) --上月注册人数 select count(*) from [user] where create_day>=dateadd(month,-1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))) and create_day<dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)) --本周 select count(*) from User where datediff(dd,create_day,getdate()) <= datepart(dw,getdate()) --上周 select count(*) from User where datediff(dd,create_day,(getdate() - datepart(dw,getdate()))) <= 7 --本月 select count(*) from User where datepart(mm,create_day) = datepart(mm,getdate()) --上月 select count(*) from User where datepart(mm,create_day) = datepart(mm,getdate()) - 1 --本周 select count(*) from [User] where datediff(dd,create_day,getdate()) <= datepart(dw,getdate()) --上周 select count(*) from [User] where datediff(dd,create_day,(getdate() - datepart(dw,getdate()))) <= 7 --本月 select count(*) from [User] where datepart(mm,create_day) = datepart(mm,getdate()) --上月 select count(*) from [User] where datepart(mm,create_day) = datepart(mm,getdate()) - 1 学习 month(create_day)=month(getdate())本月 month(create_day)=month(getdate())-1 上月 补充 查询今日所有的 SELECT * from feedback WHERE (DATEDIFF(d,fedtime,GETDATE())=0) ORDER BY fedid DESC
获取当前月份下一个月的年月日
SELECT CONVERT(varchar(7),DateAdd(m,+1, GETDATE()), 120)