| Author |
Topic |
|
Rembrantai
Starting Member
1 Post |
Posted - 2007-08-20 : 02:23:29
|
| Hi im new to SQL. Ive looked through your forums but most answers seem to complicated.I'm trying to get todays date for a SELECT statement.My code is:List all the records bought today. SELECT recordNo, recordName FROM Record r, Properties p WHERE r.recordNo = p.recordNo AND purchaseDate = '20/08/07'; I'm certain writing '20/08/07' is not right and it should be something like: getDate.Can anyone help? thankyou |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-20 : 04:00:23
|
| [code]SELECT recordNo, recordName FROM Record r, Properties p WHERE r.recordNo = p.recordNo AND purchaseDate>=dateadd(day,datediff(day,0,GETDATE()),0)[/code]provided purchaseDate is of Datetime datatypeMadhivananFailing to plan is Planning to fail |
 |
|
|
mageshks
Yak Posting Veteran
59 Posts |
Posted - 2007-08-21 : 12:12:53
|
| SELECT recordNo, recordName FROM Record r, Properties p WHERE r.recordNo = p.recordNo AND purchaseDate = cast ( cast (getdate() as varchar(12)) as datetime) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-22 : 01:14:48
|
quote: Originally posted by mageshks SELECT recordNo, recordName FROM Record r, Properties p WHERE r.recordNo = p.recordNo AND purchaseDate = cast ( cast (getdate() as varchar(12)) as datetime)
You dont need to double cast. See my replyMadhivananFailing to plan is Planning to fail |
 |
|
|
mohit_sme
Starting Member
13 Posts |
Posted - 2007-08-22 : 13:15:52
|
| HiThis will also help in standard SQL configuration.SELECT recordNo, recordName FROM Record r, Properties p WHERE r.recordNo = p.recordNo AND purchaseDate = CONVERT(VARCHAR(10), GETDATE(), 121); ThanksMohit Nayyarhttp://mohitnayyar.blogspot.com |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-08-22 : 13:23:54
|
quote: Originally posted by mohit_sme HiThis will also help in standard SQL configuration.SELECT recordNo, recordName FROM Record r, Properties p WHERE r.recordNo = p.recordNo AND purchaseDate = CONVERT(VARCHAR(10), GETDATE(), 121); ThanksMohit Nayyarhttp://mohitnayyar.blogspot.com
MohitIn your code you are comparing a date against a string.Follow Madhivannan's advise which keeps the right side of the equation still in datetime format.Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
mohit_sme
Starting Member
13 Posts |
Posted - 2007-08-26 : 00:56:35
|
| DinakarYes, you are right, but as per the first thread, he just passed a date string, so technically he needs something (a date string) instead of passing a fixed string.Thats why I thought of writing this. And even this is something standard to pass dates.ThanksMohit Nayyarhttp://mohitnayyar.blogspot.com |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-27 : 01:58:59
|
quote: Originally posted by mohit_sme HiThis will also help in standard SQL configuration.SELECT recordNo, recordName FROM Record r, Properties p WHERE r.recordNo = p.recordNo AND purchaseDate = CONVERT(VARCHAR(10), GETDATE(), 121); ThanksMohit Nayyarhttp://mohitnayyar.blogspot.com
If purchaseDate has both Date and Time, your solution wont workAs I told, you dont need to convert dates to StringsMadhivananFailing to plan is Planning to fail |
 |
|
|
|