How to get month and year from date in SQL - 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

Related

Concatenate Year and Month

I have 2 date fields, surv_year (int) and surv_month (text).
Example outputs:
surv_year surv_month
2022 January
2022 August
Is there anyway to concatenate the 2 and change the month from a text to a number and have my final output as YYYYMM with my example above showing 202201 and 202208?
You can convert the two values to a proper date using the to_date() function:
select to_date(concat(surv_month, ' ', surv_year), 'Month yyyy')
from the_table
will return a date on the first day of that month.
This date can be formatted using the to_char() function to the format that you want:
select to_char(to_date(concat(surv_month, ' ', surv_year), 'Month yyyy'), 'yyyymm')
from the_table

Sort by text (nvarchar)

Not sure how to sort with column 'DateText' column like this:
April 2020
March 2001
May 2020
June 2021
December 2021
Expect result
December 2021
June 2021
March 2001
May 2020
April 2020
I tried this but no luck
ORDER BY
CASE WHEN 1 = ISNUMERIC(TextDate)
THEN CAST(TextDate AS INT)
END
Happily, your format can be converted to a date:
order by convert(date, textdate)
You actually want desc for your example.
To build a little on the good answer and advice given here, once the data is in a date datatype there are useful functions you can use to get specific information out of your date, such as the day of the week or the month name and so much more. These can then also be used to order your dates in many different ways should you require it!
Below I have made a little demo of the DATEPART and DATENAME functions, there are far more than this too.
https://www.w3schools.com/sql/func_sqlserver_datename.asp
This was written and tested in T-SQL for MS SQL Server
DECLARE #Dates TABLE(textdate nvarchar(15))
INSERT INTO #Dates
VALUES ('April 2020'),
('March 2001'),
('May 2020'),
('June 2021'),
('December 2021')
SELECT *,
CONVERT(date, textdate) AS datetype
INTO #dates
FROM #Dates
ORDER BY CONVERT(date, textdate) desc
SELECT *,
DATEPART(DD,datetype) AS DayNum,
DATEPART(MM,datetype) AS MonthNum,
DATEPART(YYYY,datetype) AS YearNum,
DATENAME(WEEKDAY, datetype) AS DayName,
DATENAME(MONTH, datetype) AS MonthName
FROM #dates
ORDER BY datetype desc
DROP TABLE #dates

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

Extract Month from date as number

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;

Convert Date in SQL

Select ReceiveDate,SentDate
From Customers
ReceiveDate SentDate
2012-11-29 19:17:00 2007-08-28 10:48:00
I want to change such format is : Month Day, Year (Example : Nov 29, 2012)
How can I do?
Thanks
I don't think there is a certain mysql function that does that.
But if you want to show the date on a site with the format (jan 24, 1998) then you can use php.
You make three queries one to take the day, one for the month and one for the year.
Then you make switch case for the month to associate it with its name (e.g. 03 -> Mar).
On SQL Server you can do using CONVERT and STUFF;
SELECT STUFF(CONVERT(VARCHAR(11), GETDATE(),100),7,1,', ')
--Results: Dec 10, 2012
Assuming SQL server you want some combination of DATEPART and DATENAME like this:
SELECT
DATENAME(month, '2012-11-29 19:17:00') + ' ' +
CAST(DATEPART(dd, '2012-11-29 19:17:00') AS VARCHAR)+ ', ' +
CAST(DATEPART(yyyy, '2012-11-29 19:17:00') AS VARCHAR);
You did not specify what RDBMS, but if you are using SQL Server, you can use:
select
convert(varchar(6), receivedate, 100)+', '+cast(year(receivedate) as char(4)) ReceiveDate,
convert(varchar(6), sentdate, 100)+', '+cast(year(sentdate) as char(4)) SentDate
from customers
See SQL Fiddle with Demo
Result:
| RECEIVEDATE | SENTDATE |
-------------------------------
| Nov 29, 2012 | Aug 28, 2007 |
If you are using MySQL, then you can use the Date_Format() function:
select
date_format(receivedate, '%b %e, %Y') ReceiveDate,
date_format(sentdate, '%b %e, %Y') SentDate
from customers
See SQL Fiddle with Demo