1
select concat(Sno,Table) as STB from levels

Above query gives error if run as it is. Say i have valuse in levels as

Sno   Table
1     Sale
2     Stock

I need to fetch them as

STB
---
1Sale
2Stock

What can be the solution other than changing the column name because putting quotes around the word 'Table' gives the wrong output as it becomes just a string

Sami
  • 8,168
  • 9
  • 66
  • 99
  • Thanks a lot for the responses and special thanks to @Tenner. I accepted the other solution because of first came and low repo was also a bit factor, hope so tenner would not mind because there is also a first come reason as well :) – Sami Sep 24 '12 at 15:46

3 Answers3

2

Use backticks for reserved words.

select concat(Sno, `Table`) as STB from levels

Though in general, if you can avoid using reserved words for database, table, or column names in the future, that's a good idea.

James Cronen
  • 5,715
  • 2
  • 32
  • 52
  • Hope you have read my last statement `because putting quotes around the word 'Table' gives the wrong output as it becomes just a string` – Sami Sep 24 '12 at 15:27
  • That's not a quote, that's a backtick. If you're on a US keyboard, it's below the tilde character (~), probably next to the 1 key at the upper left. – James Cronen Sep 24 '12 at 15:28
  • I am sorry it was a very stupid question. Don't know why i missed to try backtick which I usually use rather than quotes. But thanks now I came to know the difference, and very foolish that i had been told that they are same and I never tried to look for the difference. Thanks a lot – Sami Sep 24 '12 at 15:33
2
select concat(Sno,`Table`) as STB 
from levels 
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
  • Hope you have read my last statement `because putting quotes around the word 'Table' gives the wrong output as it becomes just a string` – Sami Sep 24 '12 at 15:28
  • I am sorry it was a very stupid question. Don't know why i missed to try backtick which I usually use rather than quotes. But thanks now I came to know the difference, and very foolish that i had been told that they are same and I never tried to look for the difference. Thanks a lot – Sami Sep 24 '12 at 15:33
1

Try with ` instead of ' like this :

SELECT CONCAT(Sno,`Table`) AS STB FROM levels
Fry_95
  • 241
  • 1
  • 6
  • Hope you have read my last statement `because putting quotes around the word 'Table' gives the wrong output as it becomes just a string` – Sami Sep 24 '12 at 15:27
  • I am sorry it was a very stupid question. Don't know why i missed to try backtick which I usually use rather than quotes. But thanks now I came to know the difference, and very foolish that i had been told that they are same and I never tried to look for the difference. Thanks a lot – Sami Sep 24 '12 at 15:33
  • Haha no problem, it's a common mistake, but stop using reserved words. ;) – Fry_95 Sep 24 '12 at 15:37