Click here to Skip to main content
12,818,964 members (27,733 online)
Rate this:
Please Sign up or sign in to vote.
See more: SQL-Server
ALTER proc  [dbo].[pGetCode] --'InvestmentGroup', 'Code', 'I',5 

@TableName  varchar(20),

@ColumnName varchar(20)='',
@key varchar(10),
@TotalDigits int 
create table #temp (incode varchar(20));
declare @incode varchar(20);
declare @a varchar(10)=null;
declare @length int = 1;
declare @string varchar(10) ='';
declare @startposition varchar(20);
declare  @query varchar(100);

set @startposition=Len(@key)+1;

select @query='insert into #temp select   SUBSTRING( max('+@ColumnName+') ,'+@startposition+','+@TotalDigits+') as  int  from  '+ @TableName +'  where '+@ColumnName+' like '''+ @key +'0%'''
exec (@query)
select @incode = incode from #temp;
select @incode
drop table #temp;
Posted 2-Jan-13 18:26pm
Updated 2-Jan-13 18:43pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You're trying to use 'as int' to convert a string to an int and it's failing. In fact, all of this is ugly as hell. Try using CONVERT. Or better yet, select the values as a string and find out why your values don't convert to int. That is the issue, just like the error says

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100