Use CASE in WHERE clause - Data Parameters Sql Server -
i'm trying verify example, if date null pick date field. below query:
select * orderpublish op inner join advertising ad on ad.idorderpublish = op.idorderpublish inner join client cli on cli.idclient = op.idclient left join advertisinginserted aii on ad.idadvertisinginserted = aii.idadvertisinginserted left join typeproduct tp on aii.idtypeproduct = tp.idtypeproduct inner join publication pub on pub.idadvertisinginserted = aii.idadvertisinginserted tp.idtypeproduct in (5,7) , case when pub.dtini null op.dtinc >= isnull('08/18/2013', op.dtinc) else pub.dtini >= isnull('08/18/2013', pub.dtini) end , case pub.dtfinal when null op.dtinc <= isnull('08/23/2013', op.dtinc) else pub.dtfinal <= isnull('08/23/2013', pub.dtfinal) end
you can coalesce()
in case, if want use case
in where
criteria can so. move matching criteria outside case
statement, need case
statement return values compared, not comparison itself. requires doubling each case
statement:
select * orderpublish op inner join advertising ad on ad.idorderpublish = op.idorderpublish inner join client cli on cli.idclient = op.idclient left join advertisinginserted aii on ad.idadvertisinginserted = aii.idadvertisinginserted left join typeproduct tp on aii.idtypeproduct = tp.idtypeproduct inner join publication pub on pub.idadvertisinginserted = aii.idadvertisinginserted tp.idtypeproduct in (5,7) , case when pub.dtini null op.dtinc else pub.dtini end >= case when pub.dtini null isnull('08/18/2013', op.dtinc) else isnull('08/18/2013', pub.dtini) end , case pub.dtfinal when null op.dtinc else pub.dtfinal end <= case pub.dtfinal when null isnull('08/23/2013', op.dtinc) else isnull('08/23/2013', pub.dtfinal) end
that's how can use case
statement in where
criteria, use and ()
, or ()
criteria along coalesce()
without case
also, isnull()
statements backwards, '08/18/2013'
never null
, field won't evaluated, mean: isnull(op.dtinc,'08/23/2013')
Comments
Post a Comment