I'm trying to get it to display the number of employees that are hired after June 20, 1994, But I get an error saying "JUN' invalid identifier. Please help, thanks!
Select employee_id, count(*)
From Employee
Where to_char(employee_date_hired, 'DD-MON-YY') > 31-DEC-95;
31-DEC-95
isn't a string, nor is20-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.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.TO_DATE()
This method has a few unnecessary pitfalls
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
insteadDate 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.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
.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]
Further information
NLS_DATE_LANGUAGE
is derived fromNLS_LANGUAGE
andNLS_DATE_FORMAT
is derived fromNLS_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 theALTER SESSION
syntax. For instance:This means:
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-59You can find out your current language and date language settings by querying
V$NLS_PARAMETERSs
and the full gamut of valid values by queryingV$NLS_VALID_VALUES
.Further reading
Incidentally, if you want the
count(*)
you need to group byemployee_id
This gives you the count per
employee_id
.Conclusion,
to_char
works in its own waySo,
Always use this format YYYY-MM-DD for comparison instead of MM-DD-YY or DD-MM-YYYY or any other format
Single quote must be there, since date converted to character.
You can use trunc and to_date as follows:
from your query:
i think its not to display the number of employees that are hired after June 20, 1994. if you want show number of employees, you can use:
I think for best practice to compare dates you can use: