SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Extract first numeric value from a string


Extract first numeric value from a string

Author
Message
valeryk2000
valeryk2000
Right there with Babe
Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)

Group: General Forum Members
Points: 731 Visits: 412
Brothers,
I need to parse the first numeric value from a string that usually contains several numerics.
This function works OK when the first number has no decimals ( 1 - returns '500000').
In #2 (naturally!) the result is 0
Can you spare some help?


alter FUNCTION dbo.fnExtractDigits (@inString VARCHAR(8000))
RETURNS VARCHAR(8000) AS
BEGIN
DECLARE @X VARCHAR(100)
Select @X=@inString
Select @X= SubString(@X,PATINDEX('%[0-9]%',@X),Len(@X))
Select @X= SubString(@X,0,PATINDEX('%[^0-9]%',@X))

RETURN @X
END

-- 1. select dbo.fnExtractDigits ('NYSTATIN SUSP 500000 UNIT = 5 ML (1 5 ML CUP)')
-- 2. select dbo.fnExtractDigits ('NYSTATIN SUSP 0.75 UNIT = 5 ML (1 5 ML CUP)')



Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62497 Visits: 17959
I would recommend NOT doing this as a scalar function. Scalar functions are not good for performance. You can easily leverage the DelimitedSplit8K function for this.


select top 1 *
from dbo.DelimitedSplit8K('NYSTATIN SUSP 500000 UNIT = 5 ML (1 5 ML CUP)', ' ')
where Item not like '%[^0-9.]%'

select top 1 *
from dbo.DelimitedSplit8K('NYSTATIN SUSP 0.75 UNIT = 5 ML (1 5 ML CUP)', ' ')
where Item not like '%[^0-9.]%'



You can find the code the that function by following the link in my signature about splitting strings.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
David Webb-CDS
David Webb-CDS
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4068 Visits: 8586
Sis,

Provided you don't have '.' anywhere except as a decimal separator:


alter FUNCTION dbo.fnExtractDigits (@inString VARCHAR(8000))
RETURNS VARCHAR(8000) AS
BEGIN
DECLARE @X VARCHAR(100)
Select @X=@inString
Select @X= SubString(@X,PATINDEX('%[0-9]%',@X),Len(@X))
Select @X= SubString(@X,0,PATINDEX('%[^.0-9]%',@X))

RETURN @X
END





And then again, I might be wrong ...
David Webb
valeryk2000
valeryk2000
Right there with Babe
Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)

Group: General Forum Members
Points: 731 Visits: 412
Thanks Sean. Will read (love charts!)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62497 Visits: 17959
Let's take the excellent example from David and turn it into a iTVF instead of a scalar function.


create FUNCTION dbo.fnExtractDigits (@inString VARCHAR(8000))
RETURNS table
return
select SubString(
SubString(@inString, PATINDEX('%[0-9]%', @inString), Len(@inString)), 0,
PATINDEX('%[^.0-9]%', SubString(@inString,PATINDEX('%[0-9]%',@inString),Len(@inString)))) as MyValue



Now we can really leverage some power and performance.


--need to setup our test data
;with myCte(SomeKey, SomeValue) as
(
select 1, 'NYSTATIN SUSP 500000 UNIT = 5 ML (1 5 ML CUP)' union all
select 2, 'NYSTATIN SUSP 0.75 UNIT = 5 ML (1 5 ML CUP)'
)

--now we can retrieve it
select *
from myCte
cross apply dbo.fnExtractDigits(SomeValue) s



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
valeryk2000
valeryk2000
Right there with Babe
Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)

Group: General Forum Members
Points: 731 Visits: 412
Dave - thanks, it works -need to play to make sure it's ok in other strings
Thanks
(I can only be your brother, not sister - need to ask dad ;-)
David Webb-CDS
David Webb-CDS
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4068 Visits: 8586
Sean has the best idea. His implementation will be far better behaved than the code I posted.



And then again, I might be wrong ...
David Webb
valeryk2000
valeryk2000
Right there with Babe
Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)

Group: General Forum Members
Points: 731 Visits: 412
Ha! Sean, you elegantly rewrote my function (stollen from somwhere anyway).
I do not think that to use cte in my case is a good idea - I use this function within a select statement from a big table - not sure that cte can be used here


SELECT
Real_Date_Time ChartedAt
,DRUG_INFO_1
,LEFT(drug_info_1,CHARINDEX(CHAR(32),drug_info_1)) Drug
,dbo.fnExtractDigits(DRUG_INFO_1) as DoseOrFirstNumeric
, Coalesce(case charindex('MG', DRUG_INFO_1)
when 0 then null
else 'MG'
end --Mg
,case charindex('GM', DRUG_INFO_1)
when 0 then null
else 'GM'
end --GM
,case charindex('UNIT', DRUG_INFO_1)
when 0 then null
else 'UNITS'
end ----UNITS
)

,ROUTE
,NO_OF_INGREDIENTS
,PMP
,pat_num
,ADMIN_STATUS
FROM MAK_Data.dbo.CHRT_Tran

Where
LEFT(drug_info_1,CHARINDEX(CHAR(32),drug_info_1))
IN
(
'abacavir', 'acyclovir', 'amikacin', 'amoxicillin', 'amoxicillin-clavulanic', 'amphotericin', 'ampicillin',
'ampicillin-sulbactam', 'atazanavir', 'atovaquone', 'azithromycin', 'aztreonam',
'cefazolin', 'cefdinir', 'cefepime', 'cefixime', 'cefotaxime', 'cefotetan',
'cefoxitin', 'cefpodoxime proxetil', 'ceftazidime', 'ceftriaxone', 'cefuroxime',
'cefuroxime axetil', 'cephalexin', 'ciprofloxacin', 'clarithromycin', 'clindamycin',
'colistimethate', 'dapsone', 'daptomycin', 'demeclocycline', 'dicloxacillin', 'doxycycline',
'efavirenz', 'ertapenem', 'erythromycin', 'erythromycin', 'ethambutol', 'fluconazole', 'gentamicin',
'hydroxychloroquine', 'imipenem', 'isoniazid', 'itraconazole', 'ketoconazole', 'lamivudine', 'levofloxacin',
'linezolid', 'meropenem', 'methenamine', 'metronidazole', 'minocycline', 'nafcillin',
'neomycin', 'nevirapine', 'nitrofurantoin', 'nystatin', 'oseltamivir', '
penicillin', 'pentamidine', 'piperacillin-tazobactam (single)', 'pyrazinamide', 'ribavirin',
'rifabutin', 'rifampin', 'ritonavir', 'sulfamethoxazole/trimethoprim', 'sulfasalazine',
'tenofovir', 'terbinafine', 'tetracycline', 'tigecycline', 'tobramycin', 'trimethoprim',
'valacyclovir', 'vancomycin', 'voriconazole'
)
and ROUTE IN ('iv','PO')
AND Real_Date_Time >'6/1/2013'
and ADMIN_STATUS ='admin'


Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62497 Visits: 17959
valeryk2000 (6/27/2013)
Ha! Sean, you elegantly rewrote my function (stollen from somwhere anyway).
I do not think that to use cte in my case is a good idea - I use this function within a select statement from a big table - not sure that cte can be used here


The only reason there is a cte there is for testing. I don't have a table with the values you posted so I just stuck them in a cte. It is merely an example of usage. That could a table, a cte, whatever.

They way you wrote the function and are using it is a scalar function. They are horrible for performance. Look at how I used the iTVF with cross apply instead of calling the function as a column. The performance benefits are HUGE!!!!

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
valeryk2000
valeryk2000
Right there with Babe
Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)Right there with Babe (731 reputation)

Group: General Forum Members
Points: 731 Visits: 412
Ok. I'll try it (never tried before)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search