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

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"')

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

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

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;

SQLDeveloper query automatic padding CHAR field

Given the table ATABLE with a field AFIELD of type CHAR(8), and where i have a field with value "1234567 "
Why, in SQL Developer, if I query:
SELECT * FROM ATABLE WHERE AFIELD = '1234567';
It will automatically pad the missing space and return the results, and if I query with :
SELECT * FROM ATABLE WHERE AFIELD = :value;
and input the value, it wont ?
From the documentation:
Within expressions and conditions, Oracle treats text literals as though they have the data type CHAR by comparing them using blank-padded comparison semantics.
When you do WHERE AFIELD = '1234567' the text literal '1234567' is treated as char and blank-padded comparison semantics are used to compare the column value and the literal. Even though the literal doesn't have the trailing space, those semantics see them as the same, so it finds a match.
When you use a bind variable the literal you assign to it is a char, but the bind variable itself is varchar2 - even if you declare it as char, oddly, though in that case the value is blank-padded anyway:
var char_value char(8);
exec :char_value := '1234567';
var varchar2_value varchar2(8);
exec :varchar2_value := '1234567';
select dump('1234567') as d1, dump(:char_value) as d2, dump(:varchar2_value) as d3
from dual;
D1 D2 D3
------------------------------------ ------------------------------------ ------------------------------------
Typ=96 Len=7: 49,50,51,52,53,54,55 Typ=1 Len=8: 49,50,51,52,53,54,55,32 Typ=1 Len=7: 49,50,51,52,53,54,55
The text literal is data type 96 (char), while both bind variables are type 1 (varchar/varchar2); but notice the char_value bind variable has the trailing space, with length 8 and the last character as code point 32.
When you compare your char column value with a varchar2 bind variable the column value is implicitly converted from char to varchar2:
The following rules govern implicit data type conversions:
During SELECT FROM operations, Oracle converts the data from the column to the type of the target variable.
So your space-padded char(8) column value is implicitly converted to varchar2(8) to match the bind variable's data type, and then because they are varchar2 the nonpadded comparison semantics are used.
When you compare your char(8) column with the supposedly-char(8) bind variable, you're actually comparing with a padded varchar2(8) - but both the implicitly converted column value and the blank-padded bind variable are actually the same, both with the trailing space; '1234567 ' is the same as '1234567 ', so there is a match, even with nonpadded comparison semantics.
With the varchar2(8) bind variable the same thing happens, but now the bound value is not padded, and as you are using nonpadded comparison semantics to compare '1234567 ' with '1234567' - they are not the same, so there is no match, and no data is returned by the query.
As #a_horse_with_no_name said you should almost always use varchar2 rather than char. But if you must use it and are stuck with it then at least make sure you use the same data type for comparisons.
You are right
SELECT * FROM ATABLE WHERE AFIELD = :value;
does not work with CHAR as you desire.
Anyway I have noticed that the following query works as you desire:
SELECT * FROM ATABLE WHERE AFIELD = &value;
If you use &value in several places, you can use &&value (double &) the first time (and &value elsewhere),
in order to avoid to input the same value several times;
when you have to change that value, you can undefine it with:
undef value;

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;

How do I find out which columns and rows contain Extended ASCII codes?

Can any one help How to find out which columns and rows has Extended ASCII Codes in the table (EX:-Ž,™,Ù)?
Examples
jacob\Û
=pal®
I need query some thing like Select * from table to get Extended ASCII
Thanks For help
You can try with:
SELECT *
FROM mytable
WHERE mycolumn<>CONVERT(mycolumn, 'US7ASCII');
You can use TRANSLATE to remove all valid characters from the string, so only the special characters remain. Then check for NULL (as an empty string is NULL in Oracle; don't use length, for the length will not be 0 as one would expect but null):
select name
from mytable
where TRANSLATE(name, '®ABCDEFG...abc...', '®') is not null;
You will have to put all valid characters in the string where I simply put '...'.
I used one special character to replace itself as you see, because otherwise the replacement string had to be empty, but empty means null in Oracle and translate doesn't work with null.
(Yes, that empty string is null thing in Oracle is really a nuisance.)

Resources