Click here to Skip to main content
15,903,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I have a SQL query that has 4 parameters in where clause.

Patient_No (=)
Last_Name (begin with)
First_Name (begin with)
DOB (=)

The values for these 4 are supplied by user from UI. Now user may or may not enter all the values.
SO if user enters Patient_No, it should match exactly (=).
If he enters Last name, it should search begin with (like 'pete%').
If he enters First name, it should search begin with (like 'pete%').
If he enters DOB, it should match exactly.

If user enters all the values right, the query works perfect. But if he enters any of only one value, then the query doesn't work.

Here is my query:
SQL
SELECT  [Last_Name],[First_Name], [middle_name],[Patient_No],[gender],[Date_of_Birth] 
FROM [Demographics] 
where 
([Patient_No] = {MRN} or [Patient_No] = '') and 
( [Last_Name] like  {Last Name}+'%' or [Last_Name] = '') and 
( [First_Name] like {First Name}+'%' or [First_Name] = '') and 
( [Date_of_Birth] = {Date of Birth} or [Date_of_Birth] = '' )

Where the values in { } are user provided.
Posted

Let me draw your attention on your mistake with just one of the expressions, but all are the same:
[Last_Name] like {Last Name}+'%' or [Last_Name] = ''
[Last_Name] is the field name, mot the parameter. You should use this isntead:
[Last_Name] like {Last Name}+'%' or {Last Name} = ''
But be aware to really pass empty string nothing is entered, and not null or whitespaces.
 
Share this answer
 
Comments
Lokesh Zende 3-Feb-15 6:26am    
Yes. That was it. Thanks Zoltan.
try

SQL
Decalre @MRN varchar(50) --parameter
Decalre @FName varchar(50)--parameter
Decalre @LName varchar(50)--parameter
Decalre @DOB varchar(50)--parameter

SELECT  [Last_Name],[First_Name], [middle_name],[Patient_No],[gender],[Date_of_Birth] 
FROM [Demographics] 
where Patient_No= 
    Case
       When @MRN <>'' Then
         @MRN 
       Else
         Patient_No
   End
And First_Name like 
  Case 
     When @FName <>'' Then
        @FName+'%'
      Else 
       '%'
   End
And Last_Name like 
  Case 
     When @LName<>'' Then
        @LName+'%'
      Else 
       '%'
   End
And Date_of_Birth=
  Case 
     When @DOB <>'' Then
        @DOB
      Else 
       Date_of_Birth
   End

Ref.
https://msdn.microsoft.com/en-us/library/ms181765.aspx[^]
 
Share this answer
 
v2
The main problem you have is testing for the Column = '' rather than the variable as Zoltan has pointed out, but you're also not catering for null values.

The following solution will cater for any of the columns being null AND/OR any of the variables being null
SQL
-- Variables
DECLARE @MRN VARCHAR(3)
DECLARE @FN varchar(30)
DECLARE @LN varchar(30)
DECLARE @DOB Date

-- Test data (changes)
SET @MRN = ''
SET @FN = NULL
SET @LN = 'Smith'
SET @DOB = NULL

SELECT  [Last_Name],[First_Name], [middle_name],[Patient_No],[gender],[Date_of_Birth]
FROM [Demographics]
where
ISNULL([Patient_No],'') LIKE COALESCE(@MRN + '%',Patient_No,'%')
AND
ISNULL([Last_Name],'') LIKE COALESCE(@LN + '%', Last_Name, '%')
AND
ISNULL([First_Name],'') LIKE COALESCE(@FN + '%', First_Name, '%')
AND
ISNULL(Date_of_Birth,'14-OCT-1066') = COALESCE(@DOB, Date_of_Birth, '14-OCT-1066')

A couple of points of interest:
- I used 14-OCT-1066 for handling null dates of birth, largely because no-one is likely to have that date of birth - you might need to change this depending on your context. (Aside - it's the date of the Battle of Hastings if you're interested)

- I used LIKE for the Patient_No instead of = to simplify the code - i.e. to avoid having to use a case statement. Otherwise that first condition would have been
ISNULL([Patient_No],'') = CASE WHEN @MRN='' THEN ISNULL(Patient_No,'') ELSE COALESCE(@MRN,Patient_No,'') END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900