大家好,又见面了,我是你们的朋友全栈君。
–以2013-12-10 12:56:55为例
–print convert(nvarchar(10),getdate(),120) => 2013-12-10
–print convert(nvarchar(20),getdate(),23) => 2013-12-10
–print convert(nvarchar(20),getdate(),20) => 2018-07-18 09:09:09
–print convert(varchar(100), getdate(), 112) => 20200809
–print convert(nvarchar(16),getdate(),120) => 2019-04-26 11:56
–DATEPART(month,CreateDate) => 12
–DATEPART(year,CreateDate) => 2013
--2020-09-29 08:58:18
select convert(varchar(100), getdate(), 20)
--20200929085818
select replace(replace(replace(convert(varchar(100), getdate(), 20), '-', ''), ' ', ''), ':', '')
--202009290858
select left(replace(replace(replace(convert(varchar(100), getdate(), 20), '-', ''), ' ', ''), ':', ''), 12)
--2020-09-29 09:06:25.557
select convert(varchar(100), getdate(), 121)
--20200929090625557
select replace(replace(replace(replace(convert(varchar(100), getdate(), 121), '-', ''), ' ', ''), ':', ''),'.','')
----------------------------------
--09:12:55
select convert(varchar(100), getdate(), 8)
--09-29-20
select convert(varchar(100), getdate(), 10)
--20/09/29
select convert(varchar(100), getdate(), 11)
--200929
select convert(varchar(100), getdate(), 12)
--09:13:13
select convert(varchar(100), getdate(), 24)
--09:13:13:093
select convert(varchar(100), getdate(), 114)
--2020-09-29 09:13:13.093
select convert(varchar(100), getdate(), 121)
–当前日期+3天
PRINT CONVERT(VARCHAR(10),DATEADD(day,3,GETDATE()),120)
–还可以这样
–年
select datepart(YEAR,’2013-06-08′)
select datepart(yyyy,’2013-06-08′)
select datepart(yy,’2013-06-08′)
–月
select datepart(MONTH,’2013-06-08′)
select datepart(mm,’2013-06-08′)
select datepart(m,’2013-06-08′)
–日
select datepart(dd,’2013-06-08′)
–1年中的第多少天
select datepart(dy,’2013-06-08′)
–季度
select datepart(qq,’2013-06-08′)
–1年中的第多少周
select datepart(wk,’2013-06-08′)
–星期
select datepart(dw,’2013-06-08′) 会输出 7
select datepart(weekday, ‘2013-06-08’) 会输出 7
select datename(weekday, ‘2013-06-08’) 会输出 星期六
时间差
print datediff(year, '2020-05-01', getdate())
print datediff(month, '2020-05-01', getdate())
print datediff(day, '2020-05-01', getdate())
print datediff(hour, '2022-03-31', getdate())
print datediff(minute, convert(datetime,'2020-05-01 10:00:00'), getdate())
print datediff(second, convert(datetime,'2020-05-01 10:00:00'), getdate())
本周星期一 至 星期日
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) --本周星期一
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 1) --本周星期二
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 2) --本周星期三
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 3) --本周星期四
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 4) --本周星期五
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 5) --本周星期六
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 6) --本周星期日
上n周的星期一 至 星期日
--上1周星期一【1*7-1=6】 上1周星期日【1*7-7=0】
select convert(char(10),dateadd(dd, - DATEPART (weekday , getdate()+@@DATEFIRST -1)-6,getdate()),120)
select convert(char(10),dateadd(dd, - DATEPART (weekday , getdate()+@@DATEFIRST -1)-0,getdate()),120)
--上2周星期一【2*7-1=13】 上2周星期日【2*7-7=7】
select convert(char(10),dateadd(dd, - DATEPART (weekday , getdate()+@@DATEFIRST -1)-13,getdate()),120)
select convert(char(10),dateadd(dd, - DATEPART (weekday , getdate()+@@DATEFIRST -1)-7,getdate()),120)
--上3周星期一【3*7-1=20】 上3周星期日【3*7-7=14】
select convert(char(10),dateadd(dd, - DATEPART (weekday , getdate()+@@DATEFIRST -1)-20,getdate()),120)
select convert(char(10),dateadd(dd, - DATEPART (weekday , getdate()+@@DATEFIRST -1)-14,getdate()),120)
下n周星期一 至 周星期日
--下1周星期一【1*7+2=9】 下1周星期日【2*7+1=15】
SELECT DATEADD(D,(select 9-(select DATEPART(weekday,getdate()))), GETDATE());
SELECT DATEADD(D,(select 15-(select DATEPART(weekday,getdate()))), GETDATE());
--下2周星期一【2*7+2=16】 下2周星期日【3*7+1=22】
SELECT DATEADD(D,(select 16-(select DATEPART(weekday,getdate()))), GETDATE());
SELECT DATEADD(D,(select 22-(select DATEPART(weekday,getdate()))), GETDATE());
--下3周星期一【3*7+2=23】 下3周星期日【4*7+1=29】
SELECT DATEADD(D,(select 23-(select DATEPART(weekday,getdate()))), GETDATE());
SELECT DATEADD(D,(select 29-(select DATEPART(weekday,getdate()))), GETDATE());
--下4周星期一【4*7+2=30】 下4周星期日【5*7+1=36】
SELECT DATEADD(D,(select 30-(select DATEPART(weekday,getdate()))), GETDATE());
SELECT DATEADD(D,(select 36-(select DATEPART(weekday,getdate()))), GETDATE());
获取某个时间段内所有“月份”
declare @begin datetime,@end datetime
set @begin='2015-2-6'
set @end='2015-12-2'
declare @months int
set @months=DATEDIFF(month,@begin,@end)
select convert(varchar(7),DATEADD(month,number,@begin) ,120) AS 月份
from master.dbo.spt_values
where type='p' AND number<=@months
获取某个时间段内所有“日期”
SQL Pivot 行转列
DECLARE @days INT,
@date_start DATETIME = '2017-03-15',
@date_end DATETIME = '2017-04-13'
SET @days = DATEDIFF(DAY, @DATE_START, @DATE_END);
SELECT convert(char(10), DATEADD(dd, number, @DATE_START),112) AS yyyymmdd
FROM master.dbo.spt_values as spt
WHERE type = 'p'
AND number <= @days
--直接查询
SELECT convert(varchar(10), DATEADD(dd, number, '2017-03-15'),112) AS duty_date
FROM master.dbo.spt_values as spt
WHERE type = 'p' AND number <= 29
直接输出日期,效果图如下
DECLARE @sql2 NVARCHAR(4000)
DECLARE @days INT,
@date_start DATETIME = '2020-02-01',
@date_end DATETIME = '2020-02-10'
SET @days = DATEDIFF(DAY, @DATE_START, @DATE_END);
SELECT @sql2 = ISNULL(@sql2 + ',', '') + '['+convert(varchar(100), duty_date, 112)+']' FROM (
SELECT convert(varchar(10), DATEADD(dd, number, @DATE_START),112) AS duty_date
FROM master.dbo.spt_values as spt
WHERE type = 'p' AND number <= @days
) tt
GROUP BY duty_date
PRINT @sql2
go
查询小时数据,按小时分组
-- 按小时产生连续的
SELECT
substring(convert(char(32),DATEADD(HH,number,CONCAT('2019-01-18',' ', '00:00')),120),1,16) AS GroupDay,type
FROM
master..spt_values
WHERE type = 'p' AND DATEDIFF(HH,DATEADD(HH,number,CONCAT('2019-01-18',' ', '00:00')),CONCAT('2019-01-18',' ', '23:00'))>=0
-- 按小时统计交易笔数
declare @paySdate nvarchar(10) = '2021-05-22'
declare @payEdate nvarchar(10) = '2021-05-23'
declare @paySTime nvarchar(10) = '00:00'
declare @payETime nvarchar(10) = '23:59'
select a.GroupDay, ISNULL(b.e,0) 'feeCount' from (
SELECT
substring(convert(char(32),DATEADD(HH,number,CONCAT(@paySdate,' ', @paySTime)),120),1,16) AS GroupDay,type
FROM
master..spt_values
WHERE
type = 'p' AND DATEDIFF(HH,DATEADD(HH,number,CONCAT(@paySdate,' ', @paySTime)),CONCAT(@payEdate,' ', @payETime))>=0
) a
left join (
select
convert(char(32),created_time,23) as d, datepart(hh,created_time) as h,
substring(convert(char(32),DATEADD(HH,datepart(hh,created_time),convert(char(32),created_time,23)),120),1,16) as st,
count(*) as e
from
trans_order
where 1=1
and created_time >= @paySdate and created_time<=@payEdate
and convert(char(8),created_time,108)>=@paySTime and convert(char(8),created_time,108)<=@payETime
group by convert(char(32),created_time,23),datepart(hh,created_time)) b
on b.st=a.GroupDay order by GroupDay
查询小时数据,按小时分组
select isnull(convert(varchar(2), sv.number) + '-' + convert(varchar(2), sv.number + 1), '合计') AS [时间段]
--,sum(xiaoshoushu) AS [销量]
--,sum(shixiao) AS [实销]
--,sum(chengben) as [成本]
--,(sum(shoujia)-sum(chengben)) as [毛利]
--,(SELECT sum(shixiao) FROM xiaoshoux c WHERE DATEPART(hh, c.xiaoshoutime) <= ISNULL(sv.number, 23) ) AS [累计实销]
from master..spt_values sv
--join xiaoshoux x on (sv.number = DATEPART(hh, x.xiaoshoutime))
where sv.type='p' and sv.number >= 0 and sv.number < 24
--and x.xiaoshoutime between '2013-07-01' and '2013-07-16'
group by sv.number
with rollup
查询当天的数据
-- 今天的所有数据
select * from trans_queue where DateDiff(dd,registration_time,getdate())=0
-- 昨天的所有数据
select * from trans_queue where DateDiff(dd,registration_time,getdate())=1
-- 7天内的所有数据
select * from trans_queue where DateDiff(dd,registration_time,getdate())<=7
-- 30天内的所有数据
select * from trans_queue where DateDiff(dd,registration_time,getdate())<=30
-- 本月的所有数据
select * from trans_queue where DateDiff(mm,registration_time,getdate())=0
-- 本年的所有数据
select * from trans_queue where DateDiff(yy,registration_time,getdate())=0
-- 今天的所有数据
select * from trans_queue where 1=1
and registration_time > convert(varchar(10),dateadd(day,-1,getdate()),23) + ' 23:59:59'
and registration_time < convert(varchar(10),dateadd(day,1,getdate()),23)
查询当天的数据(EF Core)
var list = dbcontext.users.Where(x => ((DbFunctions)null).DateDiffDay(x.created_time, DateTime.Now) == 0).ToList();
/*
* 设置语言为英语
*/
set language N’English’ –设置语言为英语
select datename(weekday, ‘2013-06-08’) 会输出 Saturday
/*
* 设置语言为简体中文
*/
set language N’Simplified Chinese’ –设置语言为简体中文
select datename(weekday, ‘2013-06-08’) 会输出 星期六
/*
* 设置一周的第一天为星期一
*/
set datefirst 1 –设置一周的第一天为星期一(美国一周的第一天习惯为星期天)
select datepart(weekday, ‘2013-06-08’) 会输出 6
/*
* 设置一周的第一天为星期日
*/
set datefirst 7 –设置一周的第一天为星期日(美国一周的第一天习惯为星期天)
select datepart(weekday, ‘2013-06-08’) 会输出 7
/*
* 查询当前会话的 datefirst 设置值
*/
select @@datefirst
SELECT CONVERT(VARCHAR(10),GETDATE(),120) –2015-07-13
SELECT CONVERT(VARCHAR(10),GETDATE(),101) –07/13/2015
–按日分组:2013-01-01
select convert(nvarchar(10),CreateDate,120) as Times,ISNULL(sum(Unit),0.0) as Drinking from pdt_Out
group by convert(nvarchar(10),CreateDate,120)
go
–按月分组:2012-01
select DATEPART(month,CreateDate) as Times,sum(Unit) as Totals from pdt_Out
group by DATEPART(month,CreateDate)
go
–按年分组:2013
select DATEPART(year,CreateDate) as Times,sum(Unit) as Totals from pdt_Out
group by DATEPART(year,CreateDate)
go
–当前日期减一
SELECT DATEADD(dd,-1,GETDATE())
–当前日期减一(格式为:2018-01-01)
SELECT CONVERT(NVARCHAR(10),DATEADD(dd,-1,GETDATE()),120)
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/141494.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...