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

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

Related

How in Oracle to calculate difference between current date and column date?

I have column INACTIVE_TIME where I need to put integer number (how many days pass from some date), to represent difference between current date and column date ("LOAD_DATE" column).
In column LOAD_DATE I have data in format 03-AUG-22 03.55.57.587481000 PM.
I understand I need to get current date and than minus date from LOAD_DATE column.
I try something like this:
SELECT COUNT(*)
FROM TABLE_NAME
WHERE ((TO_DATE(SYSDATE,'DD/MM/YYYY')-(TO_DATE(LOAD_DATE,'DD/MM/YYYY'));
It is about load_date column's datatype, not the way you see that value (because it can be changed). I presume (and hope) it is timestamp; you aren't storing it as a string, are you?
If so, then you don't apply to_date to sysdate - it is a function that already returns date datatype.
Setting timestamp and date format (just to know what is what; your tool displays different format, with month name and two-digits year) (you don't have to do that).
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss.ff9';
Session altered.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
Sample table; note datatype:
SQL> create table table_name (load_date timestamp);
Table created.
SQL> insert into table_name values (systimestamp);
1 row created.
Query you're looking for (at least, I think so):
SQL> select load_date, sysdate,
2 --
3 sysdate - load_date as diff
4 from table_name;
LOAD_DATE SYSDATE DIFF
------------------------------ ------------------- ------------------------------
04.08.2022 10:22:58.101062000 04.08.2022 10:23:08 +000000000 00:00:09.898938
SQL>
To extract days, hours, minutes ... whatever, you can use that function - extract. For example:
SQL> select load_date,
2 sysdate,
3 sysdate - load_date as diff,
4 --
5 extract (day from sysdate - load_date) as diff_days,
6 extract (hour from sysdate - load_date) as diff_hours,
7 extract (minute from sysdate - load_date) as diff_minutes
8 from table_name;
LOAD_DATE SYSDATE DIFF DIFF_DAYS DIFF_HOURS DIFF_MINUTES
------------------------- ------------------- -------------------------- ---------- ---------- ------------
04.08.22 10:22:58,101062 04.08.2022 11:51:32 +000000000 01:28:33.898938 0 1 28
SQL>
Your Where clause isn't saying anything. What are you wanting it to filter?
Try
Where (sysdate - table_name.load_date) > 0
This might not be what you want, but you need to tell the query something else

how to get name of a month in sql oracle

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>

Why doesn't date subtraction produce whole numbers when casting TIMESTAMP to DATE?

Normal date subtraction looks like this:
SELECT TO_DATE('12-29-2019') - TO_DATE('12-20-2019') FROM DUAL
/* RESULT: 9 */
When I cast a TIMESTAMP to a DATE, Oracle truncates the hours/minutes/seconds and produces a "whole" DATE value.
SELECT CAST(LOCALTIMESTAMP AS DATE) FROM DUAL
/* RESULT: 12/07/2019 */
But when performing date subtraction with a CAST from a TIMESTAMP, I don't get whole numbers anymore.
SELECT TO_DATE('12-29-2019') - CAST(LOCALTIMESTAMP AS DATE) FROM DUAL
/* RESULT: 21.0999421296296296296296296296296296296 */
Why doesn't date subtraction produce whole numbers when casting TIMESTAMP to DATE in Oracle?
Because what you see is not what you have.
This was your command and result:
SQL> SELECT CAST(LOCALTIMESTAMP AS DATE) FROM DUAL;
CAST(LOC
--------
07.12.19
But, that's just because date format was set as such.
If you alter session and set different format, then you get
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> SELECT CAST(LOCALTIMESTAMP AS DATE) FROM DUAL;
CAST(LOCALTIMESTAMP
-------------------
07.12.2019 22:44:47
SQL>
which is quite different, is it not? And that's why you got decimal number as a result. TRUNC it first to remove time component.

Oracle Date Comparsion

I have a column in TableA which contains date as Varchar2 datatype for column Start_date.( '2011-09-17:09:46:13').
Now what i need to do is , compare the Start_date of TableA with the SYSDATE, and list out any values thts atmost 7days older than SYSDATE.
Can any body help me with this isssue.
You may perform the below to check the date:
select * from
TableA
where
to_date(start_date,'YYYY-MM-DD') between sysdate and sysdate-7;
something like
select * from tableA
where start_date between sysdate-7 and sysdate
I have a column in TableA which contains date as Varchar2 datatype for column Start_date.( '2011-09-17:09:46:13').
Then you have a flawed design. You must use appropriate data types for the data. A datetime should always be stored as DATE data type.
Now what i need to do is , compare the Start_date of TableA with the SYSDATE, and list out any values thts atmost 7days older than SYSDATE.
Since the data type of your column is VARCHAR2, you must use TO_DATE to explicitly convert the string into date.
where to_date(start_date,'YYYY-MM-DD HH24:MI:SS') between sysdate and sysdate-7;
Remember, a DATE has both date and time elements. IF you want to ignore the time portion, then you need to use TRUNC.
For example,
SQL> alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS';
Session altered.
SQL> SELECT sysdate FROM DUAL;
SYSDATE
-------------------
2015-07-28 16:03:32
SQL> SELECT TRUNC(sysdate) FROM DUAL;
TRUNC(SYSDATE)
-------------------
2015-07-28 00:00:00
SQL>
So, BETWEEN sysdate AND sysdate -7 will consider the current datetime till past 7 days till that time portion. If you only want to consider the date portion, use TRUNC.
For example,
where to_date(start_date,'YYYY-MM-DD') between TRUNC(sysdate) and TRUNC(sysdate-7);
Although you ought to be storing your times as DATE data types, you are at least storing the dates in a format that allows greater-than/less-than comparisons.
So, while you ought to convert these columns to dates, or alternatively convert the values to dates for comparison with SYSDATE -7, you could also convert SYSDATE -7 to the same string format as you are storing.
For example:
start_date between to_char(sysdate -7, "YYYY-MM-DD HH24:MI:SS") and
to_char(sysdate , "YYYY-MM-DD HH24:MI:SS")
This would let you use an indexed search without needing a function-based index on the start date column.

Oracle SQL Roll back date to beginning of month best practice CONCAT & SUBSTR or TRUNC

I have a field date which is a date format.
It outputs like this for example.
09-NOV-14
Now what i want the output to be is any date rolled back to the 1st of its month.
01-NOV-14
What i did to achieve this is
CONCAT('01',SUBSTR(table.date,3))
Is this the most efficient/best practice way of doing this?
moved from comment to answer:
trunc(date,'month')
The output format you get when a date is cast to string is configurable:
SQL> SELECT SYSDATE FROM DUAL;
SYSDATE
---------
26-NOV-14
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
Session altered.
SQL> SELECT SYSDATE FROM DUAL;
SYSDATE
-------------------
2014-11-26 16:15:44
... thus your approach is not particularly robust. To use date functions (rather than string manipulation) please check Make date time's first day of its month.