Oracle星期几(无字符串比较)

时间:2018-08-08 16:12:09

标签: sql oracle oracle11g

我将以此为开头说我发现的所有文档都不适用于我的oracle版本: https://docs.oracle.com/cd/E51711_01/DR/WeekDay.html; https://docs.oracle.com/cd/E37483_01/server.751/es_eql/src/ceql_functions_date_extract.html

或我可以使用的功能参考没有提及星期几: https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions052.htm#SQLRF00639

,因此我只想找到使用to_char进行此操作的方法,然后进行varchar2比较。如果可能的话,我只想坚持日期格式。

到目前为止,这是我的SQL

select dateSold from sales
where extract(dateSold, day_of_week) in (1, 3, 4, 7)

SQL Error: ORA-00904: "DAY_OF_WEEK": invalid identifier

1 个答案:

答案 0 :(得分:4)

您需要使用to_char()来获取日期,但是您可以将其转换为数字:

select dateSold, sum(quantity) from sales
where to_number(to_char(dateSold, 'D')) in (1, 3, 4, 7)

但是D是NLS依赖的,因此,如果在美国和法国进行的会话中运行此命令,则会得到不同的结果。这也许就是您看到字符串比较的原因,因为您至少可以控制更多:

select dateSold, sum(quantity) from sales
where to_char(dateSold, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') in ('MON', 'WED', 'THU', 'SUN')

要查看NLS设置的不同之处,将显示DDY值,用于相同的数据和查询,就像在美国运行一样:

alter session set nls_territory = 'AMERICA';
alter session set nls_language = 'ENGLISH';

with cte (dateSold) as (
  select date '2018-08-01' + level - 1 from dual connect by level <= 7
)
select dateSold,
  to_number(to_char(dateSold, 'D')) as d,
  to_char(dateSold, 'DY') as dy,
  to_char(dateSold, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') as dy_english
from cte;

DATESOLD           D DY           DY_ENGLISH  
--------- ---------- ------------ ------------
01-AUG-18          4 WED          WED         
02-AUG-18          5 THU          THU         
03-AUG-18          6 FRI          FRI         
04-AUG-18          7 SAT          SAT         
05-AUG-18          1 SUN          SUN         
06-AUG-18          2 MON          MON         
07-AUG-18          3 TUE          TUE         

然后好像在法国运行一样:

alter session set nls_territory = 'FRANCE';
alter session set nls_language = 'FRENCH';

with cte (dateSold) as (
  select date '2018-08-01' + level - 1 from dual connect by level <= 7
)
select dateSold,
  to_number(to_char(dateSold, 'D')) as d,
  to_char(dateSold, 'DY') as dy,
  to_char(dateSold, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') as dy_english
from cte;

and the same thing as if run in the USA:

DATESOLD          D DY               DY_ENGLISH  
-------- ---------- ---------------- ------------
01/08/18          3 MER.             WED         
02/08/18          4 JEU.             THU         
03/08/18          5 VEN.             FRI         
04/08/18          6 SAM.             SAT         
05/08/18          7 DIM.             SUN         
06/08/18          1 LUN.             MON         
07/08/18          2 MAR.             TUE         

请注意,日期编号和名称/缩写完全不同,因此尝试将它们与固定值进行比较-无论是in (1, 3, 4, 7)还是使用字符串文字的日期名称-都不可靠。

将日期语言强制为英语可确保比较安全。 (当然,也可以是任何其他语言-字符串文字值只需与您为to_char()的第三个参数选择的语言匹配即可。)