Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a column name with a space in MySQL [duplicate]

Tags:

sql

mysql

I am working on a project where another developer created a table with column names like 'Business Name'. That is a space between two words. If I run a SELECT statement with 'Business Name' it says there is no column with name 'Business'.

How can I solve this problem?

like image 884
ehp Avatar asked Jan 07 '13 06:01

ehp


People also ask

Can MySQL column names have spaces?

Blanks spaces are restricted in the naming convention of the database object's name and column name of the table. If you want to include the blanks space in the object name or column name, the query and application code must be written differently. You must be careful and precise while writing dynamic SQL queries.

Can you have spaces in column names SQL?

Column names can contain any valid characters (for example, spaces).

How do I add a space to a selected query in SQL?

In many SQL relational database you will have to use the RPAD function to insert spaces into a character string. That also works in Vertica. However, for a more robust solution, Vertica provides the built-in function SPACE which returns the specified number of blank spaces.

How do you give an alias name with spaces in SQL?

If the alias_name contains spaces, you must enclose the alias_name in quotes. It is acceptable to use spaces when you are aliasing a column name. However, it is not generally good practice to use spaces when you are aliasing a table name. The alias_name is only valid within the scope of the SQL statement.


1 Answers

Generally the first step is to not do that in the first place, but if this is already done, then you need to resort to properly quoting your column names:

SELECT `Business Name` FROM annoying_table 

Usually these sorts of things are created by people who have used something like Microsoft Access and always use a GUI to do their thing.

like image 107
tadman Avatar answered Oct 08 '22 09:10

tadman