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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 convert

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 = 1

select
field1,
'GrossQ' + convert(char(1), @Qrt) = GrossQrt,
field3
from tblMain

Thanks

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 = 1

select
field1,
GrossQrt = 'GrossQ' + convert(char(1), @Qrt),
field3
from tblMain

OR



select
field1,
'GrossQ' + convert(char(1), @Qrt) AS GrossQrt ,
field3
from tblMain


SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page

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.
Go to Top of Page

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.
Go to Top of Page

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) = GrossQrt

Even better

'GrossQ' + convert(char(1), @Qrt) AS GrossQrt

Go to Top of Page

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 sql

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-07-16 : 06:47:16
Thanks
Go to Top of Page
   

- Advertisement -