dbTalk Databases Forums  

Need help writing a stored procedure

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Need help writing a stored procedure in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
JJ297
 
Posts: n/a

Default Need help writing a stored procedure - 02-01-2008 , 07:09 PM






How would I write a stored procedure to get * where duedate is less
than and not equal to today's date. Is this right?

select * from library
where duedate < != getdate()

Thanks

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Need help writing a stored procedure - 02-01-2008 , 10:37 PM






You would write it like this:

select * from library
where duedate < getdate()

However, since GETDATE() returns the current date and time, there are
chances you will get duedate values for today, those between midnight and
the current time. The correct way is to reset the current time portion to
midnight. You will end up with something like this:

SELECT <columns>
FROM Library
WHERE duedate < DATEADD(day, DATEDIFF(day, '20010101', CURRENT_TIMESTAMP),
'20010101')

In the above formula the time portion is trimmed by simple arithmetic:
calculating the difference in days between a preset date (Jan-1-2001) and
today, and then adding back the number of days to the same date. Since the
DATEDIFF returns the number of days only, the time portion is discarded.

And CURRENT_TIMESTAMP is just the ANSI SQL equivalent to GETDATE().

HTH,

Plamen Ratchev
http://www.SQLStudio.com


Reply With Quote
  #3  
Old   
Jack Vamvas
 
Posts: n/a

Default Re: Need help writing a stored procedure - 02-04-2008 , 08:54 AM



select * from library
where duedate < getdate()



--

Jack Vamvas
___________________________________
Search IT jobs from multiple sources- http://www.ITjobfeed.com




"JJ297" <nc297 (AT) yahoo (DOT) com> wrote

Quote:
How would I write a stored procedure to get * where duedate is less
than and not equal to today's date. Is this right?

select * from library
where duedate < != getdate()

Thanks



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.