To select a column with spaces in SQL, you typically need to use quotation marks or square brackets around the column name, depending on the database management system (DBMS) you are using.
1. For MySQL and PostgreSQL:
You should use backticks (
`) to enclose column names with spaces.
SELECT `column with spaces`
FROM table_name;
2. For SQL Server:
You should use square brackets ([ ]
).
SELECT [column with spaces]
FROM table_name;
3. For Oracle:
You should use double quotes (" "
).
SELECT "column with spaces"
FROM table_name;
Example:
Let's assume you have a table called employee_data
with a column employee name
(with a space).
-
MySQL/PostgreSQL:
SELECT `employee name` FROM employee_data;
-
SQL Server:
SELECT [employee name] FROM employee_data;
-
Oracle:
SELECT "employee name" FROM employee_data;
General Advice:
- If possible, avoid using spaces in column names as it can lead to confusion and errors. You can use underscores (
_
) instead, likeemployee_name
, which is easier to work with across all DBMSs.
No comments:
Post a Comment