Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

What is Order of operation with LTRIM/RTRIM/ISNULL in SQL server?

Asked by Celina Lagunas Apr 24, 2021 3.1K views 1 answer
Share

About this question

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?

what is sql server rtrim? And what does it does?

Your answer

1 Answer

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.