user3794422
user3794422

Reputation: 331

SQL Date_Format Month Number to Month Name

I have an issue in regards to trying to run a sql statement that returns the values of a column called month(of which I have defined as a varchar type, but only has integer values 1-12) as the associated month name. So, for example, the query would return a value of 1 as january. The issue I have is I am trying to use date_format

     select date_format(month,'%M')from db.table name

but the values return as null. I was informed that the month values have to be a 'date' type in order for date_format to work. However, the values in this column 'month' are simply integers. So I run into the issue of not being able to assign the date type to the month columns because they're just integers and not correct format for dates? How could I take these single integers and return the month then?

Upvotes: 1

Views: 1473

Answers (1)

AbhishekTaneja
AbhishekTaneja

Reputation: 111

Syntax

DATE_FORMAT(date,format)

Requires date as first param

Check out MySQL date function here:

http://www.w3schools.com/sql/sql_dates.asp

For this you can use this,

SELECT col as MonthNumber, MONTHNAME(STR_TO_DATE(col, '%m')) as MonthName FROM table_name WHERE col <= 12

Upvotes: 2

Related Questions