Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving alias name with space in sql

I have a column in my query for which I want to use its alias name.

Currently it is looking like this:

SELECT U.first_name + ' ' + U.last_name UserName,

But I want to use it as like below

SELECT U.first_name + ' ' + U.last_name as User Name,

I tried but I got error as:

Incorrect syntax near the keyword 'User'.

like image 356
Nad Avatar asked Aug 31 '16 09:08

Nad


People also ask

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.

Can SQL field 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 we give space in table name in SQL?

To create a table with a space in the table name in MySQL, you must use backticks otherwise you will get an error.

How do you put a space in SQL query?

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.


2 Answers

Try This

SELECT U.first_name + ' ' + U.last_name as [User Name]
like image 159
taotechnocom Avatar answered Oct 04 '22 11:10

taotechnocom


Double quotes for alias with a space.

SELECT U.first_name + ' ' + U.last_name AS "User Name"
like image 24
Matt Avatar answered Oct 04 '22 12:10

Matt