September 28, 2015 at 1:46 am
Hi Team,
Help to get the Sql Query e.g when execute on 8/15/2015 get the records from 8/1/2015 to 8/15/2015, if run on 9/20/2015 get the results from 9/1/2015 to 9/20/2015
Same for every month ?
September 28, 2015 at 1:59 am
smer (9/28/2015)
Hi Team,Help to get the Sql Query e.g when execute on 8/15/2015 get the records from 8/1/2015 to 8/15/2015, if run on 9/20/2015 get the results from 9/1/2015 to 9/20/2015
Same for every month ?
If you have a look at this link[/url] it will give you some very useful date calculations. There's one there for the start of a month.
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
September 28, 2015 at 2:08 am
smer (9/28/2015)
Hi Team,Help to get the Sql Query e.g when execute on 8/15/2015 get the records from 8/1/2015 to 8/15/2015, if run on 9/20/2015 get the results from 9/1/2015 to 9/20/2015
Same for every month ?
I suggest you use
WHERE YourDateColumn >= @StartDate
AND YourDateColumn < @DayAfterEndDate
@StartDate for beginning of the month would be
DATEADD(Month, DATEDIFF(Month, 0, GetDate()), 0)
and @DayAfterEndDate for "tomorrow" (i.e. the day after your end point)
DATEADD(Day, DATEDIFF(Day, 0, GetDate()) + 1, 0
September 28, 2015 at 7:34 am
Kristen-173977 (9/28/2015)
smer (9/28/2015)
Hi Team,Help to get the Sql Query e.g when execute on 8/15/2015 get the records from 8/1/2015 to 8/15/2015, if run on 9/20/2015 get the results from 9/1/2015 to 9/20/2015
Same for every month ?
I suggest you use
WHERE YourDateColumn >= @StartDate
AND YourDateColumn < @DayAfterEndDate
@StartDate for beginning of the month would be
DATEADD(Month, DATEDIFF(Month, 0, GetDate()), 0)
and @DayAfterEndDate for "tomorrow" (i.e. the day after your end point)
DATEADD(Day, DATEDIFF(Day, 0, GetDate()) + 1, 0
+1000
--Jeff Moden
Change is inevitable... Change for the better is not.
September 28, 2015 at 9:16 am
Jeff Moden (9/28/2015)
+1000
My new high score :w00t:
FWIW a +1000 pet-hate of mine is using short mnemonics for the datepart parameters e.g. DATEADD(dd, ... instead of DATEADD(Day, ...
I have no idea, without checking, whether "M" is Month, Minute, Millisecond, Microsecond and which of W and WW is Week and which is WeekDay, is N nanosecond, or something else (its something else ...) and I think it leads to coding errors, e.g. during maintenance, as someone else looking at the code "assumes" that X is the correct mnemonic for the coding situation. Using the mnemonics seems to be very popular though ...
Whereas I reckon I can reliably get the full datepart names right-first-time-every-time:
year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, nanosecond
September 28, 2015 at 9:23 am
Kristen-173977 (9/28/2015)
smer (9/28/2015)
Hi Team,Help to get the Sql Query e.g when execute on 8/15/2015 get the records from 8/1/2015 to 8/15/2015, if run on 9/20/2015 get the results from 9/1/2015 to 9/20/2015
Same for every month ?
I suggest you use
WHERE YourDateColumn >= @StartDate
AND YourDateColumn < @DayAfterEndDate
@StartDate for beginning of the month would be
DATEADD(Month, DATEDIFF(Month, 0, GetDate()), 0)
and @DayAfterEndDate for "tomorrow" (i.e. the day after your end point)
DATEADD(Day, DATEDIFF(Day, 0, GetDate()) + 1, 0
Why:
WHERE YourDateColumn >= @StartDate
AND YourDateColumn < @DayAfterEndDate
and not:
WHERE YourDateColumn >= @StartDate
AND YourDateColumn <= GETDATE()
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
September 28, 2015 at 9:36 am
Alvin Ramard (9/28/2015)
Why:
WHERE YourDateColumn >= @StartDate
AND YourDateColumn < @DayAfterEndDate
and not:
WHERE YourDateColumn >= @StartDate
AND YourDateColumn <= GETDATE()
If it is a DATE datatype column that's fine. If it is a DATETIME datatype then there is still time between GetDate() and midnight tonight.
You cannot say
AND YourDateColumn <= '20150928 23:59:59.999'
because SQL will round 23:59:59.999 up to midnight
We have a mix of DATE and DATETIME datatypes in our code and my preference is to always use
AND YourDateColumn < @DayAfterEndDate
so that I don't have to worry about whether the column being tested is DATE or DATETIME - or might CHANGE! between the two datatypes in the future.
September 28, 2015 at 9:47 am
Kristen-173977 (9/28/2015)
Alvin Ramard (9/28/2015)
Why:
WHERE YourDateColumn >= @StartDate
AND YourDateColumn < @DayAfterEndDate
and not:
WHERE YourDateColumn >= @StartDate
AND YourDateColumn <= GETDATE()
If it is a DATE datatype column that's fine. If it is a DATETIME datatype then there is still time between GetDate() and midnight tonight.
You cannot say
AND YourDateColumn <= '20150928 23:59:59.999'
because SQL will round 23:59:59.999 up to midnight
We have a mix of DATE and DATETIME datatypes in our code and my preference is to always use
AND YourDateColumn < @DayAfterEndDate
so that I don't have to worry about whether the column being tested is DATE or DATETIME - or might CHANGE! between the two datatypes in the future.
I realized after I posted my comment that I had forgotten about the time before midnight.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
September 28, 2015 at 10:02 am
Alvin Ramard (9/28/2015)
Kristen-173977 (9/28/2015)
Alvin Ramard (9/28/2015)
Why:
WHERE YourDateColumn >= @StartDate
AND YourDateColumn < @DayAfterEndDate
and not:
WHERE YourDateColumn >= @StartDate
AND YourDateColumn <= GETDATE()
If it is a DATE datatype column that's fine. If it is a DATETIME datatype then there is still time between GetDate() and midnight tonight.
You cannot say
AND YourDateColumn <= '20150928 23:59:59.999'
because SQL will round 23:59:59.999 up to midnight
We have a mix of DATE and DATETIME datatypes in our code and my preference is to always use
AND YourDateColumn < @DayAfterEndDate
so that I don't have to worry about whether the column being tested is DATE or DATETIME - or might CHANGE! between the two datatypes in the future.
I realized after I posted my comment that I had forgotten about the time before midnight.
Sometimes we need to leave those rows out because they're still in the future. It depends on what's needed and the design of the application (some table shouldn't have future records).
September 28, 2015 at 10:08 am
Luis Cazares (9/28/2015)
Alvin Ramard (9/28/2015)
Kristen-173977 (9/28/2015)
Alvin Ramard (9/28/2015)
Why:
WHERE YourDateColumn >= @StartDate
AND YourDateColumn < @DayAfterEndDate
and not:
WHERE YourDateColumn >= @StartDate
AND YourDateColumn <= GETDATE()
If it is a DATE datatype column that's fine. If it is a DATETIME datatype then there is still time between GetDate() and midnight tonight.
You cannot say
AND YourDateColumn <= '20150928 23:59:59.999'
because SQL will round 23:59:59.999 up to midnight
We have a mix of DATE and DATETIME datatypes in our code and my preference is to always use
AND YourDateColumn < @DayAfterEndDate
so that I don't have to worry about whether the column being tested is DATE or DATETIME - or might CHANGE! between the two datatypes in the future.
I realized after I posted my comment that I had forgotten about the time before midnight.
Sometimes we need to leave those rows out because they're still in the future. It depends on what's needed and the design of the application (some table shouldn't have future records).
I thought about that too. If the date column was an order date, then there should not be any future dates/times, but it's an expected ship date, then the opposite would be true.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
September 29, 2015 at 1:44 am
In these situations I adopt what I consider to be a Defensive Programming stance. Of course we can all argue that Black-is-White on this situation π It may be that the system does allow future dates and we definitely don't want a date/time that is not yet due.
Assuming there should be no future dates I could say
SET @EndDate = GetDate()
SELECT ...
...
WHERE MyDateColumn >= @StartDate
AND MyDateColumn <= @EndDate
to ensure I don't accidentally get any future-dated rows.
But if they shouldn't be there I wouldn't program for that eventuality, I would treat that as a bug that had to be solved as a separate issue, when it was detected / reported.
In fact for this situation
WHERE MyDateColumn >= @StartDate
would be enough.
However, assuming the first scenario, I would program against the @EndDate potentially becoming something other than "now"
SET @EndDate = DATEADD(Day, DATEDIFF(Day, 0, GetDate()), 0) -- Date-after-end-point
SELECT ...
...
WHERE MyDateColumn >= @StartDate
AND MyDateColumn < @EndDate
so that @EndDate could become any suitable cutoff date in the future.
October 1, 2015 at 1:59 am
SELECT CONVERT(DATE, DATEADD(DAY, - DAY(GETDATE()) + 1, GETDATE()))
October 1, 2015 at 2:05 am
WHERE UpdateDate >= CONVERT(DATE, DATEADD(DAY, - DAY(GETDATE()) + 1, GETDATE()))
October 1, 2015 at 8:54 pm
Kristen-173977 (9/28/2015)
Jeff Moden (9/28/2015)
+1000My new high score :w00t:
FWIW a +1000 pet-hate of mine is using short mnemonics for the datepart parameters e.g. DATEADD(dd, ... instead of DATEADD(Day, ...
I have no idea, without checking, whether "M" is Month, Minute, Millisecond, Microsecond and which of W and WW is Week and which is WeekDay, is N nanosecond, or something else (its something else ...) and I think it leads to coding errors, e.g. during maintenance, as someone else looking at the code "assumes" that X is the correct mnemonic for the coding situation. Using the mnemonics seems to be very popular though ...
Whereas I reckon I can reliably get the full datepart names right-first-time-every-time:
year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, nanosecond
Heh... we're even! A pet hate of mine is people that spell the date-parts out especially when there are many nested values. π I've never had any code errors because of it either. Seems like a lot of unnecessary clutter to me. That, notwithstanding, I won't change someone else's code just because of my dislike for it nor will I try to convince anyone that they're wrong in their choice to use the long versions because they're not. It's a style preference of mine that has worked very well for me and I also understand why other's might prefer otherwise.
Just as an interesting point, I find it odd that people that don't like the 2 character date parts will still use "0" to represent 1900-01-01 and "-1" in next day calculations. π
--Jeff Moden
Change is inevitable... Change for the better is not.
October 2, 2015 at 1:15 am
Jeff Moden (10/1/2015)
Kristen-173977 (9/28/2015)
Jeff Moden (9/28/2015)
+1000My new high score :w00t:
FWIW a +1000 pet-hate of mine is using short mnemonics for the datepart parameters e.g. DATEADD(dd, ... instead of DATEADD(Day, ...
I have no idea, without checking, whether "M" is Month, Minute, Millisecond, Microsecond and which of W and WW is Week and which is WeekDay, is N nanosecond, or something else (its something else ...) and I think it leads to coding errors, e.g. during maintenance, as someone else looking at the code "assumes" that X is the correct mnemonic for the coding situation. Using the mnemonics seems to be very popular though ...
Whereas I reckon I can reliably get the full datepart names right-first-time-every-time:
year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, nanosecond
Heh... we're even! A pet hate of mine is people that spell the date-parts out especially when there are many nested values. π I've never had any code errors because of it either. Seems like a lot of unnecessary clutter to me. That, notwithstanding, I won't change someone else's code just because of my dislike for it nor will I try to convince anyone that they're wrong in their choice to use the long versions because they're not. It's a style preference of mine that has worked very well for me and I also understand why other's might prefer otherwise.
Just as an interesting point, I find it odd that people that don't like the 2 character date parts will still use "0" to represent 1900-01-01 and "-1" in next day calculations. π
I've just found it easier to use the full name of the part as I tend to forget a few of the abbreviated forms or I confuse one for something else. More the former than the latter.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply