Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Find out date different

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2005-12-26 : 10:55:56
In a order table, there is a OrderDate column in which stored order date as varchar data. How to code a select statement to select all orders which order date big than 15 days compare to today date? Like:
"select * from Order where OrderDate ..."

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2005-12-26 : 11:41:34
Check for the functions
Convert()
GetDate()
DateDiff()

and ur query would be like:
select * from Order where Datediff (Getdate() - CONVERT(datetime, OrderDate,3)) > 15
The number in red is to be selected depending on the data in ur char column.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-26 : 23:20:57
>>select * from Order where Datediff (Getdate() - CONVERT(datetime, OrderDate,3)) > 15

That should be

select * from Order where Datediff (Day,Getdate(),CONVERT(datetime, OrderDate)) > 15

Why did you use varchar datatype to store date. Use Proper DateTime datatype

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2005-12-26 : 23:22:34
select * from Order where Datediff (d,Getdate() ,CONVERT(datetime, OrderDate,<dateformat>)) > 15
use 101 for mm/dd/yyyy
103 for dd/mm/yyyy
see BOL for more info on convert
Go to Top of Page
   

- Advertisement -