How do you convert YYYY_MM to date in Oracle SQL? - sql

I have a column which has dates as texts. Like: '2021_01' (I will reefer this column as TextDate)
I convert it to '2021-01' with this code:
SELECT REPLACE(at.TextDate,'_','-') as DataFormat FROM tableName at
But when I try to cast it to date, or convert it to date, I always get 'Missing expression' errors. I tried this:
SELECT REPLACE(CONVERT(VARCHAR(7), at.TextDate, 126, '_', '-') as date FROM tableName at
But it will give me errors. Any suggestion?

convert means something completely different in Oracle than it does elsewhere. You need the to_date() function:
SELECT TO_DATE(at.textDate, 'YYYY_MM') as DataFormat FROM tableName at
If you want to display it in a particular format then you can either let your client/application do that - most clients by default will use your session's NLS_DATE_FORMAT setting - or explicitly convert it back to a string with the complementary to_char() function.
db<>fiddle
The valid date elements are also in the documentation. You should only convert to a string for display though; while you are manipulating or storing it you should treat it as a date.
How can I filter last 3 months with it?
You need to use Oracle syntax, not SQL Server or other syntax. You also can't refer to a column alias in the same level of query. SO you can recalculate the date value; or as your string format is relatively sane you can convert the target date to a string and compare that, which might allow an index to be used. Something like:
SELECT TO_DATE(at.textDate, 'YYYY_MM') as DataFormat
FROM tableName at
WHERE at.textDate >= TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -3), 'YYYY_MM')
db<>fiddle

TO_DATE with appropriate format mask.
Just to know what's what:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
Code:
SQL> select to_date('2021-01', 'yyyy_mm') from dual;
TO_DATE('2021-01','
-------------------
01.01.2021 00:00:00
SQL>

Related

Oracle's SQL how to convert the date's format

this is my table:
table name EXAMPLE
column name DATE
this is the output for the following query:
SELECT date
FROM example;
1/23/2010
I want to convert the output to:
23-Jan-10
Is it possible?
Thanks
You can use formatting along with TO_CHAR() conversion such as
SELECT TO_CHAR(dt,'DD-Mon-RR')
FROM example
where
DATE is not a good name for a column as being a reserved keyword. So, I've replaced it with dt
If your column is in varchar/text.. then you can first convert to date then back to char
SELECT
TO_CHAR(
to_date('1/23/2010','mm/dd/yyyy'),'DD-Mon-YY'
)
datec FROM dual;
If it is in date format, to_char only will do
SELECT
TO_CHAR(date_column, 'DD-Mon-YY' )
datec FROM dual;
A DATE data type is a binary format that is stored in 7-bytes that has no format and always contains the components: year (stored as century and year-of-century), month, day, hour, minute and second.
Therefore, you cannot change the format of a DATE data type.
If instead, you ask the question:
How can format a DATE to output it as 23-Jan-10?
Then you can convert the DATE to a formatted string using the TO_CHAR function:
SELECT TO_CHAR(your_date_column, 'DD-MON-YY', 'NLS_DATE_LANGUAGE=English')
AS formatted_date
FROM your_table;
Alternatively, if you want the output as a DATE data type then you can use:
SELECT your_date_column
FROM your_table;
and change the user interface (SQL/Plus, SQL Developer, Toad, PLSQLDeveloper, PHP, Java, etc.) you are using to alter how that that user interface displays dates. The solution is going to depend on which user interface you are using but for SQL/Plus and SQL Developer, you can change Oracle's NLS_DATE_FORMAT session parameter:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YY';
Other user interfaces will have different solutions specific to those interfaces.

how to use substr function with to_date in sql?

query is :
select substr(to_date('01-02-2018','mm-dd-yyyy'),4,3) from dual;
output is :
JAN
can anyone explain how the output came ?
When you apply a text function directly to something that's of DATE datatype, you force an implicit conversion of the date into a string. This conversion uses the NLS_DATE_FORMAT parameter to decide the format of the output string.
In effect,
substr(to_date('01-02-2018','mm-dd-yyyy'),4,3)
is the same as
substr(to_char(to_date('01-02-2018','mm-dd-yyyy'), <NLS_DATE_FORMAT>),4,3)
The usual default value (for English-language versions of the database) of the NLS_DATE_FORMAT parameter is DD-MON-RR - which it sounds like the value of your NLS_DATE_FORMAT parameter is set to, which means your query is doing:
substr(to_char(to_date('01-02-2018','mm-dd-yyyy'), 'DD-MON-RR'),4,3)
Therefore, the substr is working on the string 02-JAN-18, and the 3 characters starting from the 4th character is JAN.
Rather than use substr on a date, you would do better to use to_char instead, e.g.:
to_char(to_date('01-02-2018', 'mm-dd-yyyy'), 'MON')

Format string to Datetime

I am currently in the process of creating a query on our Oracle DB in Microsoft Query. Somehow, I only get the date from the Oracle DB as a string. The format looks like this: "YYYY-DD-MM SS:MM:HH".
Excel recognizes the column at the end only as a string.
How can I have the column output directly in the date format "DD.MM.YYYY"?
Can I change the format directly via Select?
SELECT
DB_Gen.STRT,
DB_Gen.SRST,
DB_Gen.DOCO,
DB_Gen.WR02,
DB_Gen.WR03,
DB_Gen.UORG,
DB_Gen.LITM
FROM
MCC.POOLDB DB_Gen
WHERE
(DB_Gen.WR03 Like 'G%')
AND (DB_Gen.MCU='AMC')
AND (DB_Gen.DCTO='WO')
AND (DB_Gen.WR01<>'EX')
AND (DB_Gen.STRT Between TRUNC(TO_DATE('01.01.2019','dd.mm.yyyy'))
AND TRUNC(TO_DATE('01.09.2019','dd.mm.yyyy')))
Do you even know how I can display the last 60 days?
So far, I'm only talking about Between and Trunc(TO_DATE .....
Best Regards
Joshua
How I can display the last 60 days?
where DB_Gen.STRT >= trunc(sysdate) - 60
How can I have the column output directly in the date format
"DD.MM.YYYY"?
select to_char(DB_Gen.STRT, 'DD.MM.YYYY') as strt
I think you're looking for
DB_Gen.STRT BETWEEN SYSDATE - 60 AND SYSDATE --Or CURRENT_DATE
What I prefer is to set NLS_DATE_FORMAT at session level to the desired format if I had to access dates in my query multiple times.
In your case, the following is my suggestion:
-- setting date format at session-level
alter session set NLS_DATE_FORMAT = 'DD.MM.YYYY';
SELECT
DB_Gen.STRT, -- this date will be populated in DD.MM.YYYY format in result
DB_Gen.SRST,
DB_Gen.DOCO,
DB_Gen.WR02,
DB_Gen.WR03,
DB_Gen.UORG,
DB_Gen.LITM
FROM
MCC.POOLDB DB_Gen
WHERE
(DB_Gen.WR03 Like 'G%')
AND (DB_Gen.MCU='AMC')
AND (DB_Gen.DCTO='WO')
AND (DB_Gen.WR01<>'EX')
AND (TRUNC(DB_Gen.STRT) BETWEEN TRUNC(SYSDATE-60) AND TRUNC(SYADATE);
-- Trunc is used to ignore time portion
Cheers!!

Convert date to month year format and pass it in the query

I have a BIRT report where user will be entering the dates in dd-mm-yyyy format however I need to convert dd-mm-yyyy to MON-YYYY format.
I have tried to use VARCHAR_FORMAT(FIELDNAME,'MON-YYYY') however it doesn't work.
select …….
where VARCHAR_FORMAT(fieldname,'MON-YYYY') = '2017-05-15';
User would end the date as
15/05/2017
The value present in the database for this field is 2017-05-15 07:30:00.0
update
Apparently the column is not a string but a datetime which means the conversion is only
to_date(fieldname, 'MON-YYYY')
But if the column is used in a Where clause it shouldn’t be converted at all.
——
Use to_date and to_char to first convert your string to a date and then back to a string with the right format
to_char(to_date(fieldname, 'DD-MM-YYYY'), 'MON-YYYY')
select *
from table (values
timestamp('2017-05-15-07.30.00')
) t(fieldname)
where
fieldname between to_date('15/05/2017', 'DD/MM/YYYY') and to_date('15/05/2017', 'DD/MM/YYYY') + 1 day
--date(fieldname) = to_date('15/05/2017', 'DD/MM/YYYY')
;
You may run it as is.
Both cases work, but to use the 2-nd one efficiently, you must create an index by the date(fieldname) expression (since db2 10.5) or add generated always column to the table with the same expression and index on it.

ORA-01843 not a valid month- Comparing Dates

I have a problem when try to select data from a table filtering by date.
For example:
SELECT * FROM MYTABLE WHERE MYTABLE.DATEIN = '23/04/49';
The Oracle Error is:
Informe de error:
Error SQL: ORA-01843: mes no válido
01843. 00000 - "not a valid month"
*Cause:
*Action:
Probably the source data of table is corrupted, in this case:
How can i solve this problem?
Can I change this dates for null?
The results of this select, select * from nls_session_parameters; , is:
PARAMETER VALUE
------------------------------ ----------------------------------------
NLS_LANGUAGE SPANISH
NLS_TERRITORY SPAIN
NLS_CURRENCY ¿
NLS_ISO_CURRENCY SPAIN
NLS_NUMERIC_CHARACTERS ,.
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD/MM/RR
NLS_DATE_LANGUAGE SPANISH
NLS_SORT SPANISH
NLS_TIME_FORMAT HH24:MI:SSXFF
NLS_TIMESTAMP_FORMAT DD/MM/RR HH24:MI:SSXFF
NLS_TIME_TZ_FORMAT HH24:MI:SSXFF TZR
NLS_TIMESTAMP_TZ_FORMAT DD/MM/RR HH24:MI:SSXFF TZR
NLS_DUAL_CURRENCY ¿
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE
You should use the to_date function (oracle/functions/to_date.php
)
SELECT * FROM MYTABLE WHERE MYTABLE.DATEIN = TO_DATE('23/04/49', 'DD/MM/YY');
You are comparing a date column to a string literal. In such a case, Oracle attempts to convert your literal to a date, using the default date format.
It's a bad practice to rely on such a behavior, as this default may change if the DBA changes some configuration, Oracle breaks something in a future revision, etc.
Instead, you should always explicitly convert your literal to a date and state the format you're using:
SELECT * FROM MYTABLE WHERE MYTABLE.DATEIN = TO_DATE('23/04/49','MM/DD/YY');
If you don't need to check exact timestamp, use
SELECT * FROM MYTABLE WHERE trunc(DATEIN) = TO_DATE('23-04-49','DD-MM-YY');
otherwise, you can use
SELECT * FROM MYTABLE WHERE DATEIN = TO_DATE('23-04-49 20:18:07','DD-MM-YY HH24:MI:SS');
Here, you use hard code date,if you directly compare then you must use DD-MM-YY HH24:MI:SS else you might get ORA-01849: hour must be between 1 and 12.
I know this is a bit late, but I'm having a similar issue. SQL*Plus executes the query successfully, but Oracle SQL Developer shows the ORA-01843: not a valid month error.
SQL*Plus seems to know that the date I'm using is in the valid format, whereas Oracle SQL Developer needs to be told explicitly what format my date is in.
SQL*Plus statement:
select count(*) from some_table where DATE_TIME_CREATED < '09-12-23';
VS
Oracle SQL Developer statement:
select count(*) from some_table where DATE_TIME_CREATED < TO_DATE('09-12-23','RR-MM-DD');
Just in case this helps, I solved this by checking the server date format:
SELECT * FROM nls_session_parameters WHERE parameter = 'NLS_DATE_FORMAT';
then by using the following comparison (the left field is a date+time):
AND EV_DTTM >= ('01-DEC-16')
I was trying this with TO_DATE but kept getting an error. But when I matched my string with the NLS_DATE_FORMAT and removed TO_DATE, it worked...
In a comment to one of the answers you mention that to_date with a format doesn't help. In another comment you explain that the table is accessed via DBLINK.
So obviously the other system contains an invalid date that Oracle cannot accept. Fix this in the other dbms (or whatever you dblink to) and your query will work.
Having said this, I agree with the others: always use to_date with a format to convert a string literal to a date. Also never use only two digits for a year. For example '23/04/49' means 2049 in your system (format RR), but it confuses the reader (as you see from the answers suggesting a format with YY).
If the source date contains minutes and seconds part, your date comparison will fail.
you need to convert source date to the required format using to_char and the target date also.
If you are using command line tools, then you can also set it in the shell.
On linux, with a sh type shell, you can do for example:
export NLS_TIMESTAMP_FORMAT='DD/MON/RR HH24:MI:SSXFF'
Then you can use the command line tools and it will use the specified format:
/path/to/dbhome_1/bin/sqlldr user/pass#host:port/service control=table.ctl direct=true
Try using:
SELECT *
FROM MYTABLE
WHERE MYTABLE.DATEIN is not null
AND MYTABLE.DATEIN = '23/04/49';
Use the month as a string.
Example:
(12-Apr-2002) or (12-April-2002)
Although the answers using TO_DATE are correct, I prefer to use the ANSI SQL format for dates:
DATEIN = DATE '1949-04-23'
It works in Oracle and other DBMS ANSI SQL compliant. This is specially important if your application is DBMS agnostic.
Try alter session set NLS_DATE_FORMAT='DD/MM/YY'; -- or whatever format you want
I faced the same problem, on PROD, all the code were already in this format, but on preprod, it's not set,
So this means you change the default date format used by oracle
ALTER session set NLS_LANGUAGE=’AMERICAN’;