CASE syntax inside a WHERE clause on MSSQL - sql

I have the following SQL syntax on MSSQL
SELECT
id,
firstName,
lastName
FROM
Person
WHERE
((CASE WHEN #Filter = 'firstName' THEN #Value END) = firstName ) or
((CASE WHEN #Filter = 'lastName' THEN #Value END) = lastName )
It's working but I don't know if there is a better and more efficient way to do this.
Thanks in advanceName

In this example you don't really need CASE at all. AND and OR will run faster:
SELECT
id,
firstName,
lastName
FROM
Person
WHERE
(#Filter = 'firstName' AND #Value = firstName) OR
(#Filter = 'lastName' AND #Value = lastName)

This may compile, but it seems like something's missing. #Filter apparently represents the field you're filtering on, and one would expect to see a value to check against the field, like so
SELECT
id,
firstName,
lastName
FROM
Person
WHERE #Value =
CASE WHEN #Filter = 'firstName' THEN firstName
WHEN #Filter = 'lastName' THEN lastName
END
If this is your intent, then only one CASE expression is needed, regardless of the number of possible filter fields.
Either way, the CASE statement is going to be very efficient. SQL Server will see that the WHEN expressions are comparing scalar values and optimize accordingly (in other words, the value of #Filter does not need to be re-evaluated for every row).

Related

How do I show all records when one field is null in SQL?

I have my code
SELECT ID, NAME, ADDRESS FROM CUSTOMER WHERE NAME = #NAME
but the #NAME is an optional field for searching.
If the #NAME is blank or NULL, it should show all records.
How do I approach this?
Thanks!
There are some ways to do it the easy way. I would do something like this.
You can use the Function ISNULL to handle blank or NULL.
SELECT ID, NAME, ADDRESS
FROM CUSTOMER
WHERE ISNULL(#NAME,'') = '' OR NAME = #NAME
With this statement you will get all records if #NAME is blank or NULL.
If #NAME is not blank and not NULL, it will return all records that match #NAME.
If you have more optional fields you can use or. But dont forget the parenthesis.
SELECT ID, NAME, ADDRESS
FROM CUSTOMER
WHERE
(ISNULL(#NAME,'') = '' OR NAME = #NAME)
OR
(ISNULL(#ADDRESS,'') = '' OR ADDRESS = #ADDRESS)
It will not select all records if #NAME is null or blank because if the WHERE condition is not true, nothing will be selected. (The only way something is selected with your current query is if this WHERE condition is true. You will have to use a different query.
You are best off with a straight IF statement:
IF (#NAME IS NULL)
SELECT ID, NAME, ADDRESS
FROM CUSTOMER
WHERE NAME = #NAME;
ELSE
SELECT ID, NAME, ADDRESS
FROM CUSTOMER;

LEN of value in the variable in TSQL

I have a variable #ColumnnName in T-SQL user defined function that basically contains the column Name.
For example :
declare #ColumnnName= 'firstName'
which means I have a column with Name firstName in my table and have this column name stored in variable named #ColumnnName.
I have a query like this:
Insert into #Temp([Row], VarLength) select Rowid, LEN(#ColumnnName) from Patients
where
/
*rest of code
*/
In above query, LEN(#ColumnnName) will return 9 which is the length of firstName but not the value contained in the column firstName.
How do I get length of the value contained the column firstName and not the length of string firstName itself.
Edit:
Complete query is like this :
Insert into #Temp([Row], Counts, VarLength) select Rowid, #counter, LEN(#ColumnnName) from Patients where
(case when #ColumnnName = 'firstname' then firstname
when #ColumnnName = 'middlename' then middlename
when #ColumnnName = 'lastname' then lastname
when #ColumnnName = 'State' then [State]
when #ColumnnName = 'City' then City
when #ColumnnName = 'StreetName' then StreetName
when #ColumnnName = 'StreetType' then StreetType end) LIKE SUBSTRING(#parameterFromUser, 1, #counter) + '%'
This might be a solution to your problem:
INSERT INTO #Temp([Row], VarLength)
SELECT Rowid, CASE WHEN #ColumnnName = 'FirstName' THEN LEN(FirstName)
WHEN #ColumnnName = 'LastName' THEN LEN(LastName)
... etc
END AS len
FROM Patients
... etc
If you want to use the above CASE expression more than once in your query then you can wrap it inside a CTE:
;WITH CTE AS (
SELECT Rowid, CASE WHEN #ColumnnName = 'FirstName' THEN LEN(FirstName)
WHEN #ColumnnName = 'LastName' THEN LEN(LastName)
... etc
END AS len
FROM Patients
)
INSERT INTO #Temp([Row], VarLength)
SELECT Rowid, len
FROM CTE
You can use the EXECUTE command to run a dynamic SQL.
Say you have a string var, called #myvar with the name of a column. You can then build up a complete SQL statement (something like 'select '+#myvar+' from tableName;') and then use the EXECUTE command, like this simple example (from which you can build up your solution easily):
BEGIN
DECLARE #temp NVARCHAR;
SET #temp = 'getdate()';
EXECUTE ('select '+#temp+';');
END;
Hope this helps.

SQL: Conditional AND in where

I'm trying to create a stored procedure that allows parameters to be omitted, but ANDed if they are provided:
CREATE PROCEDURE
MyProcedure
#LastName Varchar(30) = NULL,
#FirstName Varchar(30) = NULL,
#SSN INT = NULL
AS
SELECT LastName, FirstName, RIGHT(SSN,4) as SSN
FROM Employees
WHERE
(
(LastName like '%' + ISNULL(#LastName, '') + '%')
AND
(FirstName like '%' + ISNULL(#FirstName, '') + '%')
)
AND
(SSN = #SSN)
I only want to do the AND if there's an #SSN provided. Or is there some other way to do this?
If an SSN is provided, all records are returned by the LastName/FirstName part of the query. If just a lastname/firstname is provided, the AND makes it so no records are returned since the SSN won't validate.
Ideas?
Additional Clarification
Assume a basic recordset:
First Last SSN
Mike Smith 123456789
Tom Jones 987654321
IF we ALTER the Procedure above so it doesn't include this chunk:
AND
(SSN = #SSN)
then everything works great, provided we're passed a FirstName or LastName. But I want to be able to include SSN, if one is provided.
If I change the AND to an OR, then I get a bad result set since the FirstName/LastName portion of the query evalute to:
WHERE (LastName like '%%' and FirstName like '%%')
(which will, of course, return all records)
If I can't conditionally abort the AND, I'm hoping for something like:
AND
(SSN like ISNULL(#SSN, '%'))
:-)
Change:
AND (SSN = #SSN)
to:
AND (SSN = #SSN or #SSN is null)
If SSN is never null, you could also do:
AND SSN = ISNULL(#SSN, SSN)
You can also try:
(ISNULL(SSN, #SSN) = #SSN)
You could put both versions of the statement in an if block.
AS
IF #SSN IS NULL
/*regular statement*/
ELSE
/*Statement with #SSN*/
or
AND
SSN = ISNULL(#SSN, SSN)
I have never used the 2nd option but I know it exists.
HERE's THE BIBLE on dynamic search parameters in SQL Server. You should write it like this
SELECT LastName, FirstName, RIGHT(SSN,4) as SSN
FROM Employees
WHERE (NULLIF(#LastName,'') IS NULL OR LastName LIKE '%' + #LastName + '%')
AND (NULLIF(#FirstName,'') IS NULL OR FirstName LIKE '%' + #FirstName + '%')
AND (#SSN is null or SSN = #SSN)
-- or if not provided means blank, then
-- (#SSN = '' or SSN = #SSN)
OPTION (RECOMPILE)
BTW, there's no such thing as a short circuit boolean in SQL Server. Yes it does stop at the first conclusion, but there's no guarantee the condition on the left is processed before the right. e.g. this code is NOT SAFE. I'm using 'ABC.123' but that could just as well be a column.
WHERE ISNUMERIC('ABC.123') = 1 OR CAST('ABC.123' AS INT) > 10
To illustrate on the AND (#SSN is null or SSN = #SSN) clause, for when #SSN is not provided, i.e. NULL
AND (#SSN is null or SSN = NULL)
=> AND (NULL IS NULL or SSN = NULL)
=> AND (TRUE or SSN = NULL)
=> AND (TRUE)
-- In other words, this filter is "dissolved" and appears as if it never existed.
-- (in the scheme of a series of AND conditions)
CREATE PROCEDURE
MyProcedure
#LastName Varchar(30) = NULL,
#FirstName Varchar(30) = NULL,
#SSN INT = -1
AS
SELECT LastName, FirstName, RIGHT(SSN,4) as SSN
FROM Employees
WHERE
(
(LastName like '%' + ISNULL(#LastName, '') + '%')
AND
(FirstName like '%' + ISNULL(#FirstName, '') + '%')
)
AND
(SSN = #SSN or #SSN = -1)
Just give #SSN a default value and you can use the AND all the time and worry about it.

Help with SQL Server query condition

I have the following query. but it gives me error in the in clause.
declare #lastName varchar(20)
set #lastName = 'Davis'
select *
from Table1
where Date >= '2013-01-09'
and lastname in(
case
when #lastName = 'DAvis' THEN #lastName
else 'Brown','Hudson' end)
Something like this:
select *
from Table1
where
Date >= '2013-01-09' and
(
(#lastName = 'Davis' and lastname = #lastName) or
(#lastName <> 'Davis' and lastname in ('Brown','Hudson'))
)
Interesting. I think in this case specifically it is the "'Brown','Hudson'" that is causing the issue. You would probably be best not using a case statement at all.
The problem is I don't know of a way to return multiple values in a single case so you have to do like Alex said and use simple boolean logic. You can use a case statement in your where clause however, just not to return multiple values in a single case.
If you want to filter results by three names, use following query:
declare #lastName varchar(20)
set #lastName = 'Davis'
select * from Table1
where Date >= '2013-01-09'
and lastname in(
'DAvis' ,'Brown','Hudson')

How do I create a stored procedure that will optionally search columns?

I'm working on an application for work that is going to query our employee database. The end users want the ability to search based on the standard name/department criteria, but they also want the flexibility to query for all people with the first name of "James" that works in the Health Department. The one thing I want to avoid is to simply have the stored procedure take a list of parameters and generate a SQL statement to execute, since that would open doors to SQL injection at an internal level.
Can this be done?
While the COALESCE trick is neat, my preferred method is:
CREATE PROCEDURE ps_Customers_SELECT_NameCityCountry
#Cus_Name varchar(30) = NULL
,#Cus_City varchar(30) = NULL
,#Cus_Country varchar(30) = NULL
,#Dept_ID int = NULL
,#Dept_ID_partial varchar(10) = NULL
AS
SELECT Cus_Name
,Cus_City
,Cus_Country
,Dept_ID
FROM Customers
WHERE (#Cus_Name IS NULL OR Cus_Name LIKE '%' + #Cus_Name + '%')
AND (#Cus_City IS NULL OR Cus_City LIKE '%' + #Cus_City + '%')
AND (#Cus_Country IS NULL OR Cus_Country LIKE '%' + #Cus_Country + '%')
AND (#Dept_ID IS NULL OR Dept_ID = #DeptID)
AND (#Dept_ID_partial IS NULL OR CONVERT(varchar, Dept_ID) LIKE '%' + #Dept_ID_partial + '%')
These kind of SPs can easily be code generated (and re-generated for table-changes).
You have a few options for handling numbers - depending if you want exact semantics or search semantics.
The most efficient way to implement this type of search is with a stored procedure. The statement shown here creates a procedure that accepts the required parameters. When a parameter value is not supplied it is set to NULL.
CREATE PROCEDURE ps_Customers_SELECT_NameCityCountry
#Cus_Name varchar(30) = NULL,
#Cus_City varchar(30) = NULL,
#Cus_Country varchar(30) =NULL
AS
SELECT Cus_Name,
Cus_City,
Cus_Country
FROM Customers
WHERE Cus_Name = COALESCE(#Cus_Name,Cus_Name) AND
Cus_City = COALESCE(#Cus_City,Cus_City) AND
Cus_Country = COALESCE(#Cus_Country,Cus_Country)
Taken from this page: http://www.sqlteam.com/article/implementing-a-dynamic-where-clause
I've done it before. It works well.
Erland Sommarskog's article Dynamic Search Conditions in T-SQL is a good reference on how to do this. Erland presents a number of strategies on how to do this without using dynamic SQL (just plain IF blocks, OR, COALESCE, etc) and even lists out the performance characteristics of each technique.
In case you have to bite the bullet and go through the Dynamic SQL path, you should also read Erland's Curse and Blessings of Dynamic SQL where he gives out some tips on how to properly write dynamic SQLs
It can be done, but usually these kitchen-sink procedures result in some poor query plans.
Having said all that, here is the tactic most commonly used for "optional" parameters. The normal approach is to treat NULL as "ommitted".
SELECT
E.EmployeeID,
E.LastName,
E.FirstName
WHERE
E.FirstName = COALESCE(#FirstName, E.FirstName) AND
E.LastName = COALESCE(#LastName, E.LastName) AND
E.DepartmentID = COALESCE(#DepartmentID, E.DepartmentID)
EDIT:
A far better approach would be parameterized queries.
Here is a blog post from one of the world's foremost authorities in this domain, Frans Bouma from LLBLGen Pro fame:
Stored Procedures vs. Dynamic Queries
Using the COALESCE method has a problem in that if your column has a NULL value, passing in a NULL search condition (meaning ignore the search condition) will not return the row in many databases.
For example, try the following code on SQL Server 2000:
CREATE TABLE dbo.Test_Coalesce (
my_id INT NOT NULL IDENTITY,
my_string VARCHAR(20) NULL )
GO
INSERT INTO dbo.Test_Coalesce (my_string) VALUES (NULL)
INSERT INTO dbo.Test_Coalesce (my_string) VALUES ('t')
INSERT INTO dbo.Test_Coalesce (my_string) VALUES ('x')
INSERT INTO dbo.Test_Coalesce (my_string) VALUES (NULL)
GO
DECLARE #my_string VARCHAR(20)
SET #my_string = NULL
SELECT * FROM dbo.Test_Coalesce WHERE my_string = COALESCE(#my_string, my_string)
GO
You will only get back two rows because in the rows where the column my_string is NULL you are effective getting:
my_string = COALESCE(#my_string, my_string) =>
my_string = COALESCE(NULL, my_string) =>
my_string = my_string =>
NULL = NULL
But of course, NULL does not equal NULL.
I try to stick with:
SELECT
my_id,
my_string
FROM
dbo.Test_Coalesce
WHERE
(#my_string IS NULL OR my_string = #my_string)
Of course, you can adjust that to use wild cards or whatever else you want to do.
Copying this from my blog post:
USE [AdventureWorks]
GO
CREATE PROCEDURE USP_GET_Contacts_DynSearch
(
-- Optional Filters for Dynamic Search
#ContactID INT = NULL,
#FirstName NVARCHAR(50) = NULL,
#LastName NVARCHAR(50) = NULL,
#EmailAddress NVARCHAR(50) = NULL,
#EmailPromotion INT = NULL,
#Phone NVARCHAR(25) = NULL
)
AS
BEGIN
SET NOCOUNT ON
DECLARE
#lContactID INT,
#lFirstName NVARCHAR(50),
#lLastName NVARCHAR(50),
#lEmailAddress NVARCHAR(50),
#lEmailPromotion INT,
#lPhone NVARCHAR(25)
SET #lContactID = #ContactID
SET #lFirstName = LTRIM(RTRIM(#FirstName))
SET #lLastName = LTRIM(RTRIM(#LastName))
SET #lEmailAddress = LTRIM(RTRIM(#EmailAddress))
SET #lEmailPromotion = #EmailPromotion
SET #lPhone = LTRIM(RTRIM(#Phone))
SELECT
ContactID,
Title,
FirstName,
MiddleName,
LastName,
Suffix,
EmailAddress,
EmailPromotion,
Phone
FROM [Person].[Contact]
WHERE
(#lContactID IS NULL OR ContactID = #lContactID)
AND (#lFirstName IS NULL OR FirstName LIKE '%' + #lFirstName + '%')
AND (#lLastName IS NULL OR LastName LIKE '%' + #lLastName + '%')
AND (#lEmailAddress IS NULL OR EmailAddress LIKE '%' + #lEmailAddress + '%')
AND (#lEmailPromotion IS NULL OR EmailPromotion = #lEmailPromotion)
AND (#lPhone IS NULL OR Phone = #lPhone)
ORDER BY ContactID
END
GO
We can use Generic #Search Parameter and pass any value to it for searching.
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: --
-- Create date:
-- Description: --
-- =============================================
CREATE PROCEDURE [dbo].[usp_StudentList]
#PageNumber INT = 1, -- Paging parameter
#PageSize INT = 10,-- Paging parameter
#Search VARCHAR(MAX) = NULL, --Generic Search Parameter
#OrderBy VARCHAR(MAX) = 'FirstName', --Default Column Name 'FirstName' for records ordering
#SortDir VARCHAR(MAX) = 'asc' --Default ordering 'asc' for records ordering
AS
BEGIN
SET NOCOUNT ON;
--Query required for paging, this query used to show total records
SELECT COUNT(StudentId) AS RecordsTotal FROM Student
SELECT Student.*,
--Query required for paging, this query used to show total records filtered
COUNT(StudentId) OVER (PARTITION BY 1) AS RecordsFiltered
FROM Student
WHERE
--Generic Search
-- Below is the column list to add in Generic Serach
(#Search IS NULL OR Student.FirstName LIKE '%'+ #Search +'%')
OR (#Search IS NULL OR Student.LastName LIKE '%'+ #Search +'%')
--Order BY
-- Below is the column list to allow sorting
ORDER BY
CASE WHEN #SortDir = 'asc' AND #OrderBy = 'FirstName' THEN Student.FirstName END,
CASE WHEN #SortDir = 'desc' AND #OrderBy = 'FirstName' THEN Student.FirstName END DESC,
CASE WHEN #SortDir = 'asc' AND #OrderBy = 'LastName' THEN Student.LastName END,
CASE WHEN #SortDir = 'desc' AND #OrderBy = 'LastName' THEN Student.LastName END DESC,
OFFSET #PageSize * (#PageNumber - 1) ROWS FETCH NEXT #PageSize ROWS ONLY;
END
My first thought was to write a query something like this...
SELECT EmpId, NameLast, NameMiddle, NameFirst, DepartmentName
FROM dbo.Employee
INNER JOIN dbo.Department ON dbo.Employee.DeptId = dbo.Department.Id
WHERE IdCrq IS NOT NULL
AND
(
#bitSearchFirstName = 0
OR
Employee.NameFirst = #vchFirstName
)
AND
(
#bitSearchMiddleName = 0
OR
Employee.NameMiddle = #vchMiddleName
)
AND
(
#bitSearchFirstName = 0
OR
Employee.NameLast = #vchLastName
)
AND
(
#bitSearchDepartment = 0
OR
Department.Id = #intDeptID
)
...which would then have the caller provide a bit flag if they want to search a particular field and then supply the value if they are to search for it, but I don't know if this is creating a sloppy WHERE clause or if I can get away with a CASE statement in the WHERE clause.
As you can see this particular code is in T-SQL, but I'll gladly look at some PL-SQL / MySQL code as well and adapt accordingly.
I would stick with the NULL/COALESCE method over AdHoc Queries, and then test to make sure you don't have performance problems.
If it turns out that you have slow running queries because it's doing a table scan when you're searching on columns that are indexed, you could always supplement the generic search stored procedure with additional specific ones that allow searching on these indexed fields. For instance, you could have a special SP that does searches by CustomerID, or Last/First Name.
Write a procedure to insert all employee data whose name start with A in table??