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 6165487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:10:25+00:00 2026-05-23T22:10:25+00:00

I am attempting to error trap a T-SQL variable name by making sure that

  • 0

I am attempting to error trap a T-SQL variable name by making sure that the value of the variable is prefixed with a bracket "[".

Here’s an example of how I am trying to do this:

DECLARE @thing nvarchar(20)
SET @thing = '[55555'
IF(@thing NOT LIKE '[' + '%') --If the value does not start with [ then add it
BEGIN
SET @thing = '[' + @thing
END
PRINT @thing

The example above PRINT’s [[55555

Notice that the original value of @thing was prefixed with the bracket "[". I was expecting the IF condition would have returned false since "[55555" is LIKE '[' + '%'

Why is the IF condition not returning false? And, more importantly I suppose, what is the correct syntax to check for the existence of a string that occurs at the beginning of a variable string value?

EDIT
It appears as there is something special about the bracket "[". When I run LIKE on a bracket it doesn’t do what I expect, but when I don’t use a bracket the LIKE works the way I expect.

Check out these examples:

IF('[' NOT LIKE '[')
BEGIN
PRINT '[ is NOT LIKE ['
END
ELSE
BEGIN
PRINT '[ is LIKE ['
END

IF('StackO' NOT LIKE 'StackO')
BEGIN
PRINT 'STACKO is NOT LIKE StackO'
END
ELSE
BEGIN
PRINT 'STACKO is LIKE StackO'
END

Here’s the output of the two conditions:

[ is NOT LIKE [

STACKO is LIKE StackO

  • 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-23T22:10:26+00:00Added an answer on May 23, 2026 at 10:10 pm

    I believe it may be because ‘[‘ is actually part of the LIKE operators syntax, as defined here: http://msdn.microsoft.com/en-us/library/ms179859.aspx

    You need to define an escape character to escape the [, like this:

    DECLARE @thing nvarchar(20)
    SET @thing = '[55555'
    IF(@thing NOT LIKE '\[%' ESCAPE '\' )
    BEGIN
    SET @thing = '[' + @thing
    END
    PRINT @thing
    

    An alternative solution would be the following:

    DECLARE @thing nvarchar(20)
    SET @thing = '[55555'
    IF(LEFT(@thing,1) <> '[') --If the value does not start with [ then add it
    BEGIN
    SET @thing = '[' + @thing
    END
    PRINT @thing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

An odd error here, perhaps someone can help track down source as it's attempting
I've encountered a strange error when attempting to update a SharePoint 2010 list that
I get the following error when attempting to install RubyGems . I've tried Googling
I get the following error when attempting to connect to a web service via
I am getting the following error when attempting to build my project in Visual
I get the following error when attempting to use django-openid-auth OpenID discovery error: No
I'm receiving a rather cryptic error when attempting to debug a simple C++ application.
Using HttpClient , I receive the following error when attempting to communicate over HTTPS:
I am attempting to access a queue and receiving an access denied error. The
When attempting to compile my C# project, I get the following error: 'C:\Documents and

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.