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.
| 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] owhere o.ORDERITEM_ID in (IF @selectdate=nullBEGINselect ORDERITEM_ID from [dbo].[OrderItem] where UPDATEDDATE is not nullEND IF @selectdate is not NULLBEGINdeclare @tempdate datetimeselect @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] owhere o.ORDERITEM_ID in CASE WHEN @selectdate is not NULL THEN (SELECT ORDERITEM_ID from [dbo].[OrderItem] WHERE UPDATEDDATE is not nullELSE SELECT ORDERITEM_ID from [dbo].[OrderItem] WHERE CAST(FLOOR( CAST( CREATEDDATE AS FLOAT ) )AS DATETIME)AND UPDATEDDATE is not nullEND)[/code]I think? Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-28 : 03:03:06
|
| [code]select * from [dbo].[OrderItem] owhere 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] |
 |
|
|
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 |
 |
|
|
|
|
|
|
|