Come posso convertire @dateb:
SET @dateb = dateadd(month, datediff(month, 0, getdate()) - 3, 0)
che restituisce 2014-04-04come data un numero intero di201404
Grazie
Come posso convertire @dateb:
SET @dateb = dateadd(month, datediff(month, 0, getdate()) - 3, 0)
che restituisce 2014-04-04come data un numero intero di201404
Grazie
Risposte:
Sulla versione 2012 o successiva è possibile utilizzare la formatfunzione per ottenere solo anno e mese, quindi eseguirne il cast come int.
Nelle versioni precedenti al 2012 è possibile eseguire la formattazione con la convertfunzione, quindi eseguire il cast come int.
declare @dateb datetime
set @dateb = getdate()
select cast(format(@dateb,'yyyyMM') as int) --2012 or higher
select cast(convert(varchar(6),@dateb,112) as int) -- all versions
Forse un po 'più ordinato:
SELECT YEAR(@dateb)*100 + MONTH(@dateb);
Questo potrebbe fare il trucco per te?
set @dateb = cast(convert(varchar, dateadd(month, datediff(month, 0, getdate()) - 3, 0), 112) as int)
Ah scusa, ho dimenticato, avrai bisogno anche di una sottostringa. Così è:
select cast(substring(convert(varchar, dateadd(month, datediff(month, 0, getdate()) - 3, 0), 112), 0, 7) as int)
Un altro metodo:
DECLARE @PeriodToCalculate_DATE [DATE] ='2016-02-29'
SELECT
CAST(DATEPART(YYYY,@PeriodToCalculate_DATE) AS [CHAR](4))
+ RIGHT('0' + CAST(DATEPART(M,@PeriodToCalculate_DATE) AS [VARCHAR](2)),2)
+ RIGHT('0' + CAST(DATEPART(D,@PeriodToCalculate_DATE) AS [VARCHAR](2)),2);
Dà: 20160229
FORMAT(), in generale . YMMV.