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 2000 Forums
 SQL Server Development (2000)
 Substring function returns : Invalid column Name

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-12-09 : 07:00:15
padmanaban writes "select substring(Col_1,7,12) as col_1 from tbl

here col_1 is a column name in the table tbl and it is working fine.


declare @strq varchar(500)
set @strq = 'select '+ substring(Col_1 ,1,12)+' as Col_1 from tbl'
exec(@strq)

im getting error invalid column name 'col_1' while its executed.
Could Anyone help for this.
thanks in advance.

Padmanaban."

surendrakalekar
Posting Yak Master

120 Posts

Posted - 2005-12-09 : 07:26:44
try this..
declare @strq varchar(500)
set @strq = 'select substring(Col_1 ,1,12) as Col_1 from tbl'
exec(@strq)

Surendra
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-09 : 08:43:06
The entire statement should be assigned to Variable as suggested
Read this more on Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html

Madhivanan

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

- Advertisement -