1

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

1

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

thank you Khairul but this didnt work, I need the output for a date field which is in yyyymmdd format to give me the monthname and year
See second line, it's out is as your requirement
Actually, I guess the tag earlier was not specifying that this requirement is for SQL Server. Is that the reason this solution is not going through?
thanks Khairul, this works, however how would i use order by in such a case such that I get the ouput in chronological order. I want to see count of sales by this month year field, so it should give out output as Jan 2015 - 1000, Feb 2015 - 2000 and so on. Currently its giving Jan 2015 -1000, Jan 2016 -5000. Does that make sense?
I guess what im asking is how to write the order by clause here
|
0

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') 

1 Comment

thanks Gordon, its giving me an error saying, DATE_FORMAT' is not a recognized built-in function name. Also, my requirement is not just for this particular field but for an entire column.
0

there is a list of all differnet sql date formats sql date formats

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.