sql server - SQL Need two WHERE clause conditions -


i need conditional where clause or case statement based on table field's value.

this have , it's not working:

select n.id, n.message, n.type notifications n (       n.type = 1        , applicationmask & isnull(@applicationmask,2147483647)<> 0       , cast(getdate() date) between cast(startdate date) ,                                              cast(enddate date)        , not exists(select notificationid                       notificationdelivery nd                       nd.notificationid = n.id                       , nd.userid = @userid)   )  or  (       n.type = 2        , applicationmask & isnull(@applicationmask,2147483647) <> 0       , cast(getdate() date) between cast(startdate date) ,                                             cast(enddate date)        ) 

the or messing up, think. top where clause works itself, returning rows should not return. seems not exist part being ignored. there better way? not strong in sql queries.

try better performance , better readability. assume return result need:

set @applicationmask = isnull(@applicationmask,1)  select n.id, n.message, n.type notifications n type in (1,2)  , applicationmask = 1 , @applicationmask > 0 , dateadd(d, 0, datediff(d, -1, getdate()) > startdate , dateadd(d, 0, datediff(d, 0, getdate())) <= enddate , (type = 2 or not exists(select null                  notificationdelivery nd                  nd.notificationid = n.id                  , nd.userid = @userid)) 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -