Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 953527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:03:59+00:00 2026-05-16T00:03:59+00:00

I’ve heard that in some programming languages it is faster to check if the

  • 0

I’ve heard that in some programming languages it is faster to check if the length of a string is 0, than to check if the content is "". Is this also true for T-SQL?

Sample:

SELECT user_id FROM users WHERE LEN(user_email) = 0

vs.

SELECT user_id FROM users WHERE user_email = ''
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:03:59+00:00Added an answer on May 16, 2026 at 12:03 am

    Edit
    You’ve updated your question since I first looked at it. In that example I would say that you should definitely always use

    SELECT user_id FROM users WHERE user_email = ''
    

    Not

    SELECT user_id FROM users WHERE LEN(user_email) = 0
    

    The first one will allow an index to be used. As a performance optimisation this will trump some string micro optimisation every time! To see this

    SELECT * into #temp FROM [master].[dbo].[spt_values]
    
    CREATE CLUSTERED INDEX ix ON #temp([name],[number])
    
    SELECT [number] FROM #temp WHERE [name] = ''
    
    SELECT [number] FROM #temp WHERE LEN([name]) = 0
    

    Execution Plans

    Execution Plans

    Original Answer

    In the code below (SQL Server 2008 – I “borrowed” the timing framework from @8kb’s answer here) I got a slight edge for testing the length rather than the contents below when @stringToTest contained a string. They were equal timings when NULL. I probably didn’t test enough to draw any firm conclusions though.

    In a typical execution plan I would imagine the difference would be negligible and if you’re doing that much string comparison in TSQL that it will be likely to make any significant difference you should probably be using a different language for it.

    DECLARE @date DATETIME2
    DECLARE @testContents INT
    DECLARE @testLength INT
    
    SET @testContents = 0
    SET @testLength = 0
    
    
    DECLARE 
      @count INT,
      @value INT,
      @stringToTest varchar(100)
    
    
    set @stringToTest = 'jasdsdjkfhjskdhdfkjshdfkjsdehdjfk'
    SET @count = 1
    
    WHILE @count < 10000000
    BEGIN
    
      SET @date = GETDATE()
      SELECT @value = CASE WHEN @stringToTest = '' then 1 else 0 end
      SET @testContents = @testContents + DATEDIFF(MICROSECOND, @date, GETDATE())
    
      SET @date = GETDATE()
      SELECT @value = CASE WHEN len(@stringToTest) = 0 then 1 else 0 end
      SET @testLength = @testLength + DATEDIFF(MICROSECOND, @date, GETDATE())
    
      SET @count = @count + 1
    END
    
    SELECT 
      @testContents / 1000000. AS Seconds_TestingContents, 
      @testLength / 1000000. AS Seconds_TestingLength
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.