Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a parameter is null or empty in a stored procedure

I know how to check if a parameter is null but i am not sure how to check if its empty ... I have these parameters and I want to check the previous parameters are empty or null and then set them like below

ALTER PROCEDURE [dbo].[GetSummary]     @PreviousStartDate NVARCHAR(50) ,     @PreviousEndDate NVARCHAR(50) ,     @CurrentStartDate NVARCHAR(50) ,     @CurrentEndDate NVARCHAR(50) AS   BEGIN     IF(@PreviousStartDate IS NULL OR EMPTY)         SET @PreviousStartdate = '01/01/2010'  for example.. 

I would appreciate the help.

like image 333
user710502 Avatar asked Mar 24 '12 20:03

user710502


People also ask

IS NOT NULL in SQL stored procedure?

Let's look at an example of how to use the IS NOT NULL condition in a SELECT statement in SQL Server. For example: SELECT * FROM employees WHERE last_name IS NOT NULL; This SQL Server IS NOT NULL example will return all records from the employees table where the last_name does not contain a null value.

IS NOT NULL in MySQL stored procedure?

The MySQL ISNULL() function is used for checking whether an expression is NULL or not. This function returns 1 if the expression passed is NULL, else it returns 0. The ISNULL() function accepts the expression as a parameter and returns an integer a value 0 or 1 depending on the parameter passed.

Is null or empty SQL query?

NULL is used in SQL to indicate that a value doesn't exist in the database. It's not to be confused with an empty string or a zero value. While NULL indicates the absence of a value, the empty string and zero both represent actual values.


2 Answers

I sometimes use NULLIF like so...

IF NULLIF(@PreviousStartDate, '') IS NULL 

There's probably no reason it's better than the way suggested by @Oded and @bluefeet, just stylistic preference.

@danihp's method is really cool but my tired old brain wouldn't go to COALESCE when I'm thinking is null or empty :-)

like image 103
Rex Miller Avatar answered Oct 24 '22 17:10

Rex Miller


Here is the general pattern:

IF(@PreviousStartDate IS NULL OR @PreviousStartDate = '') 

'' is an empty string in SQL Server.

like image 32
Oded Avatar answered Oct 24 '22 18:10

Oded