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 1234»»»

Extract first numeric value from a string Expand / Collapse
Author
Message
Posted Thursday, June 27, 2013 12:57 PM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Thursday, October 9, 2014 7:14 AM
Points: 148, Visits: 294
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)')





Post #1468299
Posted Thursday, June 27, 2013 1:10 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Tuesday, December 23, 2014 12:10 PM
Points: 13,330, Visits: 12,829
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 Moden's 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)
Post #1468307
Posted Thursday, June 27, 2013 1:11 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, December 23, 2014 10:56 PM
Points: 868, Visits: 7,581
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
Post #1468308
Posted Thursday, June 27, 2013 1:32 PM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Thursday, October 9, 2014 7:14 AM
Points: 148, Visits: 294
Thanks Sean. Will read (love charts!)
Post #1468316
Posted Thursday, June 27, 2013 1:34 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Tuesday, December 23, 2014 12:10 PM
Points: 13,330, Visits: 12,829
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 Moden's 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)
Post #1468318
Posted Thursday, June 27, 2013 1:37 PM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Thursday, October 9, 2014 7:14 AM
Points: 148, Visits: 294
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
Post #1468321
Posted Thursday, June 27, 2013 1:44 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, December 23, 2014 10:56 PM
Points: 868, Visits: 7,581
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
Post #1468325
Posted Thursday, June 27, 2013 1:47 PM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Thursday, October 9, 2014 7:14 AM
Points: 148, Visits: 294
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'

Post #1468329
Posted Thursday, June 27, 2013 2:01 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Tuesday, December 23, 2014 12:10 PM
Points: 13,330, Visits: 12,829
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 Moden's 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)
Post #1468338
Posted Friday, June 28, 2013 9:18 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Thursday, October 9, 2014 7:14 AM
Points: 148, Visits: 294
Ok. I'll try it (never tried before)
Post #1468588
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse