Extract Month from date as number - sql

I have a query that accepts the date in the following format:
'31 AUG 2012'
I need the query to return the month as a number. For the above date, the returned value would be 08
I have tried the following:
EXTRACT(MONTH FROM DATE '31 AUG 2012')
TO_DATE('31 AUG 2012', 'MM')
TO_CHAR('31 AUG 2012', 'MM')
All of which give me the below errors respectively:
ORA-01861: literal does not match format string
ORA-01843: not a valid month
ORA-01722: invalid number
How can this be accomplished? Thanks

SELECT EXTRACT(MONTH FROM date '2012-08-31') FROM dual;

EXTRACT(MONTH FROM to_date('31 AUG 2012','DD MON YYYY'))
The date '' operator only accepts ISO format ...
date '2012-08-31'

Actually what you made mistake is you given only 'MM' as the format_mask instead you give 'DD/MM/YYYY', hope that would work as well
SELECT TO_CHAR(SYSDATE,'DD/MM/YYYY HH24:MI:SS') FROM AB;
Output: 26/03/2013 15:15:34
If you need only the month to be displayed try using this:
SELECT EXTRACT(MONTH FROM SYSDATE) FROM AB;
the above will display the MAR as 3(considering today's date)

You have to convert 08 to 8 because 08 is character field so i have used to_number() over to_char so that it will return exact number.
select to_number(to_char(to_date('31-AUG-2012','DD-MON-YYYY'),'MM')) from your_tbl;

select substr(to_date(sysdate,'dd-mon-yyyy'),4,3) from dual;

Related

Get the month name from a timestamp column [duplicate]

How to fetch month name from a given date in Oracle?
If the given date is '15-11-2010' then I want November from this date.
select to_char(sysdate, 'Month') from dual
in your example will be:
select to_char(to_date('15-11-2010', 'DD-MM-YYYY'), 'Month') from dual
Try this,
select to_char(sysdate,'dd') from dual; -> 08 (date)
select to_char(sysdate,'mm') from dual; -> 02 (month in number)
select to_char(sysdate,'yyyy') from dual; -> 2013 (Full year)
to_char(mydate, 'MONTH') will do the job.
In Oracle (atleast 11g) database :
If you hit
select to_char(SYSDATE,'Month') from dual;
It gives unformatted month name, with spaces, for e.g. May would be given as 'May '. The string May will have spaces.
In order to format month name, i.e to trim spaces, you need
select to_char(SYSDATE,'fmMonth') from dual;
This would return 'May'.
If you are trying to pull the value from a field, you could use:
select extract(month from [field_name])
from [table_name]
You can also insert day or year for the "month" extraction value above.
if you are taking system date:
--Full month name :
select to_char(trunc(sysdate,'MONTH'),'MONTH') as month from dual; --MARCH
--Short month name:
select to_char(trunc(sysdate,'MON'),'MON') as month from dual; --MAR
--Month number:
select to_char(trunc(sysdate,'MM'),'MM') as month from dual; --03
if you are taking a specific date:
--Full month's name:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MONTH'),'MONTH') as month from dual; --MARCH
--Short month's name:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MON'),'MON') as month from dual; --MAR
--Month's number:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MM'),'MM') as month from dual; --03
Try this
select to_char(SYSDATE,'Month') from dual;
for full name and try this
select to_char(SYSDATE,'Mon') from dual;
for abbreviation
you can find more option here:
https://www.techonthenet.com/oracle/functions/to_char.php

How do I correct the year in the date?

There is a date field for users.birth_date.
This field contains a date, for example, Mon, 18 Dec 1989.
How to take this date into account in the current year, for example, Mon, 18 Dec 2021
I do this so that I know the exact day of the week (1-Monday, 2-Tuesday,...) in the current year.
To find the day of the week in the date from the users.birth_date field, I do this:
extract(dow from users.birth_date::timestamp) IN (1,2,3)
how can I convert the date to find out what day of the week the user's birthday is in the current year?
I think you just need to get the birth date in the current year based on the birth date with the year saved in your database.
This should do the trick for you:
select extract(dow from format('%s-%s-%s', extract(year from now()), extract(month from users.birth_date), extract(day from users.birth_date))::date) from users;
I would use make_date() for this:
select birth_date,
make_date(extract(year from current_date)::int,
extract(month from birth_date)::int,
extract(day from birth_date)::int) as this_years_date
from users;
A view that does this for you would probably be helpful if you need this a lot. Then you can use extract(dow from this_years_date) while selecting from the view.
Using a combination of to_char and to_date might do the trick. See the following examples:
WITH users (birth_date) AS (
VALUES (to_date('Mon, 18 Dec 1989','Dy, DD Mon YYYY'))
)
SELECT
birth_date,
to_char(birth_date,'Dy, DD Mon YYYY'),
extract(dow from birth_date) dow_1,
to_date(extract(year from current_date)||
to_char(birth_date,'-mm-dd'),'yyyy-mm-dd'),
to_char((extract(year from current_date)||
to_char(birth_date,'-mm-dd'))::date,'Dy, DD Mon YYYY'),
extract(dow from to_date(extract(year from current_date)||
to_char(birth_date,'-mm-dd'),'yyyy-mm-dd')) dow_2
FROM users;
birth_date | to_char | dow_1 | to_date | to_char | dow_2
------------+------------------+-------+------------+------------------+-------
1989-12-18 | Mon, 18 Dec 1989 | 1 | 2021-12-18 | Sat, 18 Dec 2021 | 6
(1 row)
select EXTRACT(DOW FROM cast(to_date(to_char(now(),'yyyy')||to_char(users.birth_date,'-MM-dd')||' 00:00:00','YYYY-MM-DD HH24:MI:SS') as TIMESTAMP));
1.string contact current year and users.birth_date
2.string convert to date
If you are changing the year fields into current year.
You can calculate the year difference, then add it back to the original date.
SELECT (
TO_DATE('1989-02-24', 'YYYY-MM-DD') + (
INTERVAL '1 year' * (
EXTRACT(YEAR FROM CURRENT_DATE) -
EXTRACT(YEAR FROM TO_DATE('1989-02-24', 'YYYY-MM-DD'))
)
)
)
It is still a date, so you can do anything that you can apply on date.
Such as EXTRACT(dow, [date]);
Use + INTERVAL X * '1 YEAR' can prevent you generateing invalid date in leap years.

How to get month and year from date in SQL

I need help to get month and year from a date which is in yyyymmdd format.
The dates look like this, '20150102', the output should be Jan 2015.
I want the output as on single value, currently using datepart function Im getting output in 2 different columns for month and year
This may help:
SQL Server:
SELECT FORMAT (GETDATE(), 'MMM yyyy') -- Jul 2019
SELECT FORMAT (GETDATE(), 'MMMM yyyy') -- July 2019
SELECT RIGHT(CONVERT(VARCHAR(11), GETDATE(), 106), 8) -- Jul 2019
For more details: https://www.tutorialgateway.org/sql-date-format/
MySQL:
SELECT DATE_FORMAT("20150102", "%M %Y"); -- January 2015
SELECT DATE_FORMAT("20150102", "%b %Y"); -- Jan 2015
SELECT YEAR(date) AS 'year', MONTH(date) AS 'month'
For more details: http://www.sqlines.com/mysql-to-oracle/date_format
MySQL will recognize the date string as a date, so you can use date_format() to get the format you want:
select date_format('20150102', '%b %Y')
there is a list of all differnet sql date formats sql date formats

Extraxcting date using datepart function, but have it returned in text

I'm currently trying to find a way to extract the month from a timestamp field, set up as: yyyy-mm-dd but return the value in either 3 letters (Feb) or the full month (February). It is currently being returned numerically.
Any help would be appreciated!
In PostreSQL, there are several formats to choose from:
select
to_char(current_timestamp, 'MONTH') as "MONTH",
to_char(current_timestamp, 'Month') as "Month",
to_char(current_timestamp, 'MON') as "MON",
to_char(current_timestamp, 'Mon') as "Mon",
to_char(current_timestamp, 'mon') as "mon",
to_char(current_timestamp, 'MM') as "MM";
Result:
MONTH Month MON Mon mon MM
------- ------- ------- ------- ------- -------
JULY July JUL Jul jul 07
datepart() is a SQL Server function. If that is the database you are using, then you can use datename():
select datename(month, getdate())
If its SQL Server:
SELECT FORMAT(GETDATE(), 'MMM')
https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-2017

Displaying date in Month DD,YYYY format

I want to display the date in below format
July 23, 2011
SELECT REG_NO, FORMAT(DOB ,'YYYY/MM/DD')
FROM Students ;
I tried below
SELECT REG_NO, FORMAT(DOB ,'MON DD,YYYY')
FROM Students ;
It seems not working
use to_char
The syntax is to_char( value, [ format_mask ], [ nls_language ] )
SELECT REG_NO, TO_CHAR(DOB ,'MONTH DD,YYYY') FROM Students ;
How about trying this one:
SELECT REG_NO, to_char(DOB, 'FMMonth DD, YYYY') FROM Students;
TO_CHAR()
The correct SQL is
SELECT REG_NO, to_char(DOB, 'MONTH DD, YYYY') FROM Students;
DD: Day Of Month
MONTH: Full Month Name
MM: Numeric Month
MON: Abbreviated Month, ex. Jul
YYYY: 4-DIGIT Year
For more on ORACLE date-format click here