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 2012 Forums
 Transact-SQL (2012)
 Query criteria based on custom select data

Author  Topic 

WebKill
Starting Member

32 Posts

Posted - 2014-06-25 : 15:25:26
This may be an easy answer, but I'm not sure how to phrase it so I am having trouble finding the answer.

I have a select statement, lets take the following as an example

select case when field1 + isnull(field2,'') <> field3 then field1 end as compare
from mytable
where compare is not null

So basically what I am doing is only showing the data for field 1 if the concatenation of field 1 and 2 is not the same as field 3, otherwise it will show null.

What I want to do is use my custom named field and only display when its not null. Is this possible? I know how I have it written above doesn't work, but I was wondering if there is some way because it would make my query a lot cleaner.

Thanks!


Joe

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-06-25 : 15:31:29
select * from
(select *, case when field1 + isnull(field2,'') <> field3 then field1 end as compare from mytable) t
where compare is not null

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

WebKill
Starting Member

32 Posts

Posted - 2014-06-25 : 15:43:33
arg, of course, now I just feel silly. Thanks for reminding me of that!

Joe
Go to Top of Page
   

- Advertisement -