March 8, 2020 at 6:02 pm
how can the meaning of this Where clause be explained in a human language?
this query <does what?> ...does it really make sence the way it is?
delete from FROM dbo.huge_queue_table
WHERE date_created < DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)
Thank you.
Likes to play Chess
March 8, 2020 at 6:26 pm
looks clear enough for some one with your level of experience.
so what seems to be your question after you have run "select DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)" to see its result?
March 8, 2020 at 6:40 pm
this part confuses me:
DATEDIFF(wk,0,GETDATE()),
and this was a one time run query in prod, not scheduled. Deleting old rows... i actually need to make an SP out of it with params and schedule. So this part would be a good candidate to parameter but why DATEDIFF(wk,0,GETDATE()) ?
this is the number of weeks since the year 2000.
Likes to play Chess
March 8, 2020 at 6:56 pm
are you sure? did you run it? and does it really look like the number of weeks that 20 years would have?
March 8, 2020 at 7:13 pm
yes, you right. I just assumed that. it is not what it means. What does parameter value = 0 mean in Datediff function in such case?
Likes to play Chess
March 8, 2020 at 7:17 pm
"0" (zero) on these cases means the default value for datetime - which you can determine by a simple change to that code (or by reading the manuals)
March 8, 2020 at 7:21 pm
it appears that select DATEDIFF(wk,0,GETDATE())
returns 6271 , which . divided by 52 - 120 years.
so it is weeks since the year 1900.
Likes to play Chess
March 8, 2020 at 7:33 pm
Thank you! I cannot believe I either forgot or did not know about default value for 'zer0' or for ' ' for datetime datatype...
I ran this to confirm.
create table wdi(datum datetime null)
insert into wdi(datum) values ('')
insert into wdi(datum) values (0)
insert into wdi(datum) values (NULL)
select * from wdi
drop table wdi
Likes to play Chess
March 8, 2020 at 7:39 pm
but then why to juggle it ti like that
This whole thing returns date of midnight following the today's date.
Like what are we saying? Delete all records that are dated earlier than midnight tomorow?
then why go to such complexity as this?
select DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)
Likes to play Chess
March 8, 2020 at 7:42 pm
its doing more than that - it just happens that today (Sunday) happens to be a week boundary.
try it with multiple dates (consecutives, spawning 2 -3 weeks) instead of getdate to see its variation
March 9, 2020 at 1:19 am
Check this old blog: https://www.sqlservercentral.com/blogs/some-common-date-routines
March 9, 2020 at 1:32 am
ok. now i fully see how it is working. But since we are going to run this job on Sundays only, so we are fine with the code then. I don't know why such a simple piece of code made me so confused. Thanks for your help !
Likes to play Chess
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply