简体   繁体   English

SQL 查询解析字符串并将值与今天的日期进行比较

[英]SQL query to parse a string and compare the value with today's date

I have a data field with values: 2020Q3, 2020Q4, 2021Q1, 2021Q2, etc and I need to compare if Today's date > the data field value in SQL.我有一个数据字段,其值为:2020Q3、2020Q4、2021Q1、2021Q2 等,我需要比较今天的日期 > SQL 中的数据字段值。 For example, Today's date > 2020Q3 should return true.例如,今天的日期 > 2020Q3 应该返回 true。 Today's date > 2020Q4 should return false.今天的日期 > 2020Q4 应该返回 false。

I tried string tokenizer to parse and compare but no luck.我尝试了字符串标记器来解析和比较,但没有运气。 Please advise.请指教。 Thanks!谢谢!

If you have a value like 'YYYY"Q"Q' , you can get the start date of the quarter using:如果您有类似'YYYY"Q"Q'的值,则可以使用以下方法获取季度的开始日期:

select date(concat(left(yyyyq, 4), '-01-01')) + interval 3 * (right(yyyyq, 1) - 1) month
from (select '2020Q3' as yyyyq) t

And then add three months for the end date.然后添加三个月作为结束日期。 I'm not quite sure how you are interpreting > , but that information should give you what you need for the comparisons.我不太确定您是如何解释>的,但是该信息应该为您提供比较所需的信息。

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

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