Home > Back-end >  TO_CHAR(a_date, 'Day') not querying as expected
TO_CHAR(a_date, 'Day') not querying as expected

Time:10-28

I can clearly See combinations of Friday and Monday in this query:

SELECT DISTINCT TO_CHAR(DATE_CASE_CLOSED, 'Day') AS DAY_CLOSED, TO_CHAR(DATE_REPORT_SUBMITTED, 'Day') AS DAY_SUBMITTED 
from V_MY_DATA

enter image description here

But when I then do this, I get no records:

select * from V_MY_DATA 
WHERE TO_CHAR(DATE_CASE_CLOSED, 'Day') = 'Friday' 

Update: Seems the result is a char (padded).....

CodePudding user response:

Seems the result is a char (padded)

It is padded.

From the documentation:

Many datetime format elements are padded with blanks or leading zeroes to a specific length.

and:

The character elements MONTH, MON, DAY, and DY are padded with trailing blanks to the width of the longest full month name, the longest abbreviated month name, the longest full date name, or the longest abbreviated day name, respectively, among valid names determined by the values of NLS_DATE_LANGUAGE and NLS_CALENDAR parameters. For example, when NLS_DATE_LANGUAGE is AMERICAN and NLS_CALENDAR is GREGORIAN (the default), the largest element for MONTH is SEPTEMBER, so all values of the MONTH format element are padded to nine display characters.

You can use the FM format modifier to avoid the padding; so do:

TO_CHAR(DATE_CASE_CLOSED, 'FMDay')

and to be safe specify the language:

TO_CHAR(DATE_CASE_CLOSED, 'FMDay', 'NLS_DATE_LANGUAGE=English')

CodePudding user response:

See the section on Datetime Format Elements

https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/Format-Models.html#GUID-22F2B830-261E-4BF0-91FB-6A1DAFC6D0A3

Also FM

https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/Format-Models.html#GUID-A4CCA8BD-1679-432E-96BA-22FB46FF23E0

  • Related