Search code examples
sqlsql-serversql-server-2014

Creating a function to be used in the where clause - SQL Server


Every day I have to run lots of adhoc queries and all of these queries has the same condition in the where clause.

I was wondering if there is a way to create a function or an alias or something to make it easy and fast to write the queries.

Usually the queries are something like:

select * 
from tbl_1
where customer in ('ctm1','ctm2','ctmetc')
and isnull(status,'c') in (select status from tbl_status)

What I was wondering is if is possible to develop something like:

select * 
from tbl_1
where customer in ('ctm1','ctm2','ctmetc')
and function.status = 'C'

in which function.status = 'C' reffers to "isnull(status,'c') in (select status from tbl_status)"

I did a lot of research but nothing had helped...

Thanks a lot.


Solution

  • There is another way to look at this. The stated problem is that you have to type the same string over and over again.

    I suggest looking at a way to automate the typing. There are utilities out there to solve this problem. The one I use is called AutoHotKey. There might be others out there as well.