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 2008 Forums
 Transact-SQL (2008)
 date issue

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-04-26 : 11:21:54
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-26 : 11:46:26
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

52326 Posts

Posted - 2012-04-26 : 12:40:16
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-26 : 12:49:11
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
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-04-26 : 13:08:46
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
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2012-04-26 : 13:15:42
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
   

- Advertisement -