Using a Select Statement for Columns With Spaces in It

How to select a column name with a space in MySQL

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.

how to SELECT a column which has a space in between its name

Single quotes (') denote character literals. I.e., you're selecting the string 'REJECTED COST', which, obviously, cannot be cast to a number.
In order to select a column name with a space, you should use double quotes ("). Note that they need to be escaped, as you're using them inside a c++ string, which is also denoted by double quotes:

string sqlStmt = "SELECT \"REJECTED COST\", APPROVED_COST FROM COST_TABLE where PART_NUM= 'PN4879-1'";

How do you deal with blank spaces in column names in SQL Server?

select [Response Status Code], [Client Response Status Code]
from TC_Sessions (NOLOCK)
WHERE StartDate BETWEEN '05-15-2012' AND '06-01-2012'
AND SupplyID = 3367

Wrap the names in square brackets.

It is , however, best to avoid spaces in names if possible. It just creates more work for you down the road...

Handling column names with spaces in SQL Server

Looks like an error in your syntax, try this as an example:

UPDATE dbo.Survey
SET PhotoPathQ1 = null
WHERE [Q1 Photo Taken] = 0

This assumes that the field PhotoPathQ1 is nullable and you actually want to insert a true null value in to it rather than a string '(null)'.

It also assumes that [Q1 Photo Taken] is a bit or int field, although SQL Server will handle the conversion happily if you have it in quotes. If it's a string data type, then you should leave the quotes there.

You should use square brackets on field names that contain spaces instead of double quotes:

[Q1 Photo Taken]



Related Topics



Leave a reply



Submit