first day of the same month
I have a date parameter (@rptMonth) that is selected by the user from a datepicker calendar. The date must be the first day of the month. No matter what the user selects I'd like to turn that into mm/01/yyyy. For example- I need the first day of the month. So if the user selects 06/22/2010, I need开发者_开发技巧 to turn that into 06/01/2010. So in my query it would be something like WHERE YEAR_MONTH = DATEADD("m",datediff("m","1900-01-01",@RptMonth),"1900-01-01"),"mm/dd/yyyy" but when I try this I get incorrect syntax near ','. Don't know if this will even work.
Update:
select dateadd(month, datediff(month, 0, getdate()), 0)
Older:
Try this:
declare @arbitraryDate datetime;
set @arbitraryDate = getdate();
set @arbitraryDate = dateadd(dd, datediff(dd, 0, @arbitraryDate), 0) --strip time
select dateadd(dd, -day(@arbitraryDate)+1,@arbitraryDate) --strip days
Or this:
select cast(convert(varchar(6), getdate(), 112) + '01' as datetime)
This should work too:
SELECT CAST(CAST(YEAR(@pInputDate) AS VARCHAR(4)) +
CAST(MONTH(@pInputDate) AS VARCHAR(2)) + '01' AS DATETIME)
精彩评论