Posts

Showing posts from November, 2015

SQL - IsNullOrWhiteSpace Function

Just another SQL Code-snippet to check for NULL or whitespace value: -- ============================================= -- Author: John Wood -- Create date: 20151129 -- Description: Returns a boolean value -- ============================================= ALTER FUNCTION [Int].IsNullOrWhiteSpace ( -- Add the parameters for the function here @val sql_variant ) RETURNS bit AS BEGIN declare @ret bit if(SQL_VARIANT_PROPERTY(@val, 'BaseType') in ('varchar', 'nvarchar', 'char', 'nchar')) begin declare @char varchar = cast(@val as varchar) select @ret = iif(ltrim(rtrim(@char)) = '', 1, 0) end else begin -- Return the result of the function select @ret = iif( @val IS NULL, 1, 0 ) end return @ret END GO