Convert a date string in oracle pl/sql [duplicate] - oracle

How can I convert this string date to datetime in oracle.
2011-07-28T23:54:14Z
Using this code throws an error:
TO_DATE('2011-07-28T23:54:14Z', 'YYYY-MM-DD HH24:MI:SS')
How can this be done?
Error report:
SQL Error: ORA-01861: literal does not match format string
01861. 00000 - "literal does not match format string"
*Cause: Literals in the input must be the same length as literals in
the format string (with the exception of leading whitespace). If the
"FX" modifier has been toggled on, the literal must match exactly,
with no extra whitespace.
*Action: Correct the format string to match the literal.
Update:-
TO_DATE('2011-07-28T23:54:14Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
I only see the date not time in the column
28-JUL-11

Try this:
TO_DATE('2011-07-28T23:54:14Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')

Hey I had the same problem. I tried to convert '2017-02-20 12:15:32' varchar to a date with TO_DATE('2017-02-20 12:15:32','YYYY-MM-DD HH24:MI:SS') and all I received was 2017-02-20 the time disappeared
My solution was to use TO_TIMESTAMP('2017-02-20 12:15:32','YYYY-MM-DD HH24:MI:SS') now the time doesn't disappear.

You can use a cast to char to see the date results
select to_char(to_date('17-MAR-17 06.04.54','dd-MON-yy hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') from dual;

Related

Oracle: Why dateStr and dateFmt using different date format can work well in Oracle TO_DATE( dateStr, dateFmt)?

Oracle version:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
Test:
select TO_DATE('2022.02.21','YYYY-MM-DD') from dual; --- convert success
select TO_DATE('20220221','YYYY-MM-DD') from dual; --- convert success
select TO_DATE('2022/02/21','YYYY-MM-DD') from dual; --- convert success
Why dateStr and dateFmt using different date format can work well in Oracle TO_DATE( dateStr, dateFmt)?
Oracle will ignore punctuation characters:
String-to-Date Conversion Rules
The following additional formatting rules apply when converting string values to date values (unless you have used the FX or FXFM modifiers in the format model to control exact format checking):
You can omit punctuation included in the format string from the date string if all the digits of the numerical format elements, including leading zeros, are specified. For example, specify 02 and not 2 for two-digit format elements such as MM, DD, and YY.
You can omit time fields found at the end of a format string from the date string.
You can use any non-alphanumeric character in the date string to match the punctuation symbol in the format string.
If a match fails between a datetime format element and the corresponding characters in the date string, then Oracle attempts alternative format elements, as shown in Table 2-20.
Table 2-20 Oracle Format Matching
Original Format Element
Additional Format Elements to Try in Place of the Original
'MM'
'MON' and 'MONTH'
'MON'
'MONTH'
'MONTH'
'MON'
'YY'
'YYYY'
'RR'
'RRRR'
Unless you use the FX format modifier:
FX
Format exact. This modifier specifies exact matching for the character argument and datetime format model of a TO_DATE function:
Punctuation and quoted text in the character argument must exactly match (except for case) the corresponding parts of the format model.
The character argument cannot have extra blanks. Without FX, Oracle ignores extra blanks.
Numeric data in the character argument must have the same number of digits as the corresponding element in the format model. Without FX, numbers in the character argument can omit leading zeros.
When FX is enabled, you can disable this check for leading zeros by using the FM modifier as well.
If any portion of the character argument violates any of these conditions, then Oracle returns an error message.
For example, given the queries:
select TO_DATE('2022.02.21','fxYYYY-MM-DD') from dual;
select TO_DATE('20220221','fxYYYY-MM-DD') from dual;
select TO_DATE('2022/02/21','fxYYYY-MM-DD') from dual;
select TO_DATE('2022-02-21','fxYYYY-MM-DD') from dual;
Then only the last one will work and the first 3 will raise the exception:
ORA-01861: literal does not match format string
db<>fiddle here

Spring Batch: SQL error ORA-01858: a non-numeric character was found where a numeric was expected

I'm trying to use this SQL in my spring batch reader. The SQL seems to have a problem:
select DISTINCT ces.COR_ENBL_STG_ID
from HBX_BATCH_COR_ENBL_STG ces
Inner JOIN HBX_INDV_NEG_ACTION ina ON ces.INDV_ID=ina.INDV_ID
where ces.DISP_PROCESSED_FLAG='Y'
AND ina.NEG_ACTION_RUN_RSN_CD in('11054','11055','11065')
AND ces.PGM_BGN_DT+90<'#{jobExecutionContext['latest.completed.startTime']}'
The value of latest.completed.start.time is coming as '07-Oct-16 12:38:58.000000109 PM' from a tasklet using jdbctemplate and hence the SQL is throwing the following error : ORA-01858: a non-numeric character was found where a numeric was expected.
Just FYI the column ces.PGM_BGN_DT is a Date Type. I tried TO_DATE function but it didn't help. Any suggestions please?
As I understand latest.completed.startTime - is a string of characters.
And its look like as oracle timestamp datatype.
If my suggestion are true, You should use format to convert string to timestamp
to_timestamp(latest.completed.startTime, 'dd-mon-yy HH12:MI:SS.FF AM');

Oracle: How can I specify character literals in TO_DATE conversions?

How do I specify character literals in a date specification? In the second example, I would like to skip the T and Z.
select to_date('2015-04-06 19:56:30', 'YYYY-MM-DD HH24:MI:SS') from dual;
2015-04-06 19:56:30
select to_date('2015-04-06 19:56:30', 'YYYY-MM-DDTHH24:MI:SSZ') from dual;
ORA-01821: date format not recognized
You can enclose the literals in double quotes:
SQL> select to_date('2015-04-06T19:56:30Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') from dual;
TO_DATE('2015-04-0
------------------
06-APR-15
From the documentation,
Punctuation and Character Literals in Datetime Format Models
You can include these characters in a date format model:
Punctuation such as hyphens, slashes, commas, periods, and colons
Character literals, enclosed in double quotation marks
These characters appear in the return value in the same location as
they appear in the format model.
Following the documentation, enclosing the character literals in double-quotation marks will work in the format model.
TO_DATE('2015-04-06T19:56:30Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')

How to format strings to numbers with apostrophe as group separator in Oracle

In Switzerland the number format is as following.
1'234.56
with the group separator the apostrophe or simple quote '.
How can I format a string in Oracle so it is shown in this way?
This works for the comma:
select to_char(1234.56, '999G999D99', q'[NLS_NUMERIC_CHARACTERS=.,]') from dual
I tried the same approach with the simple quote:
select to_char(1234.56, '999G999D99', q'[NLS_NUMERIC_CHARACTERS=.']') from dual
But I get this error:
ORA-12702: invalid NLS parameter string used in SQL function
12702. 00000 - "invalid NLS parameter string used in SQL function"
*Cause: An unknown parameter name or invalid value is specified in a NLS
parameter string.
*Action:
select to_char(1234.56, '999G999D99', 'NLS_NUMERIC_CHARACTERS=''.''''') from dual;
With a quote operator:
select to_char(1234.56, '999G999D99', q'[NLS_NUMERIC_CHARACTERS='.'']') from dual;
Try this
select to_char(1234.56, '999G999D99', 'NLS_NUMERIC_CHARACTERS=''.''''') from dual;

Querying datetime in oracle

I have a date type column in a table, where I store date along with time.
I want to query it by WHERE clause
I did it this way:
select *
from conference_hall_book
where to_date(end_time,'dd/mon/yyyy hh24:mi:ss') <= to_date('26/oct/2013 15:00:00','dd/mon/yyyy hh24:mi:ss')
But the result has 27/10/2013 8:00:00 AM also in end_time column.
Can any one help me finding the mistake?
The problem occurs because of
to_date(end_time,'dd/mon/yyyy hh24:mi:ss')
This is a wrong usage of the to_date function. To_date converts a string to a date.
When Oracle sees this expression, it will automatically convert the end_time value to a string, using the configured date format of your database/session. This format typically doesn't include the time part, so a date with the value of "27/10/2013 8:00:00 AM" will be converted to the string "27/10/2013" (if your database date format is dd/mm/yyyy).
Your to_date expression will then convert the string value "27/10/2013" back to a date. The resulting date value will be "27/10/2013 00:00:00", so you will have lost the time portion of your original date.
The simple and correct solution is to drop the to_date(end_time) expression and just use end_time. This will also ensure that if you have index on end_time, the query will be able to use that index.
select *
from conference_hall_book
where end_time <= to_date('26/oct/2013 15:00:00','dd/mon/yyyy hh24:mi:ss')

Resources