简体   繁体   English

将年份连接到Oracle SQL中的日期

[英]Concatenate year to a date in Oracle SQL

I'm trying to make a query dynamic that looks for all the dates between two days of the year (July 1 of the last year and June 30th of this year). 我正在尝试使查询动态化,以查找一年中两天(去年7月1日至今年6月30日)之间的所有日期。 Below is the where clause of the query I've been attempting: 以下是我一直在尝试的查询的where子句:

and pbh.batch_date between
        ('30-JUN-'||extract(year from trunc(sysdate))-1))
    and ('01-JUL-'||extract(year from trunc(sysdate))))

This returns an inconsistent datatypes error: expected DATE got NUMBER. 这将返回不一致的数据类型错误:预期DATE得到NUMBER。 I then tried casting them to dates like so: 然后,我尝试将它们强制转换为类似这样的日期:

and pbh.batch_date between
        to_date(( '01-JUL-'||extract(year from trunc(sysdate))-1))
    and to_date(( '30-JUN-'||extract(year from trunc(sysdate))))

But this now returns an invalid number error. 但这现在返回一个无效的数字错误。 I don't know if it makes a difference what format pbh.batch_date is stored as, but it's (m)m/(d)d/yyyy 我不知道pbh.batch_date是以什么格式存储的,但这是(m)m /(d)d / yyyy

There's no need to cast to a string and back: 无需强制转换为字符串并返回:

and ph.batch_date >= add_months(trunc(sysdate,'YEAR'), -6) 
and ph.batch_date <  add_months(trunc(sysdate,'YEAR'), 6)

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

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