怎样用T-SQL语句查询上月周六、周日的日期。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样用T-SQL语句查询上月周六、周日的日期。相关的知识,希望对你有一定的参考价值。

谁说SQL没有定义星期的函数!!datepart,datename都可以。 select number,dateadd(month,-1,getdate()-(datepart(day,getdate())))+number from master.dbo.spt_values where type='p' and number between 1 and 31 and datepart(weekday,dateadd(month,-1,getdate()-(datepart(day,getdate())))+number) in (1,7) 这个语句可以实现,原理是配合系统的master.dbo.spt_values 表查询日期是在1或7的(外国星期日是1) 参考技术A 怎样用T-SQL语句查询上月周六、周日的日期。
谁说SQL没有定义星期的函数!!datepart,datename都可以。 select number,dateadd(month,-1,getdate()-(datepart(day,getdate())))+number from master.dbo.spt_values where type='p' and number between 1 and 31 and datepart(weekday,dateadd(month,-1,getdate()-(datepart(day,getdate())))+number) in (1,7) 这个语句可以实现,原理是配合系统的master.dbo.spt_values 表查询日期是在1或7的(外国星期日是1)
参考技术B

我只知道SQLServer的语句写法

以下是我写的代码

SELECT CAST(GETDATE()-number AS DATE) date
FROM master..spt_values WHERE type='p'AND number<62
AND DATEDIFF(MONTH,GETDATE()-number ,GETDATE())=1
AND DATEPART(WEEKDAY,GETDATE()-number)IN(1,7)
ORDER BY date

表 master..spt_values 是系统一个数据表

type='p' :的  number 数据 值是从0开始的整数,

number<62 :已知两个月总的日期数不会超过61天 

DATEDIFF(MONTH,GETDATE()-number ,GETDATE())=1:表示本月减所得日期 正好月份差一个月

DATEPART(WEEKDAY,GETDATE()-number)IN(1,7)  1表示是周日 7表示周六

所以GETDATE()-number 这个值是上月的每天的当前时间

参考技术C select *from sn_Users where day(ctime) in('','','') and Month(ctime)='' and and year(ctime)='' T-sql 没有直接定位星期几的函数 c# java有 要查找 查询上月周六、周日的日期。只能先计算出日期

sql 查找下周一/周二/周三/周四/周五/周六/周日的日期


----------------------------Vertica-----------------------------------------------------------------------
select

Now()                          as Today,

next_day(Now(), 'Monday')      as NextMonday,       --returns the date of next Monday
next_day(Now(), 'Tuesday')     as NextTuesday,      --returns the date of next Tuesday
next_day(Now(), 'Wednesday')   as NextWednesday,    --returns the date of next Wednesday
next_day(Now(), 'Thursday')    as NextThursday,     --returns the date of next Thursday
next_day(Now(), 'Friday')      as NextFriday,       --returns the date of next Friday
next_day(Now(), 'Saturday')    as NextSaturday,     --returns the date of next Saturday
next_day(Now(), 'Sunday')      as NextSunday        --returns the date of next Sunday

Today      |NextMonday |NextTuesday |NextWednesday |NextThursday |NextFriday |NextSaturday |NextSunday |
-----------|-----------|------------|--------------|-------------|-----------|-------------|-----------|
2017-11-23 |2017-11-27 |2017-11-28  |2017-11-29    |2017-11-30   |2017-11-24 |2017-11-25   |2017-11-26 |

---------------------------SQL Server---------------------------------------------------------------------


select 

convert(date,getdate()) as Today,

-- 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Sturday

convert(date, dateadd(day, 2, getdate() - datepart(dw, getdate())
           + case when datepart(dw, getdate()) < 2 then 0 else 7 end )) as NextMonday,
           
convert(date, dateadd(day, 3, getdate() - datepart(dw, getdate())
           + case when datepart(dw, getdate()) < 3 then 0 else 7 end )) as NextTuesday,
           
convert(date, dateadd(day, 4, getdate() - datepart(dw, getdate())
           + case when datepart(dw, getdate()) < 4 then 0 else 7 end )) as NextWednesday,
           
convert(date, dateadd(day, 5, getdate() - datepart(dw, getdate())
           + case when datepart(dw, getdate()) < 5 then 0 else 7 end )) as NextThursday,
           
convert(date, dateadd(day, 6, getdate() - datepart(dw, getdate())
           + case when datepart(dw, getdate()) < 6 then 0 else 7 end )) as NextFriday,
           
convert(date, dateadd(day, 7, getdate() - datepart(dw, getdate())
           + case when datepart(dw, getdate()) < 7 then 0 else 7 end )) as NextSaturday,
           
convert(date, dateadd(day, 1, getdate() - datepart(dw, getdate())
           + case when datepart(dw, getdate()) < 1 then 0 else 7 end )) as NextSunday
	
		   
Today      |NextMonday |NextTuesday |NextWednesday |NextThursday |NextFriday |NextSaturday |NextSunday |
-----------|-----------|------------|--------------|-------------|-----------|-------------|-----------|
2017-11-23 |2017-11-27 |2017-11-28  |2017-11-29    |2017-11-30   |2017-11-24 |2017-11-25   |2017-11-26 |		   
		   
		   
----------------------------MySQL--------------------------------------------------------------------------

select 

date(Now()) as Today,

-- 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Sturday

date(adddate(Now(), 2 - dayofweek(Now())
           + case when dayofweek(Now()) < 2 then 0 else 7 end )) as NextMonday,

date(adddate(Now(), 3 - dayofweek(Now())
           + case when dayofweek(Now()) < 3 then 0 else 7 end )) as NextTuesday,

date(adddate(Now(), 4 - dayofweek(Now())
           + case when dayofweek(Now()) < 4 then 0 else 7 end )) as NextWednesday,

date(adddate(Now(), 5 - dayofweek(Now())
           + case when dayofweek(Now()) < 5 then 0 else 7 end )) as NextThursday,

date(adddate(Now(), 6 - dayofweek(Now())
           + case when dayofweek(Now()) < 6 then 0 else 7 end )) as NextFriday,
           
date(adddate(Now(), 7 - dayofweek(Now())
           + case when dayofweek(Now()) < 7 then 0 else 7 end )) as NextSaturday,
           
date(adddate(Now(), 1 - dayofweek(Now())
           + case when dayofweek(Now()) < 1 then 0 else 7 end )) as NextSunday
           

Today      |NextMonday |NextTuesday |NextWednesday |NextThursday |NextFriday |NextSaturday |NextSunday |
-----------|-----------|------------|--------------|-------------|-----------|-------------|-----------|
2017-11-23 |2017-11-27 |2017-11-28  |2017-11-29    |2017-11-30   |2017-11-24 |2017-11-25   |2017-11-26 |
       

---------------------------------PostgreSQL--------------------------------------------------------------------           
          
select
-- 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday
---------------------------------------------------------------------------------------------------------------
-- timestamp + cast((num_of_days || ' day') as interval) 
---OR  
-- timestamp + "interval" (num_of_days || ' days')
----------------------------------------------------------------------------------------------------------------
Date(Now()) as Today,

Date(Now() + cast((1 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 1 then 0 else 7 end) || ' day' as interval)) as NextMonday,

Date(Now() + cast((2 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 2 then 0 else 7 end) || ' day' as interval)) as NextTuesday, 
           
Date(Now() + cast((3 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 3 then 0 else 7 end) || ' day' as interval)) as NextWednesday,
           
Date(Now() + cast((4 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 4 then 0 else 7 end) || ' day' as interval)) as NextThursday,
           
Date(Now() + cast((5 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 5 then 0 else 7 end) || ' day' as interval)) as NextFriday,
           
Date(Now() + cast((6 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 6 then 0 else 7 end) || ' day' as interval)) as NextSaturday,
           
Date(Now() + cast((7 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 7 then 0 else 7 end) || ' day' as interval)) as NextSunday		  
		   

today      |nextmonday |nexttuesday |nextwednesday |nextthursday |nextfriday |nextsaturday |nextsunday |
-----------|-----------|------------|--------------|-------------|-----------|-------------|-----------|
2017-11-23 |2017-11-27 |2017-11-28  |2017-11-29    |2017-11-30   |2017-11-24 |2017-11-25   |2017-11-26 |


--------------------------------------OR------------------------------------------------------------------

select
-- 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday
---------------------------------------------------------------------------------------------------------------
-- timestamp + cast((num_of_days || ' day') as interval) 
---OR  
-- timestamp + "interval" (num_of_days || ' days')
----------------------------------------------------------------------------------------------------------------

Date(Now()) as Today,

Date(Now() + "interval" ((1 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 1 then 0 else 7 end) || ' day')) as NextMonday,

Date(Now() + "interval" ((2 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 2 then 0 else 7 end) || ' day')) as NextTuesday, 
           
Date(Now() + "interval" ((3 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 3 then 0 else 7 end) || ' day')) as NextWednesday,
           
Date(Now() + "interval" ((4 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 4 then 0 else 7 end) || ' day')) as NextThursday,
           
Date(Now() + "interval" ((5 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 5 then 0 else 7 end) || ' day')) as NextFriday,
           
Date(Now() + "interval" ((6 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 6 then 0 else 7 end) || ' day')) as NextSaturday,
           
Date(Now() + "interval" ((7 - extract(dow from Now()))
           + (case when extract(dow from Now()) < 7 then 0 else 7 end) || ' day')) as NextSunday	


以上是关于怎样用T-SQL语句查询上月周六、周日的日期。的主要内容,如果未能解决你的问题,请参考以下文章

SQL语句怎么查周六周天

用for循环计算任意两个日期之间的工作日(去掉周六周日)

用javascript计算任意两个日期之间的工作日(去掉周六周日),不用while循环,而是用for循环

在 oracle 中运行基于周末的查询

oracle中怎么得到日期相减除去周末后的天数

php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期