从字符串转换日期和/或时间时转换失败了吗?

时间:2017-08-04 15:32:26

标签: sql-server date sql-server-2012

所以,我需要显示当前月份和最后6月份的材料消耗量。我试图在相应月份的列上添加别名,就像“MM / YY”一样。

Example of the columns: 08/17 | 07/17 | 06/17 | ... | 02/17

DECLARE @mes_atual DATE; SET @mes_atual = CAST(FORMAT(GETDATE(),'MM/YY') AS DATE)
    DECLARE @mes1 DATE; SET @mes1 = DATEADD(MM, -1, @mes_atual); SET @mes1 = CAST(@mes1 AS VARCHAR)
    DECLARE @mes2 DATE; SET @mes2 = DATEADD(MM, -2, @mes_atual); SET @mes2 = CAST(@mes2 AS VARCHAR)
    DECLARE @mes3 DATE; SET @mes3 = DATEADD(MM, -3, @mes_atual); SET @mes3 = CAST(@mes3 AS VARCHAR)
    DECLARE @mes4 DATE; SET @mes4 = DATEADD(MM, -4, @mes_atual); SET @mes4 = CAST(@mes4 AS VARCHAR)
    DECLARE @mes5 DATE; SET @mes5 = DATEADD(MM, -5, @mes_atual); SET @mes5 = CAST(@mes5 AS VARCHAR)
    DECLARE @mes6 DATE; SET @mes6 = DATEADD(MM, -6, @mes_atual); SET @mes6 = CAST(@mes6 AS VARCHAR)
    SET @mes_atual = CAST(@mes_atual AS VARCHAR)
    DECLARE @query VARCHAR;
    DECLARE @selects VARCHAR;

    SET @selects = 'QT_CONS_MES_ATUAL AS [' + CAST(@mes_atual AS VARCHAR) + '], QT_CONS_MES_1 AS [' + CAST(@mes1 AS VARCHAR) + '], QT_CONS_MES_2 AS [' + CAST(@mes2 AS VARCHAR) + '], QT_CONS_MES_3 AS [' + CAST(@mes3 AS VARCHAR) + '], QT_CONS_MES_4 AS [' + CAST(@mes4 AS VARCHAR) + '], QT_CONS_MES_5 AS [' + CAST(@mes5 AS VARCHAR) + '], QT_CONS_MES_6 AS [' + CAST(@mes6 AS VARCHAR) + ']'

    SET @query = 'SELECT [VERSAO]
          ,[IN_TP_DISP]
          ,[CD_OWNER]
          ,[DS_OWNER]
          ,[OP_ITEM_PROD]
          ,[DS_PRODUTO]
          ,[CD_MAT_GRUPO]
          ,[CD_MAT_SUBGRUPO]
          ,[CD_MAT_CLASSE]
          ,[CD_MAT_NIVEL4]
          ,[CD_MAT_NIVEL5]
          ,[CD_UNMED_EST]
          ,[CD_DIVISAO]
          ,[IN_CTR_DISP]
          ,[IN_TIPO_REPOS]
          ,[QT_ETQ_MAXIMO]
          ,[QT_ETQ_MINIMO]
          ,[QT_LOTE_COMPRA]
          ,[QT_SALDO_ATUAL]
          ,[VL_SALDO_ATUAL]
          ,[QT_SALDO_RES]
          ,[QT_SALDO_ENC]
          ,[QT_SALDO_DISP]
          ,'+@selects+'
          ,[SQ_MOVTO]
          ,[QT_SALDO_CUR]
          ,[QT_SALDO_BENTO]
          ,[QT_SALDO_BAURU]
          ,[CD_FOR_ITEM]
          ,[VL_CUSTO_ULT_COMPRA]
          ,[CD_PESSOA_FORN]
          ,[NOME_FORN]
          ,[AN_NFE]
          ,[DT_ENTRADA]
          ,[AN_PC]
      FROM [dbo].[VW_SQL_CONSUMO_BI]'

    EXEC(@query)

我正在尝试将某些列的别名设置为dinamic日期,但是当我将日期转换为VARCHAR数据时,构建查询并执行它我得到错误:

Mensagem 241, Nível 16, Estado 1, Linha 3
Conversion failed when converting date and/or time from character string.

Ps:错误信息是葡萄牙语,但我希望你们能帮助我。

3 个答案:

答案 0 :(得分:2)

问题是你的变量在最顶端。一个月和一年如何成为约会?你需要一天。摆脱那些愚蠢的演员/格式的东西,只需将你的日期设置为getdate。

DECLARE @mes_atual DATE; SET @mes_atual = GETDATE()

select @mes_atual

答案 1 :(得分:0)

不要投放日期。只需使用格式即可获得所需内容。

请注意yylowercase

Select FORMAT(@mes_atual,'MM/yy')

DECLARE @mes_atual DATE; SET @mes_atual = GETDATE()
Select @mes_atual
Select FORMAT(@mes_atual,'MM/yy')

将渲染

2017-08-04

08/17

答案 2 :(得分:0)

如果没有一天,您将无法创建有效日期 因此,将GetDate放入您的变量中,并使用Format仅显示年份和月份

DECLARE @mes_atual DATE 
SET @mes_atual = GetDate()

select @mes_atual as mes_atual,
       FORMAT(@mes_atual,'MM/yy') AS date

返回

mes_atual   date    
2017-08-04  08/17