Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2012-07-13 : 05:29:21
|
Hi,What is wrong with this query?Error is: Incorrect syntax near =declare @Qrt tinyint = 1selectfield1,'GrossQ' + convert(char(1), @Qrt) = GrossQrt,field3from tblMainThanks |
|
sql-programmers
Posting Yak Master
190 Posts |
Posted - 2012-07-13 : 06:01:34
|
Hi = GrossQrt is wrong ... You can write query as below... GrossQrt = (value) OR AS GrossQrt declare @Qrt tinyint = 1selectfield1,GrossQrt = 'GrossQ' + convert(char(1), @Qrt),field3from tblMainORselectfield1,'GrossQ' + convert(char(1), @Qrt) AS GrossQrt ,field3from tblMainSQL Server Programmers and Consultantshttp://www.sql-programmers.com/ |
 |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2012-07-13 : 06:06:28
|
I think you are incorrect because I want to rename the GrossQrt field in the select query. |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-07-13 : 07:35:08
|
In your output, what are the columns you are expecting to see? I would have thought 3 columns, field1, GrossQrt and, field3.What I interpreted from reading your original posting is the same as what sql-programmers seemed to think, and I would have suggested the same solutions s/he did. |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-07-13 : 13:30:35
|
quote: Originally posted by arkiboys I think you are incorrect because I want to rename the GrossQrt field in the select query.
That is why you should use the ANSI standard AS clause for renaming expressions/columns. You are using the equals (=) operator to do that and you have it backwards. GrossQrt = 'GrossQ' + convert(char(1), @Qrt)not'GrossQ' + convert(char(1), @Qrt) = GrossQrtEven better'GrossQ' + convert(char(1), @Qrt) AS GrossQrt |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-07-16 : 06:23:12
|
If you want to change the column value, the above will work. If you want to change the column alias it is not possible until you use dynamic sqlMadhivananFailing to plan is Planning to fail |
 |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2012-07-16 : 06:47:16
|
Thanks |
 |
|
|
|
|
|
|