Question

As you seen the image below, the month is now 1 and 2. How can I convert it to 1 to Jan, 2 to Feb and so on. Below are my sql.

Help will be appreciate. Thanks in advance! :)

enter image description here

SELECT DISTINCT fbmenuname, 
                Sum(quantity)                        AS ordercount, 
                Month(Str_to_date(date, '%d/%m/%Y')) AS month 
FROM   fb.fborders 
       INNER JOIN fb.fbmenu 
               ON fborders.fbmenuid = fbmenu.fbmenuid 
   INNER JOIN fb.fbrestaurant 
               ON fbrestaurant.fbrestaurantid = fbmenu.fbrestaurantid 
WHERE  fborders.status = 'completed' 
       AND ( Year(Str_to_date(date, '%d/%m/%Y')) = '2013' 
             AND fbrestaurant.fbrestaurantid = 'R0001' ) 
GROUP  BY fbmenuname, 
          Month(Str_to_date(date, '%d/%m/%Y')) 
Was it helpful?

Solution

As stated in this other answer:

You can use STR_TO_DATE() to convert the number to a date, and then back with MONTHNAME()

SELECT MONTHNAME(STR_TO_DATE(6, '%m'));

+---------------------------------+
| MONTHNAME(STR_TO_DATE(6, '%m')) |
+---------------------------------+
| June                            |
+---------------------------------+

Use Left Function to Trim the unwanted character

So try this

SELECT DISTINCT fbmenuname, 
                Sum(quantity) 
                AS ordercount, 
                Month(Str_to_date(date, '%d/%m/%Y')) 
                AS month, 
                Monthname(Str_to_date(Month(Str_to_date(date, '%d/%m/%Y')), '%m' 
                          )) AS MonthName 
FROM   fb.fborders 
       INNER JOIN fb.fbmenu 
               ON fborders.fbmenuid = fbmenu.fbmenuid 
       INNER JOIN fb.fbrestaurant 
               ON fbrestaurant.fbrestaurantid = fbmenu.fbrestaurantid 
WHERE  fborders.status = 'completed' 
       AND ( Year(Str_to_date(date, '%d/%m/%Y')) = '2013' 
             AND fbrestaurant.fbrestaurantid = 'R0001' ) 
GROUP  BY fbmenuname, 
          Month(Str_to_date(date, '%d/%m/%Y')) 

FIDDLE

OTHER TIPS

Assuming column Month to be int, you can try following query

UPDATE fborders
SET month=month+1
where month<12

This query will not update month DEC as month=13 doesn't exists.

You can write SQL Query like:

SELECT DISTINCT fbmenuname, 
                Sum(quantity) 
                AS ordercount, 
                Datename(month, Dateadd(month, Month( 
                                Str_to_date(date, '%d/%m/%Y')), 0) - 1) AS 
                month 
FROM   fb.fborders 
       INNER JOIN fb.fbmenu 
               ON fborders.fbmenuid = fbmenu.fbmenuid 
       INNER JOIN fb.fbrestaurant 
               ON fbrestaurant.fbrestaurantid = fbmenu.fbrestaurantid 
WHERE  fborders.status = 'completed' 
       AND ( Year(Str_to_date(date, '%d/%m/%Y')) = '2013' 
             AND fbrestaurant.fbrestaurantid = 'R0001' ) 
GROUP  BY fbmenuname, 
          Month(Str_to_date(date, '%d/%m/%Y')) 

Hope it helps..

To assume you are using MSSQL, then the query for the same would be:

 SELECT DATENAME(month, 1)
 FROM fborders

and further rest of your query, you can use it in either Update clause or Select clause.

declare @intMonth int SET @intMonth=9

SELECT SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (3 * 4) - 3, 3) SELECT SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (@intMonth * 4) - 3, 3)

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