searching from database

  • Hi

    I am trying to search names and surnames from database.For example i can search using only name and it will display all matching names in the database or i can use surname and it will display all matching surnames in the database or i can use both name and surname and it will display only exact name.check my select statement below

    SELECT Emp_No,Emp_Name,Emp_Surname FROM Employees WHERE Emp_Name LIKE @Emp_Name + '%' AND Emp_Surname LIKE @Emp_Surname+'%'AND Emp_Name LIKE @Emp_Name + '%' OR Emp_Surname LIKE @Emp_Surname+'%'

  • What you can do is if the parameter is null set it to equal itself. If you pass a parameter it will use the value otherwise it will ignore it.

    SELECT Emp_No,Emp_Name,Emp_Surname

    FROM Employees

    WHERE Emp_Name LIKE ISNULL(@Emp_Name + '%', Emp_Name) AND

    Emp_Surname LIKE ISNULL(@Emp_Surname + '%', Emp_Surname)

  • HI,

    You can also try out Dyanmic SQL query in T-SQL

    Check out the below link

    http://www.sommarskog.se/dyn-search.html#dynsql

    Thanks -- Vj

    http://dotnetvj.blogspot.com

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply