Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

allow varchar value to string and decimal parts Expand / Collapse
Author
Message
Posted Tuesday, August 26, 2008 5:02 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, January 20, 2010 2:34 PM
Points: 20, Visits: 56
i need a query that make
'A010' + 1 = 'A011' or
'ABC13456' + 12 = 'ABC13468'

how can i do this

Post #558726
Posted Tuesday, August 26, 2008 7:24 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, June 16, 2010 3:19 AM
Points: 22, Visits: 23
Dear Friend

Following procdure will help you to get your result.

Create Procedure ADD_NUMBER_TO_STRING
(
@Value varchar(20),
@AddNum int
)
AS

--===============================
--Question Is Described As below

--i need a query that make
--'A010' + 1 = 'A011' or
'ABC13456' + 12 = 'ABC13468'

--==================================


--Declare variables to be used
Declare @NumPart int
Declare @Counter int
Declare @Length int
Declare @Var as varchar(1)
Declare @NumStart int

--Create Temperory Table
Create table Tbl_Temp(AlphaBet varchar(1))

--Insert Alphabets and 0 for number start with 0
Insert Into Tbl_Temp values('0')
Insert Into Tbl_Temp values('A')
Insert Into Tbl_Temp values('B')
Insert Into Tbl_Temp values('C')
Insert Into Tbl_Temp values('D')
Insert Into Tbl_Temp values('E')
Insert Into Tbl_Temp values('F')
Insert Into Tbl_Temp values('G')
Insert Into Tbl_Temp values('H')
Insert Into Tbl_Temp values('I')
Insert Into Tbl_Temp values('J')
Insert Into Tbl_Temp values('K')
Insert Into Tbl_Temp values('L')
Insert Into Tbl_Temp values('M')
Insert Into Tbl_Temp values('N')
Insert Into Tbl_Temp values('O')
Insert Into Tbl_Temp values('P')
Insert Into Tbl_Temp values('Q')
Insert Into Tbl_Temp values('R')
Insert Into Tbl_Temp values('S')
Insert Into Tbl_Temp values('T')
Insert Into Tbl_Temp values('U')
Insert Into Tbl_Temp values('V')
Insert Into Tbl_Temp values('W')
Insert Into Tbl_Temp values('X')
Insert Into Tbl_Temp values('Y')
Insert Into Tbl_Temp values('Z')

--

Set @NumStart = 0
Set @Counter = 1


--Set @AlphaPart = substring(@Value,1,2)
--Set @NumPart = Substring(@Value,2,3)


--Seth Length of string
Set @Length = len(@Value)

while (@Counter <= @Length)
Begin

Set @Var = Substring(@Value, @Counter, @Counter + 1)

if( @Var = (Select Alphabet from Tbl_Temp where Alphabet = @Var))
Begin
Set @Counter = @Counter + 1
End
Else
Begin
Set @NumStart = @Counter
Break;
End

End

Set @NumPart = Substring(@Value, @NumStart ,@Length)
Set @Value = Substring(@Value,1,@NumStart - 1)

Set @NumPart = @NumPart + @AddNum
Set @Value = @Value + cast(@NumPart as varchar(20))

--Print Required result
Select 'Final Value : '+ @Value

--Drop Temperory Table
Drop Table Tbl_Temp

--=================
--Test : Execute Following Just Select Following Line and Execute
--Exec ADD_NUMBER_TO_STRING 'A010',1
--Exec ADD_NUMBER_TO_STRING 'ABC13456',12

Cheers,
Saurabh Singh





Post #558857
Posted Tuesday, August 26, 2008 8:39 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, September 24, 2008 1:27 PM
Points: 350, Visits: 229
Thats good saurabh.k.singh.

May I suggest that if you changed the declaration @NumPart from int to float, and @AddNum from int to float, then you will be able to add decimal figures as well :D.

EDIT:

Actually, that only allows one decimal place at the moment.
Post #558946
Posted Tuesday, August 26, 2008 1:48 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, January 20, 2010 2:34 PM
Points: 20, Visits: 56
thank's for help saurabh.k.singh
you are great
Post #559168
Posted Thursday, August 28, 2008 1:28 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 3:02 AM
Points: 177, Visits: 457
Hi there,

I have another solution. But this one also accepts symbols or any other characters EXCEPT numbers.
I'm not saying the other post was wrong. Actally I really like the post, very helpful and you can also use that sproc to do the same as mine by inputing numbers into the tables and making it do the opposite when filtering.

Oh yeah, i forgot to make this into decimal type but if you make it decimal, I think it will work fine... except for the last IF STATEMENT. But I think you can already alter it yourself.

Hope this helps

CREATE PROC AddToCodeNumber
@code VARCHAR(50),
@number INT
AS
BEGIN
DECLARE @Length INT,
@i INT

SELECT @Length = len(@code),
@i = 0

WHILE (SELECT SUBSTRING(@code,@length-@i,1)) IN ('1','2','3','4','5','6','7','8','9','0')
BEGIN
SELECT @i=@i+1
END

SELECT @number = RIGHT(@code,@i) + @number

IF LEN(@number)=LEN(RIGHT(@code,@i))
BEGIN
SELECT LEFT(@code,@Length-@i) + CAST(@number AS VARCHAR(MAX))
END
ELSE
BEGIN
SELECT 'ERROR'
END
END
RETURN
GO

EXEC AddToCodeNumber
@code ='ABC/*-+32100',
@number =212

EXEC AddToCodeNumber
@code ='ABC/*-+32100',
@number =2122346

Tell me if this was helpful ^__^ Thanks!


_____________________________________________
Quatrei Quorizawa
:):D:P;)
MABUHAY PHILIPPINES!

"Press any key...
Where the heck is the any key?
hmmm... Let's see... there's ESC, CTRL, Page Up...
but no any key"
- Homer Simpson
Post #560183
Posted Friday, August 29, 2008 5:10 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, November 6, 2014 5:01 AM
Points: 2,116, Visits: 6,441
Try this version using a Tally table. No loops, no cursors.


IF OBJECT_ID ( 'tempdb..#Tally' ) IS NOT NULL
BEGIN
DROP TABLE #Tally
END
GO


DECLARE @AlphaNumber varchar ( 20 )
DEcLARE @numtoadd decimal ( 20, 4 )
DECLARE @NumPart decimal ( 20, 4 )
DECLARE @Result varchar ( 20 )
DECLARE @strlen int
DECLARE @numpos int

SET @AlphaNumber = 'ABC12345.6789'
SET @numtoadd = 0.3211


SET @strlen = DATALENGTH ( @AlphaNumber )

SELECT TOP ( @strlen )
IDENTITY ( int, 1, 1 ) AS N
INTO #Tally
FROM master.dbo.syscolumns

ALTER TABLE #Tally
ADD C char ( 1 )

UPDATE #Tally SET C = SUBSTRING ( @AlphaNumber, N, 1 )

--SELECT * FROM #Tally

SELECT @numpos = MIN ( N ) FROM #Tally WHERE C BETWEEN '0' AND '9' OR C = '.'

--PRINT @numpos

SET @NumPart = CAST ( SUBSTRING ( @AlphaNumber, @numpos, @strlen ) AS decimal ( 20, 4 ) )

--PRINT @NumPart

SET @NumPart = @NumPart + @numtoadd

SET @Result = LEFT ( @AlphaNumber, @numpos - 1 ) + LTRIM ( STR ( @NumPart, @strlen, 4 ) )

PRINT @AlphaNumber + ' + ' + LTRIM ( STR ( @numtoadd, 10, 4 ) ) + ' = ' + @Result

DROP TABLE #Tally

I know, it breaks if there is no numeric part or there is more than one decimal point :)

Regards,
Jan


--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)

In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
Post #561104
Posted Friday, August 29, 2008 5:34 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, October 14, 2009 1:56 AM
Points: 438, Visits: 754
Hi,

try the below one.

DECLARE @Text VARCHAR(16)
SET @Text = 'ABC222'
SELECT SUBSTRING(@Text,PATINDEX('%[0-9]%',@Text),LEN(@Text)) + 100


---
Post #561117
Posted Friday, August 29, 2008 6:00 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, November 14, 2014 7:43 AM
Points: 1,792, Visits: 726
Here is another solution that builds on the previous suggestions. This one handles a few more variations of the text data and the number to be added.

create table #tester
(seq tinyint null,
txt varchar(20) null,
num integer null)
set nocount on
insert #tester values (1,'ABC44',10)
insert #tester values (2,'ABC44',0)
insert #tester values (3,'',10)
insert #tester values (4,'ABC4',10)
insert #tester values (5,'ABC44',1000)
insert #tester values (6,'ABC',10)
insert #tester values (7,null,10)
insert #tester values (8,'ABC',null)
insert #tester values (9,null,null)

set nocount off
select seq,
case when isnull(txt,'') = '' then isnull(convert(varchar,num),'')
when patindex('%[0-9]%',txt) = 0 then txt + isnull(convert(varchar,num),'')
else substring(txt,1,patindex('%[0-9]%',txt) - 1) +
convert(varchar,convert(int,substring(txt, patindex('%[0-9]%',txt),len(txt)) + isnull(num,0)))
end as 'result'
from #tester
order by seq

drop table #tester
/*
seq result
---- --------------------------------------------------
1 ABC54
2 ABC44
3 10
4 ABC14
5 ABC1044
6 ABC10
7 10
8 ABC
9

(9 row(s) affected)
*/
Post #561572
Posted Saturday, August 30, 2008 3:54 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 11:04 AM
Points: 35,546, Visits: 32,140
sqluser (8/29/2008)
Hi,

try the below one.

DECLARE @Text VARCHAR(16)
SET @Text = 'ABC222'
SELECT SUBSTRING(@Text,PATINDEX('%[0-9]%',@Text),LEN(@Text)) + 100
---

Doesn't return the "ABC" part... :)


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #561669
Posted Saturday, August 30, 2008 4:03 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 11:04 AM
Points: 35,546, Visits: 32,140
Mike Mullen (8/29/2008)
Here is another solution that builds on the previous suggestions. This one handles a few more variations of the text data and the number to be added.

Yes, it does. Nicely done! :)


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #561671
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse