Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL error when selecting column named YEAR

Tags:

sql

sql-server

I am trying to select a column named "YEAR" from a table.

I can achieve this using the following:

SELECT * FROM OPENQUERY (mybook, 'SELECT mytable.year FROM mytable')

but not when I use this:

SELECT * FROM OPENQUERY (mybook, 'SELECT year FROM mytable')

Is this because "year" is some sort of reserved word or is there another reason?

like image 354
Tom Avatar asked Aug 06 '26 23:08

Tom


1 Answers

In SQL Server, Year is a reserved word. So it will complain when you try to use it.

If you are using MySql, you can use back ticks (`) around year.

In SQL Server, you can use []'s around year, [year].

EDIT

After seeing your updated question, the following SQL should work:

SELECT * FROM OPENQUERY (mybook, 'SELECT [year] FROM mytable')

like image 193
Neil Knight Avatar answered Aug 08 '26 17:08

Neil Knight



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!