Select records based on GetDate()

I assume the reason why the select doesn't find any records is because it's looking at the time also?
update Top (3000) Order set CreateDate = GetDate()
where CreateDate = '2007-11-30 22:48:32.063'
 
select * from Order where  CreateDate = GetDate()

Open in new window

LVL 1
dba123Asked:
Who is Participating?
 
mdefalcoConnect With a Mentor Commented:
Yeah, I think since the value of GetDate has changed already.

How about;
Dim strDate
strDate = GetDate()
 
update Top (3000) Order set CreateDate = strDate
where CreateDate = '2007-11-30 22:48:32.063'
 
select * from Order where  CreateDate = strDate

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>I assume the reason why the select doesn't find any records is because it's looking at the time also?
yes, the second getdate() will actually return a new value, only slightly different than the update..

here is the corrected code:
DELCARE @d DATETIME 
SET @d = getdate()
update Top (3000) [Order] 
   set CreateDate = @d
where CreateDate = convert(datetime, '2007-11-30 22:48:32.063', 120)
 
   select * 
     from [Order] 
    where CreateDate = @d

Open in new window

0
 
ursangelCommented:
yeah, thats true. Getdate() will always provide you with instant date and time values. each instant the datetime value will be different from teh previous since the time stmp is attached with the value.

Rest you can use angelll's query. that is save the getdate() value to a variable and then try updating the table value with it and then retrieve using the same value.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.