how to get name of a month in sql oracle - sql

Display the month name of date “14-jul-15” in full.
I have tried multiple ways but i am not able to get it to display the name of the month.

Use TO_CHAR function with appropriate format mask.
SQL> select to_char(sysdate, 'fm dd-Month-yyyy') result
2 from dual;
RESULT
------------------
25-March-2021
SQL>
Or, if all you have is a string '14-jul-15', the first convert it to a valid date value (using TO_DATE), and then apply TO_CHAR to it.
I'm using then NLS_DATE_LANGUAGE parameter as well because my database speaks Croatian so "jul" is invalid month for me. You might not need it.
SQL> with test (col) as
2 (select '14-jul-15' from dual)
3 select
4 to_char(
5 to_date(col, 'dd-mon-yy', 'nls_date_language = english'),
6 'fm dd-Month-yyyy', 'nls_date_language = english'
7 ) result
8 from test;
RESULT
------------------
14-July-2015
SQL>

Related

Transform date formats

I have a column in an Oracle table which is varchar and it contains different date formats like ‘dd-mom-yyyy’, ‘dd-mm-yyyy’, ‘dd/mm/yy’.
I need to transform all these formats into a single one, keeping the column as varchar.
As already commented, idea of storing dates as strings is bad. It always is.
You said you want to keep values as they are; it means that you'd actually just want to display values as valid dates. To do that, one option is to create your own function which accepts various values, tries to convert them to date and return it (if it succeeds).
You'd add other formats into the function, once you find them.
For example:
SQL> create or replace function f_format (par_datum in varchar2) return date is
2 retval date;
3 begin
4 -- DD-MON-YYYY
5 begin
6 retval := to_date(par_datum, 'dd-mon-yyyy', 'nls_date_language = english');
7 exception
8 when others then
9
10 -- DD-MM-YYYY
11 begin
12 retval := to_date(par_datum, 'dd-mm-yyyy');
13 exception
14 when others then
15
16 -- DD/MM/YYYY
17 begin
18 retval := to_date(par_datum, 'dd/mm/yyyy');
19 exception
20 when others then
21 null;
22 end;
23 end;
24 end;
25
26 return retval;
27 end;
28 /
Function created.
Let's try it (setting date format, just to know what is what):
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
varchar2 column accepts even garbage; 45-19-2001 looks like date, but it is not.
SQL> select id, datum, f_format(datum) result
2 from test
3 order by id;
ID DATUM RESULT
---------- -------------------- ----------
1 25-OCT-1998 25.10.1998
2 13-02-2022 13.02.2022
3 03/11/1984 03.11.1984
4 45-19-2001
SQL>
From Oracle 12, you can use TO_DATE with the DEFAULT NULL ON CONVERSION ERROR option to convert the string to a date against a known format and TO_CHAR to convert it back to a string and use COALESCE to check multiple formats:
UPDATE table_name
SET date_column = COALESCE(
TO_CHAR(
COALESCE(
TO_DATE(date_column DEFAULT NULL ON CONVERSION ERROR, 'fxDD-MM-RR'),
TO_DATE(date_column DEFAULT NULL ON CONVERSION ERROR, 'DD-Mon-YYYY', 'NLS_DATE_LANGUAGE=English'),
TO_DATE(date_column DEFAULT NULL ON CONVERSION ERROR, 'YYYY-MM-DD'),
TO_DATE(date_column DEFAULT NULL ON CONVERSION ERROR, 'MM-DD-YYYY')
),
'YYYY-MM-DD'
),
date_column
)
Then, for the sample data:
CREATE TABLE table_name (date_column) AS
SELECT '2023-01-25' FROM DUAL UNION ALL
SELECT '01-25-2023' FROM DUAL UNION ALL
SELECT '25-01-23' FROM DUAL UNION ALL
SELECT '25-Jan-2023' FROM DUAL UNION ALL
SELECT 'Wed 25th January 2023' FROM DUAL
After the update, the table contains:
DATE_COLUMN
2023-01-25
2023-01-25
2023-01-25
2023-01-25
Wed 25th January 2023
fiddle
Note: This normalises all the date formats that are tested in the UPDATE statement and will leave the non-matching formats as they were (so you will not lose data).
Even better would be to store the values as a DATE data type and not a string.

Extracting the month of the date in a column [duplicate]

This question already has answers here:
Get month name from date in Oracle
(7 answers)
Closed 2 years ago.
I need help in extracting the month of the date in a certain column.
For example the whole table is called A and the column for date is called END_TIME which has a format like this MM-DD-YYYY HH24:MI:SS.
I want my output to be the name of the month.
If END_TIME column's datatype is DATE (should be, looks like it is), then you should apply TO_CHAR function to it with desired format mask. Here's an example:
SQL> create table a as select sysdate end_time from dual;
Table created.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select * From a;
END_TIME
-------------------
13.08.2020 08:51:04
SQL> select to_char(end_time, 'Month', 'nls_date_language = english') mon from a;
MON
---------
August
SQL>
Thats pretty starightforward you can make use of to_char function like below.
select to_char(sysdate, 'MONTH') FROM DUAL;
Already answered here

More on extracting year from date - Oracle

I have a question about selecting year from a date. This is in Oracle database 12c.
Given that SELECT trunc(SYSDATE) FROM DUAL; returns 02/06/2020
These work proper and return current year of 2020 -
SELECT EXTRACT(YEAR FROM trunc(SYSDATE)) FROM DUAL;
SELECT TO_CHAR(trunc(SYSDATE,'YYYY')) FROM DUAL;
These do not work and give error -
SELECT EXTRACT(YEAR FROM '02/06/2019') FROM DUAL;
Gives error: ORA-30076: invalid extract field for extract source
SELECT TO_CHAR('02/06/2019','YYYY') FROM DUAL;
Gives error: ORA-01722: invalid number
The same format is being passed with sysdate and hard coded date of 02/06/2019. Why is it that one works and the other does not?
I know I could just select 2019 from dual but that is not the point or use case here.
You can't extract year from a string (which '02/06/2019' is). First convert it to date:
SQL> SELECT EXTRACT(YEAR FROM to_date('02/06/2019', 'dd/mm/yyyy')) year FROM DUAL;
YEAR
----------
2019
SQL>
Or, if you know that last 4 digits are valid year, then
SQL> select substr('02/06/2019', -4) year from dual;
YEAR
----
2019
SQL>
It comes down to the data type being passed. sysdate by default is a DATE field. A hard date like '02/06/2020' by default is considered a string.
To get around that, just cast the string as a date. All good.
SELECT TO_CHAR(cast('6-feb-2019' as date),'YYYY') FROM DUAL;

literal does not match format string-version 11

In my SQL query I have,
TO_DATE('2019-07-22' , 'YYYY-MM-DD')
I want to pass sysdate-1 instead of hard coding the date value.
I tried this,
select TO_CHAR(to_date(
sysdate-1,'DD-Mon-YY'),'YYYY-MM-DD')dates from dual
and replaced the same in the SQL query but am getting the below 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.
Sysdate is a function that returns DATE datatype; you don't TO_DATE it. Therefore:
SQL> select to_char(sysdate - 1, 'yyyy-mm-dd') result from dual;
RESULT
----------
2019-08-01
SQL>
If you meant to use date datatype, then - as comments suggest - there's no need for any TO_something function:
SQL> select sysdate as today,
2 sysdate - 1 as yesterday
3 from dual;
TODAY YESTERDA
-------- --------
02.08.19 01.08.19
SQL>
The result of such a subtraction is date datatype. The way it is presented to you depends on NLS settings; in my database, it is set to dd.mm.yy. In order to present it differently, you'd either modify settings or apply to_char with appropriate format mask, e.g.
SQL> alter session set nls_date_format = 'yyyy-mm-dd';
Session altered.
SQL> select sysdate as today,
2 sysdate - 1 as yesterday
3 from dual;
TODAY YESTERDAY
---------- ----------
2019-08-02 2019-08-01
SQL>
Although sysdate is a date data type, Oracle still stores a time component.
Your code (attempts to) remove the time component. For this, use trunc():
select trunc(sydate - 1)
or
select trunc(sysdate - interval '1' day)
Note that the time component is often now shown when you select the data and look at it.

Can the TO_CHAR in ORACLE HAVE (TO_CHAR(04-28-2017, 'yyyy')) INSTEAD OF Passing a Column name as TO_CHAR(sysdate, 'yyyy/mm/dd')

In general, we pass a column name to the TO_CHAR CLause as TO_CHAR(sysdate, 'yyyy/mm/dd').
I want to know whether can we pass a date in the place of column name as (TO_CHAR(04-28-2017, 'yyyy'))
No, you can't because TO_CHAR function expects either number or date/time value as it's parameter. This can be tested easily.
SQL> select to_char('12-04-2017','DD-MM-YYYY') from dual;
select to_char('12-04-2017','DD-MM-YYYY') from dual
*
ERROR at line 1:
ORA-01722: invalid number
You need to convert the string to date.
SQL> select to_char(to_date('12-04-2017','DD-MM-YYYY'),'YYYY') from dual;
TO_C
----
2017
OR
SQL> select to_char(date '2017-04-12','YYYY') from dual;
TO_C
----
2017