Link to home
Create AccountLog in
Avatar of forsters
forstersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

MS SQL - GETDATE() WHERE DATEPART(m, GETDATE())

Hi Experts,

Quick one, is there a simple way to get the dates of the current month and or week using GETDATE() and datepart as suggested by my title?

 So to be clear I don't want the week number or month number but the dates that make up the current week or month...is this possible does anyone know??

Many thanks in advance
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>but the dates that make up the current week or month
What do you mean by this ... Monday, Tuesday, Wednesday?

On most servers, 1=Sunday and 7=Saturday.  Check that yours hasn't been altered.
SELECT DATEPART(DW, GETDATE())
If you mean just return the day, i.e. 7-12-2013 returns 12..
SELECT DATEPART(d, GETDATE())
Avatar of forsters

ASKER

Hi Thanks for your comments, no what I mean is GETDATE() i.e. 12/07/2013 - thats the format I want but I want to retrieve the array of dates that comprise this week e.g. 08/07/2013, 09/07/2013, 10/07/2013, 11/07/2013, 12/07/2013, 13/07/2013, 14/07/2013....is there a way to do that do you know?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
@angelIII: VERY IMPRESSIVE!!! I would vote for your solution!
Wow! thank you both, amazing, I thought I was on a no-hoper with this but that is just brilliant, thanks so much.
Please note that my solution needs to be corrected.
Change:  WHERE DATEADD(day, 1, EndValue) < @endDate
to this:   WHERE EndValue <= @endDate
Oh? But it worked...
I found that the original query I posted showed only dates up to 2 days before the end of the period (in that case 29 rather than 31).
Oh yeh I see, I'm missing a day - now fixed perfect