Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
4.43/5 (3 votes)
See more:
I want to select database column which are having multiple spaces in column
SQL
Select  emp firstname lastname, id,branch(computer science as cse) from emptable

Emp firstname lastname is a column with spaces

Pls help me
Posted
Updated 22-Nov-14 21:52pm
v4

It's bad practice to do that in the first place, but if you've already had,
use:

SQL
Select 'emp firstname lastname', id,branch(computer science as cse) from emptable


Cheers,
C
 
Share this answer
 
v2
In SQL, put square braces around an identifier that has spaces in (this includes column names, table names, triggers, etc.)

Select  [emp firstname lastname], id,branch([computer science] as cse) from emptable


(Update: per below this is for SQL Server only - for standard SQL use a double quote.)
 
Share this answer
 
v2
Comments
Jörgen Andersson 23-Nov-14 4:34am    
That's only valid for SQL Server, in standard SQL you use a double quote instead of square bracket.
Duncan Edwards Jones 23-Nov-14 5:19am    
Cool - I didn't know that.

The real solution is probably to not hard-code SQL statements in C# but rather use a data access layer...
Jörgen Andersson 23-Nov-14 5:32am    
Indeed it is.
I've been told that the reason SQLServer has it's own convention is so that you don't need to escape quotes in a string which is a common source of confusion.
Hi,

naming a table column e.g. "Computer Science"
is not good practice.

I suggest e.g. "ComputerScience"

If you designed the database then it's time to rename the columns.

Best regards,
Stephan
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900