Point in SQL statement

0

I have to pull a list of data from an Excel sheet which has a point in the column header. If I remove the point everything works perfectly, but if I keep the point then I get an error message.

Working: SELECT DISTINCT Ty FROM dbStock

Not working: SELECT DISTINCT Ty. FROM dbStock

System.Data.OleDb.OleDbException (0x80040E14): Syntax error (missing operator) in query expression 'Ty.'.

I searched on the internet, but couldn't find anything about this. I tried to place Ty. between '', "" and [], but no luck.

Any idea?

sql
excel
oledb
asked on Stack Overflow Apr 27, 2016 by GertDeWilde • edited Apr 27, 2016 by underscore_d

2 Answers

1

EDIT2: Just to put this into the answer (from comments)

The solution was to try a SELECT * FROM .... This brought up, that the column's name was translated to Ty#. The OP could continue with this name...

previous

From this

Working: SELECT DISTINCT Ty FROM dbStock
Not working: SELECT DISTINCT Ty. FROM dbStock

I take, that the column's name is Ty actually. The point let's the engine think, that this is a multipart qualifier (like tablename.columnname).

You might use

SELECT DISTINCT Ty AS [Ty.] FROM dbStock

If you need the point in the output column name...

EDIT: Just to have mentioned it:

SQL-Server can deal with this...

declare @tbl TABLE([Ty.] INT);
INSERT INTO @tbl VALUES(1),(2),(3);

SELECT * FROM @tbl
SELECT [Ty.] FROM @tbl
answered on Stack Overflow Apr 27, 2016 by Shnugo • edited Jun 20, 2020 by Community
0

You can use

SELECT DISTINCT [Ty.]

OR

SELECT DISTINCT "Ty." // In this case, check if QUOTED_IDENTIFIER is set to ON.
answered on Stack Overflow Apr 27, 2016 by MusicLovingIndianGirl • edited Apr 27, 2016 by MusicLovingIndianGirl

User contributions licensed under CC BY-SA 3.0