我有这样的陈述
SELECT @startdate = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), -7) -- Monday of previous week
SELECT @enddate = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), -1) -- Sunday of previous week
我希望将@startdate和@enddate转换为以下格式的数字'yyyymmdd'
答案 0 :(得分:3)
SELECT CONVERT(nvarchar(8), @StartDate, 112)
将2014-03-31
转换为20140331
答案 1 :(得分:1)
最简单的方法是使用year()
,month()
和day()
:
select @startdate = year(getdate())*10000 + month(getdate()) * 100 + day(getdate())
答案 2 :(得分:0)
这对你有用:
SELECT CAST(DATEPART(yyyy,@startdate) AS VARCHAR(4)) +
RIGHT('0' + CAST(DATEPART(mm,@startdate) AS VARCHAR(2)),2) +
RIGHT('0' + CAST(DATEPART(dd,@startdate) AS VARCHAR(2)),2)
但这更好:
SELECT CONVERT(nvarchar(8), @StartDate, 112)
答案 3 :(得分:0)
使用CONVERT
declare @enddate smalldatetime
SELECT @enddate = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), -1)
select convert(varchar(30), @enddate, 112)