Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用字母表从列中查询日期_Sql_Oracle - Fatal编程技术网

Sql 使用字母表从列中查询日期

Sql 使用字母表从列中查询日期,sql,oracle,Sql,Oracle,我想写一个查询来获取oracle中的日期范围。但问题是列包含带有日期值的前缀 例如(AA 2018-02-01) 我想获取日期范围从1月到3月的数据。 我希望使用select查询,而不是pl/sql查询 with testdata as (select 'XX' column1, 'AA 2018-01-02' column2 from dual union all select 'YY' column1, 'BB 2018-02-01' column2 from dual union all

我想写一个查询来获取oracle中的日期范围。但问题是列包含带有日期值的前缀 例如(AA 2018-02-01)

我想获取日期范围从1月到3月的数据。 我希望使用select查询,而不是pl/sql查询

with testdata as
(select 'XX' column1, 'AA 2018-01-02' column2 from dual union all
 select 'YY' column1, 'BB 2018-02-01' column2 from dual union all
 select 'ZZ' column1, 'CC 2018-03-01' column2 from dual union all
 select 'XZ' column1, 'DD 2018-04-01' column2 from dual)

select column1,
       column2,
       to_date(substr(column2,4),'YYYY-MM-DD') date_column
from testdata;
您所要做的就是使用substr并使用与数据匹配的日期格式字符串来指定日期。运行上述查询将生成以下结果:

column1     column2         date_column
XX          AA 2018-01-02   02-JAN-18
YY          BB 2018-02-01   01-FEB-18
ZZ          CC 2018-03-01   01-MAR-18
XZ          DD 2018-04-01   01-APR-18
如果您只想将时间限制在1月到3月,您可以这样做:

select column1,
       column2,
       to_date(substr(column2,4),'YYYY-MM-DD') date_column,
       extract(month from to_date(substr(column2,4),'YYYY-MM-DD')) date_month
from testdata
where extract(month from to_date(substr(column2,4),'YYYY-MM-DD')) between 1 and 3;
结果:

column1     column2         date_column     date_month
XX          AA 2018-01-02   02-JAN-18       1
YY          BB 2018-02-01   01-FEB-18       2
ZZ          CC 2018-03-01   01-MAR-18       3

“column2”的位置没有意义。你能把这个格式化吗
column1     column2         date_column     date_month
XX          AA 2018-01-02   02-JAN-18       1
YY          BB 2018-02-01   01-FEB-18       2
ZZ          CC 2018-03-01   01-MAR-18       3