2012-08-13 90 views
0

我需要獲取在查詢中使用當前月份第一天的tsql日期時間。如何寫這個tsql日期時間代碼更簡潔和/或優雅

我想出了

declare @StartDate datetime 
set @StartDate = cast(MONTH(getdate()) as char(2))+ '/1/' 
     + cast(Year(getdate()) as char(4)) 
select @StartDate 

,但不知道一個更好的方式來做到這一點。

對此提出建議?

回答

4

如何:

SELECT DATEADD(month, DATEDIFF(month, 0, getdate()), 0) AS FirstOfMonth 
+1

通過15秒:)毆打。我會讓我的立場,只是爲了表明這**是**你是如何做到的。 +1 – 2012-08-13 20:05:37

+1

@MikaelEriksson我想我很幸運地擊敗你。 :) – Taryn 2012-08-13 20:06:05

3
select dateadd(month, datediff(month, 0, getdate()), 0) 
相關問題