locked
Checking if String is NULL or EMPTY in SQL RRS feed

  • Question

  • User433556381 posted

    I need to check in my Stored procedure if the information passed is null or empty so I can decided to insert the new value or keep the old. How do I accomplish this please in T-SQL. Thanks in advance.

    Friday, November 9, 2007 9:29 AM

Answers

  • User1191518856 posted
    IF ISNULL(@param) OR @param = '' THEN doSomething...
    (Note the two apostrophes ' and ', not a quote mark!)
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 9, 2007 9:58 AM
  • User-1109331295 posted

      Books online (help files that comes with SQL) is an amzing little app.  Below is an example and the Syntax.

    USE pubs
    GO
    SELECT AVG(ISNULL(price, $10.00))
    FROM titles
    GO
    
    ISNULL ( check_expression , replacement_value ) 
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 9, 2007 10:00 AM
  • User-1655763558 posted

    Create Procedure mySpNameHere

    @InputValueHere VARCHAR(50) = NULL

    AS

    IF @InputValue IS NULL OR @InputValue = ''

    /*Keep the old value*/

    ELSE

    /*Run the update statement here with your new value*/

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 9, 2007 10:00 AM

All replies

  • User1191518856 posted
    IF ISNULL(@param) OR @param = '' THEN doSomething...
    (Note the two apostrophes ' and ', not a quote mark!)
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 9, 2007 9:58 AM
  • User-1109331295 posted

      Books online (help files that comes with SQL) is an amzing little app.  Below is an example and the Syntax.

    USE pubs
    GO
    SELECT AVG(ISNULL(price, $10.00))
    FROM titles
    GO
    
    ISNULL ( check_expression , replacement_value ) 
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 9, 2007 10:00 AM
  • User-1655763558 posted

    Create Procedure mySpNameHere

    @InputValueHere VARCHAR(50) = NULL

    AS

    IF @InputValue IS NULL OR @InputValue = ''

    /*Keep the old value*/

    ELSE

    /*Run the update statement here with your new value*/

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 9, 2007 10:00 AM
  • User1191518856 posted

    ISNULL ( check_expression , replacement_value ) 

     

     

    Sorry. I got a blackout. Of course, ISNULL syntax is to be used in a query where you want to specify an alternative value, if the expression is NULL.

    The correct way to check for NULL in a condition is IF @Param IS NULL as rich freeman points out.

    Friday, November 9, 2007 3:21 PM
  • User-166847701 posted

    This worked out for me!!! 

    Select
    Description=case when Description is null then '-'when Description='' then '-' else Description end

    from dbo.Products

    Monday, December 15, 2008 1:32 AM
  • User1893523956 posted

    SELECT ISNULL(NULLIF(Description,''),'-') FROM dbo.Products

    would have been an elegant one :-)

    Thursday, November 18, 2010 9:27 AM