Question

My table month stores numbers (1,2...,12) and I need to convert them in names (January, February...). How can I do this in my query?

SELECT
    id_user,
    name,
    month,
    year
FROM
    DW_RM_Log
...

PS: mySQL

Was it helpful?

Solution

In you can do

select date_format(str_to_date(columnName, '%m'), '%M') from tableName;

Note, that the %m and %M are case sensitive. Here's how you can format dates (and how str_to_date() works: MySQL Date and Time functions

OTHER TIPS

In Sql Server, you can do like this:

DECLARE @Mth smallint
SET @Mth = 11
SELECT DateName(mm,DATEADD(mm,@Mth,-1)) as [MonthName]

IN your case:

SELECT
    id_user,
    name,
    DateName(mm,DATEADD(mm,month,-1)) as [MonthName],
    year
FROM
    DW_RM_Log

this will return you November.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top