问题描述
我试图让它显示 1994 年 6 月 20 日之后雇用的员工数量,
I'm trying to get it to display the number of employees that are hired after June 20, 1994,
Select employee_id, count(*)
From Employee
Where to_char(employee_date_hired, 'DD-MON-YY') > 31-DEC-95;
但我收到一条错误消息
JUN"无效标识符.
请帮忙,谢谢!
推荐答案
31-DEC-95
不是字符串,20-JUN-94
也不是字符串.它们是在末尾添加了一些额外内容的数字.这应该是 '31-DEC-95'
或 '20-JUN-94'
- 注意单引号,'
.这将使您能够进行字符串比较.
31-DEC-95
isn't a string, nor is 20-JUN-94
. They're numbers with some extra stuff added on the end. This should be '31-DEC-95'
or '20-JUN-94'
- note the single quote, '
. This will enable you to do a string comparison.
但是,您不是在进行字符串比较;您正在进行日期比较.您应该将字符串转换为日期.通过使用内置的 TO_DATE()
函数,或 日期文字.
However, you're not doing a string comparison; you're doing a date comparison. You should transform your string into a date. Either by using the built-in TO_DATE()
function, or a date literal.
select employee_id
from employee
where employee_date_hired > to_date('31-DEC-95','DD-MON-YY')
这种方法有一些不必要的陷阱
This method has a few unnecessary pitfalls
- 正如评论中提到的 a_horse_with_no_name,
DEC
并不一定意味着 12 月.这取决于您的NLS_DATE_LANGUAGE
和NLS_DATE_FORMAT
设置.为了确保您在任何语言环境中的工作进行比较,您可以使用 日期时间格式模型MM
代替 - 95 年是不准确的.你知道你的意思是 1995 年,但如果是 50 年,那是 1950 年还是 2050 年呢?最好是明确的
- As a_horse_with_no_name noted in the comments,
DEC
, doesn't necessarily mean December. It depends on yourNLS_DATE_LANGUAGE
andNLS_DATE_FORMAT
settings. To ensure that your comparison with work in any locale you can use the datetime format modelMM
instead - The year '95 is inexact. You know you mean 1995, but what if it was '50, is that 1950 or 2050? It's always best to be explicit
select employee_id
from employee
where employee_date_hired > to_date('31-12-1995','DD-MM-YYYY')
日期文字
日期文字是 ANSI 标准的一部分,这意味着您不必使用特定于 Oracle 的函数.使用文字时,必须以 YYYY-MM-DD
格式指定日期,并且不能包含时间元素.
Date literals
A date literal is part of the ANSI standard, which means you don't have to use an Oracle specific function. When using a literal you must specify your date in the format YYYY-MM-DD
and you cannot include a time element.
select employee_id
from employee
where employee_date_hired > date '1995-12-31'
请记住,Oracle 日期数据类型包含时间元素,因此没有时间部分的日期等效于 1995-12-31 00:00:00
.
Remember that the Oracle date datatype includes a time elemement, so the date without a time portion is equivalent to 1995-12-31 00:00:00
.
如果要包含时间部分,则必须使用时间戳文字,其格式为 YYYY-MM-DD HH24:MI:SS[.FF0-9]
If you want to include a time portion then you'd have to use a timestamp literal, which takes the format YYYY-MM-DD HH24:MI:SS[.FF0-9]
select employee_id
from employee
where employee_date_hired > timestamp '1995-12-31 12:31:02'
更多信息
NLS_DATE_LANGUAGE
源自 NLS_LANGUAGE
和 NLS_DATE_FORMAT
源自 NLS_TERRITORY代码>.这些是在您最初创建数据库时设置的,但可以通过更改初始化参数文件(仅在确实需要时)或在会话级别使用
ALTER SESSION
语法.例如:
Further information
NLS_DATE_LANGUAGE
is derived from NLS_LANGUAGE
and NLS_DATE_FORMAT
is derived from NLS_TERRITORY
. These are set when you initially created the database but they can be altered by changing your inialization parameters file - only if really required - or at the session level by using the ALTER SESSION
syntax. For instance:
alter session set nls_date_format = 'DD.MM.YYYY HH24:MI:SS';
这意味着:
DD
月份中的数字日期,1 - 31MM
数字月份,01 - 12(一月是 01)YYYY
4 位数年份 - 在我看来,这 总是 比 2 位数年份YY
好,因为与哪个世纪没有混淆你指的是.HH24
一天中的小时,0 - 23MI
小时的分钟数,0 - 59SS
分钟的秒数,0-59
DD
numeric day of the month, 1 - 31MM
numeric month of the year, 01 - 12 ( January is 01 )YYYY
4 digit year - in my opinion this is always better than a 2 digit yearYY
as there is no confusion with what century you're referring to.HH24
hour of the day, 0 - 23MI
minute of the hour, 0 - 59SS
second of the minute, 0-59
您可以通过查询 V$NLS_PARAMETERSs
来了解您当前的语言和日期语言设置,通过查询 V$NLS_VALID_VALUES
可以找到所有有效值.
You can find out your current language and date language settings by querying V$NLS_PARAMETERSs
and the full gamut of valid values by querying V$NLS_VALID_VALUES
.
- 格式化模型
顺便说一下,如果你想要 count(*)
你需要按 employee_id
Incidentally, if you want the count(*)
you need to group by employee_id
select employee_id, count(*)
from employee
where employee_date_hired > date '1995-12-31'
group by employee_id
这会为您提供per employee_id
的计数.
This gives you the count per employee_id
.
这篇关于比较 Oracle SQL 中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!