Click here to Skip to main content
15,913,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all experts,

I have one string with format like this:yyyyyyxx
yyyyy: is the numeric
xx: can be cm,m,mm,dm,.....
EX:1234m,12342cm,123456mm
My question is how i can get the numeric(1234 or 12342,....) from that string?

Thanks

TONY
Posted
Comments
[no name] 3-Aug-12 21:57pm    
SubString it
Kenneth Haugland 3-Aug-12 22:24pm    
Cant you do basic aritmatic and replace in SQL?
[no name] 3-Aug-12 22:37pm    
He's not asking to do arithmetic. He is just asking to extract a string from another and maybe convert.
Kenneth Haugland 3-Aug-12 22:43pm    
Then SubString is definatly the way to go. I dont understand how you could use that if they all warent the same, but thats another issue.
[no name] 3-Aug-12 22:44pm    
All he has to do is string length - 2 if the last 2 is the measurement type. But that is his problem. Don't you ever sleep?

Use this function

SQL
CREATE FUNCTION ExtractInteger(@String VARCHAR(2000))
RETURNS int
AS
BEGIN
DECLARE @Count INT
DECLARE @IntNumbers VARCHAR(1000)
SET @Count = 0
SET @IntNumbers = ''

WHILE @Count <= LEN(@String)
BEGIN
IF SUBSTRING(@String,@Count,1) >= '0'
AND SUBSTRING(@String,@Count,1) <= '9'
BEGIN
SET @IntNumbers = @IntNumbers + SUBSTRING(@String,@Count,1)
END
SET @Count = @Count + 1
END

RETURN Cast(@IntNumbers as int)
END
GO



Then call that in select query.
SQL
Select col1,dbo.ExtractInteger(col1) from tbl_temp
 
Share this answer
 
Comments
Maciej Los 17-Aug-12 17:45pm    
Good work, my 5!
Looks like the simple answer is do it yourself:
http://msdn.microsoft.com/en-us/library/cc295312%28v=expression.40%29.aspx[^]
 
Share this answer
 
Comments
soeun tony 3-Aug-12 22:13pm    
Hi Kenneth,
It is an example that i made myself and the real information i got also like this.
Based on your link, it is in Visual Studio....but i want to do it in SQL Server.

Thanks

TONY
Kenneth Haugland 3-Aug-12 22:19pm    
If its above m then multyply, if its belov devide:
http://en.wikipedia.org/wiki/Metric_prefix

or you could do it the way Wes suggested.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900