Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql query if parameter is null select all

Tags:

sql

parameters

Can the following query be modified to return all records if the ? is null?

SELECT NAME, SURNAME FROM MY_TABLE WHERE NAME = ?; 
like image 872
Fseee Avatar asked Nov 20 '12 13:11

Fseee


People also ask

How do you return all records if parameter is NULL?

Inside the stored procedure, the parameter value is first tested for Null using the ISNULL function and then checked whether it is Blank (Empty). If the parameter has value then only matching records will be returned, while if the parameter is Null or Blank (Empty) then all records from the table will be returned.

Is NULL parameter in SQL?

If you want a parameter that can filter by a certain value, but when you have no value in your parameter, SQL returns no results. When the parameter has no value, SQL interprets it as null in your code. Null means no value. You can fix this problem by adding a code to fix the null case.

How check variable is NULL or not in SQL?

IF (@item1 IS NOT NULL) OR (LEN(@item1) > 0) SELECT @sql = 'SELECT * FROM TEST1' ELSE SELECT @sql = 'SELECT * FROM TEST2' PRINT @sql; @item1 is NVARCHAR(1000) type. It is somewhere a function in sql to verify if a string is null or empty OR I made somewhere a mistake ?

IS NULL in SQL Server SELECT query?

Description. The IS NULL condition is used in SQL to test for a NULL value. It returns TRUE if a NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.


1 Answers

Try this:

SELECT *  FROM MY_TABLE  WHERE @parameter IS NULL OR NAME = @parameter; 
like image 125
Mahmoud Gamal Avatar answered Oct 05 '22 22:10

Mahmoud Gamal