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
 General SQL Server Forums
 New to SQL Server Programming
 Incorrect syntax errors with IF's inside WHERE ??

Author  Topic 

phrankbooth
Posting Yak Master

162 Posts

Posted - 2010-01-27 : 19:00:14
In the code below, I'm getting Incorrect syntax errors on the first IF, the second FROM and the last parenthesis. I cannot see why. Is it that i can;t have embedded IF's. If that's the case, how would this be written legally? Thanks in advance!!


select * from [dbo].[OrderItem] o
where o.ORDERITEM_ID in
(
IF @selectdate=null
BEGIN
select ORDERITEM_ID from [dbo].[OrderItem] where UPDATEDDATE is not null
END

IF @selectdate is not NULL
BEGIN
declare @tempdate datetime
select @tempdate = CAST(FLOOR( CAST( @selectdate AS FLOAT ) )AS DATETIME)

select ORDERITEM_ID from [dbo].[OrderItem]
where @tempdate = CAST(FLOOR( CAST( CREATEDDATE AS FLOAT ) )AS DATETIME)
AND UPDATEDDATE is not null
END
)


--PhB

X002548
Not Just a Number

15586 Posts

Posted - 2010-01-27 : 19:42:10
[code]
select * from [dbo].[OrderItem] o
where o.ORDERITEM_ID in

CASE WHEN @selectdate is not NULL THEN
(SELECT ORDERITEM_ID from [dbo].[OrderItem] WHERE UPDATEDDATE is not null
ELSE
SELECT ORDERITEM_ID from [dbo].[OrderItem]
WHERE CAST(FLOOR( CAST( CREATEDDATE AS FLOAT ) )AS DATETIME)
AND UPDATEDDATE is not null
END)

[/code]


I think?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-28 : 03:03:06
[code]
select * from [dbo].[OrderItem] o
where o.ORDERITEM_ID in
(SELECT ORDERITEM_ID from [dbo].[OrderItem] WHERE UPDATEDDATE is not null
AND ((CREATEDDATE >DATEADD(dd,DATEDIFF(dd,0,@selectdate),0)
AND CREATEDDATE < DATEADD(dd,DATEDIFF(dd,0,@selectdate)+1,0))
OR @selectdate IS NULL)
[/code]
Go to Top of Page

phrankbooth
Posting Yak Master

162 Posts

Posted - 2010-01-28 : 09:29:07
That was close, just need to clean it up. Thanks!!

This is it:

CASE WHEN @selectdate is NULL THEN
(SELECT ORDERITEM_ID from [dbo].[OrderItem] WHERE UPDATEDDATE is not null)
ELSE
(SELECT ORDERITEM_ID from [dbo].[OrderItem]
WHERE CAST(FLOOR( CAST( @selectdate AS FLOAT ) )AS DATETIME) = CAST(FLOOR( CAST( LOGDATE AS FLOAT ) )AS DATETIME)
AND UPDATEDDATE is not null)

--PhB
Go to Top of Page
   

- Advertisement -