Where to Use SQURE Bracket Around Table Name



Have you ever thought, what if your table name

  1. Is exactly the same as a SQL Server keyword
  2. Has a space in it like "Student Fav Actor"

If you go ahead and query against the table "Student Fav Actor" like below, most likely you will get an error message as below:
SQURE Bracket in sql server

There may be one more scenario where the table name is something like FROM. If you query in the usual way, obviously you will be getting an expected error message.

SQURE Bracket in sql server

So how to query table named [However this is wrong practice to put spaces and use SQL Server keyword as table name] like above?

The solution is very simple; put the table name in square brackets.

SQURE Bracket in sql server

You might encounter columns name of table are having spaces or named as keywords. In that case also you need to apply square bracket with that particular column name to work with.

Query a range of characters?

If you need to query all People from the Person table with a FirstName starting with A, then you can easily query like below:

SQURE Bracket in sql server

What if you want to list all the people from the person table with the first name start with A and B? You can combine the Like operator with OR and query like below:

SQURE Bracket in sql server

But imagine the scenario where you need to query all the People with FirstName starting with A to M. How will you do that?
The solution is you need to put the like operator in the square bracket as below:

SQURE Bracket in sql server

If you modify one little thing in the above query and replace the like operator with an equal operator, you will get no error message however you will get no rows returned as well.

Up Next
    Ebook Download
    View all
    Learn
    View all