繁体   English   中英

sql,获取范围内的日期,但也使用getDate

[英]sql, get date within a range but also using getDate

我想获取介于某些日期之间的日期,但是我也需要使用GETDATE函数,因为它必须是从去年12月1日到当年3月31日,我可以将日期连接起来并放在一个陈述之间

[编辑]我正在使用sql服务器,下面的代码是

select c.nombre, p.folio, pg.fecha, pg.monto
from Cliente c, Contratante ct, póliza p, Pago pg
where c.IdCli = ct.IdCli and p.IdCli = ct.IdCli and p.folio = pg.folio  


我得到以下结果

Jorge González  300 2018-12-20 00:00:00.000 10000,00
Jorge González  300 2019-06-30 00:00:00.000 7000,00
Alejandro Lara  310 2018-12-30 00:00:00.000 8000,00
Francisco Hernández 320 2019-03-31 00:00:00.000 5000,00
Marcela Ocampo  330 2019-06-30 00:00:00.000 5000,00

我只想获取2018年12月1日至2019年3月31日之间的数据,但我需要使用当年作为参考

以下内容将使您获得“ 20181201”:

select convert(char(4), year(getdate()) - 1) + '12' + '01'

和'20190331':

select convert(char(4), year(getdate())) + '03' + '31'

您可以结合使用getdatefromparts,year和getdate:

以下为您提供2108-12-01

select datefromparts(year(getdate())-1,12,1) 

以下为您提供2019-03-31

select datefromparts(year(getdate()),3,31)

如果日期是静态的,那么最好的选择是结合使用extractgetdate函数。

select * from my_table
where my_date between 
  convert(date, year(getdate() - 1) + '-12-01') 
  and convert(date, year(getdate()) + '-03-01')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM