2

如下图所示,现在月份是 1 和 2。如何将其转换为 1 到 Jan,2 到 Feb 等等。下面是我的sql。

帮助将不胜感激。提前致谢!:)

在此处输入图像描述

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

5 回答 5

2

另一个答案所述:

您可以使用STR_TO_DATE()将数字转换为日期,然后返回MONTHNAME()

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

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

使用Left功能修剪不需要的字符

所以试试这个

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

小提琴

于 2014-01-28T10:58:55.477 回答
0

假设您正在使用MSSQL,那么相同的查询将是:

 SELECT DATENAME(month, 1)
 FROM fborders

以及查询的其余部分,您可以在Update子句或Select子句中使用它。

于 2014-01-28T11:01:11.620 回答
0

假设列Month是int,您可以尝试以下查询

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

此查询不会更新月份 DEC,因为 month=13 不存在。

于 2014-01-28T11:01:17.023 回答
0

您可以编写如下 SQL 查询:

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

希望能帮助到你..

于 2014-01-28T11:15:06.140 回答
0

声明@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)

于 2017-08-27T08:07:31.620 回答