jan 1st yyyy(总是前一年)到12月31日yyyy(总是前一年)之间的数据查询where子句

编程入门 行业动态 更新时间:2024-10-23 19:20:30
本文介绍了jan 1st yyyy(总是前一年)到12月31日yyyy(总是前一年)之间的数据查询where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 如果我想今天(2017年2月12日)运行查询,那么我的数据应该在2016年1月1日之间(始终是前一年) )到12月31日(总是去年) - 我不想硬编码日期。 如果我想在2018年1月1日或以后运行查询2018年的任何一天,我的数据应该在2017年1月1日(始终是前一年)到2017年12月31日(总是前一年)之间。 如果我想要运行在2019年查询然后我的数据应该在2018年1月1日(总是前一年)到2018年12月31日(总是前一年)之间。 怎么写查询,请指教。 我的尝试: 大家好, 如果我想今天(2017年2月12日)运行查询,那么我的数据应介于2016年1月1日(始终是前一年)至12月31日之间(始终是前一年) ) - 我不想硬编码日期。 如果我愿意的话在2018年1月或2018年的任何一天运行查询,那么我的数据应该在2017年1月1日(始终是前一年)到2017年12月31日(始终是前一年)之间。 如果我想在2019年运行查询,那么我的数据应该是2018年1月1日(总是前一年)到2018年12月31日(总是去年)之间。 如何编写查询,请指教。

Hi all, If I want to run the query today(12/2/2017) then my data should be inbetween 2016 January 1st (always previous year) to December 31st (always previous year) --I don't want to hard code dates. if I want to run the query on jan 1st 2018 or in any day in 2018 then my data should be inbetween 2017 January 1st(always previous year) to 2017 December 31st(always previous year). if I want to run the query in 2019 then my data should be inbetween 2018 January 1st (always previous year)to 2018 December 31st (always previous year). how to write query, please advice. What I have tried: Hi all, If I want to run the query today(12/2/2017) then my data should be inbetween 2016 January 1st (always previous year) to December 31st (always previous year) --I don't want to hard code dates. if I want to run the query on jan 1st 2018 or in any day in 2018 then my data should be inbetween 2017 January 1st(always previous year) to 2017 December 31st(always previous year). if I want to run the query in 2019 then my data should be inbetween 2018 January 1st (always previous year)to 2018 December 31st (always previous year). how to write query, please advice.

推荐答案

查看SQL函数: SQL Server DATEADD()函数 [ ^ ]和年份(Transact -SQL)| Microsoft Docs [ ^ ]或 SQL Server DATEPART()函数 [ ^ ] 获取年份,构建Jan的DATE,年。使用DATEADD -1天为最后一天,-1岁为开始日。 Look at SQL Functions: SQL Server DATEADD() Function[^] and YEAR (Transact-SQL) | Microsoft Docs[^] or SQL Server DATEPART() Function[^] Get the year, construct a DATE of Jan first that year. Use that with DATEADD -1 day for last day, and -1 year for start day.

我解决了它: ADD_MONTHS(TRUNC(SYSDATE,') YEAR'), - 12) 和ADD_MONTHS(TRUNC(SYSDATE,'YEAR'), - 1)+30) 谢谢OriginalGriff。 I resolved it : tween ADD_MONTHS (TRUNC (SYSDATE,'YEAR'), -12) and ADD_MONTHS (TRUNC (SYSDATE, 'YEAR'), -1 ) +30 ) thank you OriginalGriff.

更多推荐

jan 1st yyyy(总是前一年)到12月31日yyyy(总是前一年)之间的数据查询where子句

本文发布于:2023-10-28 23:30:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538084.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   数据查询   st   jan   yyyy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!