Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4000952
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:50:43+00:00 2026-05-20T07:50:43+00:00

I have to write one Stored Procedure, In which I have to find the

  • 0

I have to write one Stored Procedure, In which I have to find the value from the table on the basis of Last Name, Last name search criteria can be Exact, Begin with or Phonetic.
SP will be like –

SELECT * FROM  Person       
    WHERE (
       CASE @lastNameCriteria 
       WHEN 'EXACT' THEN    Person.LastName = @LastName
       WHEN 'BEGIN' THEN    Person.LastName like @LastName
       ELSE SOUNDEX(tblPerson.LastName) LIKE SOUNDEX(@lastName))                     

I dont know above query will give in SQL. I dont want to use dynamic query, I can not use “IF ELSE”, because there might be some other criteria like FirstNameCriteria, MiddleNameCriteria, which will increase the complexity of “IF ELSE”.
Please suggest me what to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:50:43+00:00Added an answer on May 20, 2026 at 7:50 am

    You can use the CASE statement properly like this

    SELECT *
    FROM Person 
    WHERE
        CASE @lastNameCriteria
        WHEN 'EXACT' THEN
            CASE WHEN Person.LastName = @LastName THEN 1 END
        WHEN 'BEGIN' THEN
            CASE WHEN Person.LastName like @LastName THEN 1 END
        ELSE
            CASE WHEN SOUNDEX(tblPerson.LastName) = SOUNDEX(@lastName) THEN 1 END
        END = 1
    

    Which will force SQL Server to evaluate the @lastNameCriteria before processing the embedded conditions. You can also use multiple OR clauses which by virtue of @lastNameCriteria being compared to a string literal will cause short-circuit boolean evaluation – the parts involving the column comparison is not evaluated unless the @lastNameCriteria matches).

    SELECT *
    FROM Person 
    WHERE
    (@lastNameCriteria = 'EXACT' AND Person.LastName = @LastName)
    OR
    (@lastNameCriteria = 'BEGIN' AND Person.LastName like @LastName)
    OR
    (isnull(@lastNameCriteria,'') NOT IN ('EXACT','BEGIN')
     AND SOUNDEX(tblPerson.LastName) = SOUNDEX(@lastName))
    --option(recompile)
    

    Notes:

    1. Don’t (have to) use LIKE between the SOUNDEX, it is a simple = that you are after
    2. I made no assumptions about @lastNameCriteria which is why it is tested for both nulls and not in the other two
    3. If you are worried about parameter sniffing or incorrect query plans, uncomment the option (recompile) bit. Planning (each time) for such a simple query should take insignificant time.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing one stored procedure, which I have to create a xml column from
I'm attempting to write a stored procedure in Oracle (which I have come to
I have a stored procedure which returns a mixture of plain columns, and one
The Problem I'm trying to write a stored procedure in SQL Server to find
I have a database which has two tables. One table contains the information of
i need to write a stored procedure which will return a string.logic is when
I have a stored procedure that updates two tables. The first table is always
This is a complex one. But I have a table which has a DATETIME
I have a SQL table called StudentMarks.which consist of StudentID,SubjectName,SubjectMark I want to write
I have one procedure which updates record values, and i want to fire it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.