SQL Start Date and End Date of Month


DECLARE @MyDate nvarchar(max);
DECLARE @MyMonth nvarchar(max);
DECLARE @MyYear nvarchar(max);
DECLARE @StartDate nvarchar(max);
DECLARE @StartEnd nvarchar(max);

SET @MyYear = '2012'
SET @MyMonth = 'Nov'

 select @MyDate =
                      CASE @MyMonth 
                           WHEN 'Jan' THEN '1/1/' + @MyYear
                           WHEN 'Feb' THEN '2/1/' + @MyYear
                           WHEN 'Mar' THEN '3/1/' + @MyYear
                           WHEN 'Apr' THEN '4/1/' + @MyYear
                           WHEN 'May' THEN '5/1/' + @MyYear
                           WHEN 'Jun' THEN '6/1/' + @MyYear
                           WHEN 'Jul' THEN '7/1/' + @MyYear
                           WHEN 'Aug' THEN '8/1/' + @MyYear
                           WHEN 'Sep' THEN '9/1/' + @MyYear
                           WHEN 'Oct' THEN '10/1/' + @MyYear
                           WHEN 'Nov' THEN '11/1/' + @MyYear
                           WHEN 'Dec' THEN '12/1/' + @MyYear
                      End

SELECT @StartDate = CONVERT(VARCHAR(25)
                           ,DATEADD(dd,-(DAY(@MyDate)-1),@MyDate)
                           ,101)

SELECT @StartEnd =    CONVERT(VARCHAR(25)
                           ,DATEADD(dd,-(DAY(DATEADD(mm,1,@MyDate)))
                                      ,DATEADD(mm,1,@MyDate))
                           ,101)

SET @StartDate = @StartDate + ' 00:00:00'
SET @StartEnd = @StartEnd + ' 23:59:59'


SELECT @StartDate AS StartDate, '__' AS __ , @StartEnd AS StartEnd
Related Posts Plugin for WordPress, Blogger...