If I have the name of the month, how can I have the first and last day of that month in SQL?
I have this query to returns the month names:
DECLARE @StartDate DATETIME,
@EndDate DATETIME;
SELECT @StartDate = '20110501'
,@EndDate = '20110801';
SELECT DATENAME(MONTH, DATEADD(MONTH, x.number, @StartDate)) AS MonthName
FROM master.dbo.spt_values x
WHERE x.type = 'P'
AND x.number <= DATEDIFF(MONTH, @StartDate, @EndDate)
Result:
Now, how can i get the first and last day of that months? changing the query.
3条答案
按热度按时间wn9m85ua1#
If you want a more general and simple solution:
Gives the result:
yftpprvb2#
Try this :-
Result :-
Result obtained taking the help from this query
jdg4fx2g3#
This code gives you the first date of Current Month:
If you know the Month's number then:
Same goes for Month's end date :