SQL语句将其转换为1到1月1日,2月到2月,依此类推

时间:2014-01-28 10:54:27

标签: sql select monthcalendar str-to-date

如下图所示,月份现在为1和2.如何将其转换为1到1月1日,2月到2月,依此类推。下面是我的sql。

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

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

5 个答案:

答案 0 :(得分:2)

如上所述in this other answer

  

您可以使用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')) 

FIDDLE

答案 1 :(得分:0)

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

 SELECT DATENAME(month, 1)
 FROM fborders

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

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

您可以编写SQL Query,如:

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

希望有所帮助......

答案 4 :(得分:0)

声明@intMonth int SET @ intMonth = 9

选择SUBSTRING(&#39; 1月2月5月5月5月6月6月6月12日,(3 * 4) - 3,3) 选择SUBSTRING(&#39; 1月2月5月5月5月6月6月6月12日,(@intMonth * 4) - 3,3)