Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query Syntax Error - Spaces in Field Names

The database my application uses has field names containing spaces. I believe this to be the cause of my problem. Here is a typical query:

SELECT * FROM 'OV2 BAS' AS bas 
INNER JOIN 'OV2 RefID' AS ids ON 'bas.Ref ID' = 'ids.Ref ID' 
WHERE ids.ENUM_H = 'TDischarge';

How do I deal with the spaces in the field names? Thanks.

Additional Information

This is to access a database made with MS Access 2007 (Microsoft.ACE.OLEDB.12.0).

like image 414
Jim Fell Avatar asked Nov 02 '11 16:11

Jim Fell


People also ask

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.

How do you query column names with spaces?

To select a column name with spaces, use the back tick symbol with column name. The symbol is ( ` `). Back tick is displayed in the keyboard below the tilde operator ( ~).

How remove spaces between names in SQL?

SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.


1 Answers

I don't think you can use quotes around the actual table name; only the name you assign to it. I would wrap the table in brackets instead: [OV2 BAS]

You also can't put quotes around your joining syntax either. Try this instead:

SELECT * FROM [OV2 BAS] AS bas INNER JOIN [OV2 RefID] AS ids ON bas.[Ref ID] = ids.[Ref ID] WHERE ids.ENUM_H = 'TDischarge';
like image 158
James Johnson Avatar answered Oct 08 '22 17:10

James Johnson