• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

explanation of if isnull sql statement

Could somebody clarify what is going on in this statement. The part that I am not sure about is...at the end  ,' ') = ' '

if isnull(@filename_to_import,' ') = ' '

Thanks
0
tesla764
Asked:
tesla764
  • 2
2 Solutions
 
tim_csCommented:
It's checking to see if @filename_to_import is NULL or an empty string.

ISNULL() checks the first value @filename_to_import, if it is NULL then it returns whatever is in the second half, in this case an empty string.  If it's not NULL then it will return whatever value @filename_to_import is.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
> isnull(@filename_to_import,' ')
Return either the contents of the @filename_to_import variable if it's not NULL, or return a single space ' '.   This approach is useful when you want to weed out NULLs early before doing expressions.

>if isnull(@filename_to_import,' ') = ' '
If the @filename_to_import variable contains either NULL or '', then do whatever is below that line, such as..
if isnull(@filename_to_import,' ') = ' '
   begin
   -- There's no file name to import.  
   -- Either do an alternate thing, or end the sp. 
   end 
ELSE 
   begin
   --  There's a file name to import.  Rock on. 
   end

Open in new window

0
 
tesla764Author Commented:
Thanks Everybody. That was very helpful.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the split.  Good luck with your project.  -Jim
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now