Does the order of operation that you place your LTRIM
and RTRIM
matter when used in conjunction with ISNULL
? For instance, take the following example where a user potentially enters a bunch of spaces in a field, but we trim their input to be an actual NULL
value to avoid storing empty strings.
I am performing the TRIM
operations outside of ISNULL
:
DECLARE @Test1 varchar(16) = ' '
IF LTRIM(RTRIM(ISNULL(@Test1,''))) = ''
BEGIN
SET @Test1 = NULL
END
SELECT @Test1
This appropriately returns a true NULL
value. Now let's place ISNULL
on the outside:
DECLARE @Test2 varchar(16) = ' '
IF ISNULL(LTRIM(RTRIM(@Test2)),'') = ''
BEGIN
SET @Test2 = NULL
END
SELECT @Test2
This also returns a NULL
value. Both work well for the intended usage, but I'm curious if there is any difference to how the SQL query optimizer handles this?
NULL
, an empty string (''
), and string literal'NULL'
values. The purpose of my OP is to understand whether the order of operation matters or not when trimming input. If I enter space a couple of times into a web form field, I do not want to store those empty strings. I want to store aNULL
column row. By trimming that space (' '
), I can evaluate anISNULL
expression. However, that is beyond the scope of the question. – PicoDeGallo Jun 19 '18 at 22:23NULL
, you might tryNULLIF(LTRIM(RTRIM(@Test1)),'')
– SQLFox Jun 19 '18 at 22:33