Click here to Skip to main content
Rate this: bad
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 17:26pm
Edited 2-Jan-13 17: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
Your Filters
0 Shai Vashdi 1,518
1 Tadit Dash 350
2 Manas Bhardwaj 319
3 OriginalGriff 243
4 Sergey Alexandrovich Kryukov 205
0 Sergey Alexandrovich Kryukov 9,530
1 OriginalGriff 5,716
2 Peter Leow 4,320
3 Maciej Los 3,540
4 Abhinav S 3,373

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