SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 date issue
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

arkiboys
Flowing Fount of Yak Knowledge

1341 Posts

Posted - 04/26/2012 :  11:21:54  Show Profile  Reply with Quote
There is something wrong with these queries I think.
Can you see what I am doing wroong please?
Thanks
Let's say today's date is 26 April 2012
Query number 1 should return 1 row whereas it does not
Query number 2 should NOT return any row whereas it does

1-
SELECT Field1, Field2, Field3 FROM tblMain WHERE Field1 = 'xxx' AND ( '26/04/2012' < ('03/05/2012'))

2-
SELECT Field1, Field2, Field3 FROM tblMain WHERE Field1 = 'xxx' AND ( '26/04/2012' > ('03/05/2012'))

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/26/2012 :  11:46:26  Show Profile  Reply with Quote
It is doing a string comparison because SQL doesn't know that you want it to be converted to dates. Cast at least one side to date/datetime. Also, preferably, use unambiguous date formatting (YYYYMMDD).
AND (CAST('20120426' AS DATETIME) < '20120503')
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 04/26/2012 :  12:40:16  Show Profile  Reply with Quote
i dont really understand the relevance of those constant date value comparison
Do they come from a variable in actual query?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/26/2012 :  12:49:11  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

i dont really understand the relevance of those constant date value comparison
Do they come from a variable in actual query?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Might be experimenting to see if the logic evaluates to TRUE/FALSE as proof of concept.
Go to Top of Page

arkiboys
Flowing Fount of Yak Knowledge

1341 Posts

Posted - 04/26/2012 :  13:08:46  Show Profile  Reply with Quote
Can you see what is wrong with this query please?
It says incorrect syntx next ) which is pointing to the last )
Thanks
set @criteriaOR = replace(@criteriaOR, 'tradedate', 'convert(datetime, ' + char(39) + convert(varchar(11), getdate(), 103) + char(39) + ')' + )
Go to Top of Page

yosiasz
Flowing Fount of Yak Knowledge

USA
1608 Posts

Posted - 04/26/2012 :  13:15:42  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
remove the + just before the last ) if that last ) goes with REPLACE

<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000