61

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.

bluish
  • 26,356
  • 27
  • 122
  • 180
user710502
  • 11,181
  • 29
  • 106
  • 161
  • Do you care if `@PreviousStartDate` is not just `NULL` or `''` but contains whitespace, e.g. `' '`. – Jodrell Sep 20 '22 at 07:06

12 Answers12

85

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 :-)

Mike G
  • 4,232
  • 9
  • 40
  • 66
Rex Miller
  • 2,706
  • 1
  • 19
  • 26
47

Here is the general pattern:

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

'' is an empty string in SQL Server.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
11

I use coalesce:

IF ( COALESCE( @PreviousStartDate, '' ) = '' ) ...
dani herrera
  • 48,760
  • 8
  • 117
  • 177
6

you can use:

IF(@PreviousStartDate IS NULL OR @PreviousStartDate = '')
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • 1
    @pavel thanks for pointing out the missin @ sign but you can see from the time stamp they were posted at the same time. – Taryn Dec 19 '12 at 23:38
4

What about combining coalesce and nullif?

SET @PreviousStartDate = coalesce(nullif(@PreviousStartDate, ''), '01/01/2010')
Pavel Hodek
  • 14,319
  • 3
  • 32
  • 37
4

Another option:

IF ISNULL(@PreviousStartDate, '') = '' ...

see a function based on this expression at http://weblogs.sqlteam.com/mladenp/archive/2007/06/13/60231.aspx

Erik K.
  • 1,024
  • 12
  • 13
3

To check if variable is null or empty use this:

IF LEN(ISNULL(@var, '')) = 0
Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
  • For MSSQL15 and a DATETIME variable, it doesn´t work: --DECLARE @var DATETIME; --SET @var='' --SET @var=NULL --SET @var='2020-06-13 22:30:00.000' --IF LEN(ISNULL(@var, '')) = 0 --PRINT 'is null or empty'; -- #same result for all cases – danalif Jun 14 '20 at 00:52
2

If you want to use a parameter is Optional so use it.

CREATE PROCEDURE uspGetAddress @City nvarchar(30) = NULL, @AddressLine1 nvarchar(60) = NULL
    AS
    SELECT *
    FROM AdventureWorks.Person.Address
    WHERE City = ISNULL(@City,City)
    AND AddressLine1 LIKE '%' + ISNULL(@AddressLine1 ,AddressLine1) + '%'
    GO
1

To check if variable is null or empty use this

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

You can try this:-

IF NULLIF(ISNULL(@PreviousStartDate,''),'') IS NULL
SET @PreviousStartdate = '01/01/2010'
rchau
  • 535
  • 9
  • 32
TracyT
  • 1
0

If you want a "Null, empty or white space" check, you can avoid unnecessary string manipulation with LTRIM and RTRIM like this.

IF COALESCE(PATINDEX('%[^ ]%', @parameter), 0) > 0
    RAISERROR ...
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • Isn't this a performance issue when doing thousands of selects per minute? – Mecanik Sep 19 '22 at 07:51
  • @Mecanik, it would effect performance, to some unmeasured extent. I'd try to prevent empty or whitespace from being inserted and keep the repeated selects simple. This answer is different to the others, in that, it checks for whitespace and not just empty. – Jodrell Sep 20 '22 at 07:03
0

I recommend checking for invalid dates too:

set @PreviousStartDate=case ISDATE(@PreviousStartDate) 
    when 1 then @PreviousStartDate 
        else '1/1/2010'
    end
John Dewey
  • 6,985
  • 3
  • 22
  • 26