1

I have a column in my database with populated with full dates : (0000-00-00) I need to show this date as a month string (ex. January).

I have too choices on my SQL QUERY here:

1 create a case when month is '01' = 'January' (which I believe is the best option since I could then change the string however I want (In Portuguese, with the first letter capital for example). I was not able to do that, I tried substring, but dates don´t like substring statements.

2 just show that date as month:

MONTH(DT_CANCEL)

However, I need to show the string (January) and the above only outputs 01

What is the best option (and how) can I show the full month name of a date in a SQL QUERY?

Using MySQL.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
ROCA
  • 81
  • 8

5 Answers5

1

You can simply use MonthName:

SELECT MONTHNAME('2009-05-18');

This will output May.

Reference here: https://www.w3resource.com/mysql/date-and-time-functions/mysql-monthname-function.php

Gauravsa
  • 6,330
  • 2
  • 21
  • 30
1

In mysql you can use monthname

SELECT MONTHNAME(DT_CANCEL) from your_table;
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1

mysql:

select date_format('2018-12-01','%M'); /* prints December*/

select date_format('2018-12-01','%b'); /* prints Dec*/
Gauravsa
  • 6,330
  • 2
  • 21
  • 30
Luca Lupidi
  • 164
  • 5
0

In Oracle, use TO_CHAR:

select TO_CHAR(SYSDATE, 'Month') FROM dual;

December 
Rob G
  • 592
  • 4
  • 18
0

If you are using MS SQL Server, you don't need to write a Case statement, try below:

SET LANGUAGE Portuguese

SELECT UPPER(LEFT(DATENAME(month, getdate()),1))+LOWER(SUBSTRING(DATENAME(month, 
getdate()),2,LEN(DATENAME(month, getdate())))) 

If you want to reset language for your server, you can see the list of aliases by running the script below:

SELECT * FROM sys.syslanguages
iMajek
  • 153
  • 1
  • 8