在我的情况下,如何解决“从字符串转换日期和/或时间时转换失败”的问题

时间:2019-03-28 07:08:27

标签: sql sql-server datetime dynamic-sql

我正在尝试使用日期进行动态查询,我不知道怎么了

销售表中时间字段的实际数据

Department  Time        ExtendedPrice
-------------------------------------
Toys        2018-11-01      1205.11
Toys        2018-12-02      1230.27
Baby Care   2018-12-01      6797.95
Sports      2019-01-01      365.8
Sports      2019-01-01      2993.44
Sports      2018-12-02      580.03
Baby Care   2019-01-02      1117.92
Baby Care   2019-01-02      390.75
soap        2018-11-01      3275.25
Baby Care   2018-12-01      367.56

以上日期为实际数据的示例格式

DECLARE @cols AS Varchar(MAX)
select @cols = STUFF((SELECT ',' + convert(varchar(MAX),Time,105)+''
                      FROM #Sales
                      GROUP by Time
                      ORDER by Time

               FOR XML PATH(''), TYPE
               ).value('.', 'VARCHAR(MAX)') 
               ,1,1,'')

输出

select @cols
//output
01-11-2018,02-11-2018,01-12-2018,02-12-2018,01-01-2019,02-01-2019

@query

DECLARE @query AS Varchar(MAX)
SET @query = 'SELECT 
                    ' + @cols + '
              FROM 
              (
                   SELECT 
                       convert(varchar(MAX),Time,105) Time,
                       ExtendedPrice
                   FROM 
                       #Sales
              ) x
              pivot 
              (
                  sum(ExtendedPrice)
                  for Time in (' + @cols + ')
              ) p '
PRINT @query

显示继错误之后的错误

  

从字符串转换日期和/或时间时转换失败。

更新

我也尝试过

DECLARE @cols AS Varchar(MAX)
SELECT @cols = STUFF((SELECT ',[' + convert(varchar(MAX),Time,105)+']'
                FROM #Sales
                GROUP by Time
                ORDER by Time
        FOR XML PATH(''), TYPE
        ).value('.', 'VARCHAR(MAX)') 
    ,1,1,'')

    select @cols

//output
[01-11-2018],[02-11-2018],[01-12-2018],[02-12-2018],[01-01-2019],[02-01-2019]

1 个答案:

答案 0 :(得分:2)

您应将日期引用为

CREATE TABLE T
    ([Department] varchar(9), [Time] datetime, [ExtendedPrice] int)
;

INSERT INTO T
    ([Department], [Time], [ExtendedPrice])
VALUES
    ('Toys', '2018-11-01 00:00:00', 1205.11),
    ('Toys', '2018-12-02 00:00:00', 1230.27),
    ('Baby Care', '2018-12-01 00:00:00', 6797.95),
    ('Sports', '2019-01-01 00:00:00', 365.8),
    ('Sports', '2019-01-01 00:00:00', 2993.44),
    ('Sports', '2018-12-02 00:00:00', 580.03),
    ('Baby Care', '2019-01-02 00:00:00', 1117.92),
    ('Baby Care', '2019-01-02 00:00:00', 390.75),
    ('soap', '2018-11-01 00:00:00', 3275.25),
    ('Baby Care', '2018-12-01 00:00:00', 367.56)
;

DECLARE @cols AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT ',' + QUOTENAME(CONVERT(VARCHAR(10), Time, 105))
                      FROM T
                      GROUP by Time
                      ORDER by Time
               FOR XML PATH(''), TYPE
               ).value('.', 'VARCHAR(MAX)') 
               ,1,1,'');

DECLARE @query AS NVARCHAR(MAX)=
             N'SELECT 
                    ' + @cols + '
              FROM 
              (
                   SELECT 
                       convert(varchar(10),Time,105) Time,
                       ExtendedPrice
                   FROM 
                       T
              ) x
              pivot 
              (
                  sum(ExtendedPrice)
                  for Time in (' + @cols + ')
              ) p ';

EXEC sp_executesql @query;

返回:

+------------+------------+------------+------------+------------+
| 01-11-2018 | 01-12-2018 | 02-12-2018 | 01-01-2019 | 02-01-2019 |
+------------+------------+------------+------------+------------+
|       4480 |       7164 |       1810 |       3358 |       1507 |
+------------+------------+------------+------------+------------+

Demo