I need help pulling multiple number values out of a string
field name is Details and here are some examples of the values
DETAILS
Maximum width is 13'2", maximum length is 30'. Minimum total size for all custom rugs is 25 sq/ft.
Maximum width is 15', maximum length is 25'. Minimum total size for all custom rugs is 25 sq/ft.
Maximum width is 12', maximum length is 30'. Minimum total size for all custom rugs is 15 sq/ft.
i need so get the number values pulled out and into their own fields, and the numbers aren't always the same
here are two hopeful outputs
thanks in advance!
March 26, 2020 at 7:37 pm
Is it really SQL 7, 2000 you are using?
If you are using SQL 7/2000 and the PARSENAME function is available (I'm not sure if it is), you can try the following. This approach is making a BIG assumption about the descriptions stored in your "Details" column as well as the need to get the data in a single SELECT statement. Hope this helps steer you into a solution that works for you.
/*
Maximum width is 13'2", maximum length is 30'. Minimum total size for all custom rugs is 25 sq/ft.
Maximum width is 15', maximum length is 25'. Minimum total size for all custom rugs is 25 sq/ft.
Maximum width is 12', maximum length is 30'. Minimum total size for all custom rugs is 15 sq/ft.
*/
-- create a dummy table
if( object_id( 'tempdb..#orders') is not null) drop table #orders;
create table #orders(
order_id int identity(1,1) not null primary key clustered,
details nvarchar(255) not null
);
-- load sample data
insert #orders( details)
values( 'Maximum width is 13''2", maximum length is 30''. Minimum total size for all custom rugs is 25 sq/ft.'),
( 'Maximum width is 15'', maximum length is 25''. Minimum total size for all custom rugs is 25 sq/ft.'),
( 'Maximum width is 12'', maximum length is 30''. Minimum total size for all custom rugs is 15 sq/ft.')
-- extract the "width", "length" and "size" using SQL 7, 2000
-- *IF* detail descriptions are consistent *AND* PARSENAME is available in your version of SQL
select details,
x.measurements,
replace( parsename( x.measurements, 3), 'in', '"') as maxW,
replace( parsename( x.measurements, 2), 'in', '"') as maxL,
parsename( x.measurements, 1) as minSqF
from #orders o
cross apply (
select replace( replace( replace( replace( replace(
details, 'Maximum width is ', '')
, ', maximum length is ', '.')
, '. Minimum total size for all custom rugs is ', '.')
, ' sq/ft.', '')
, '"', 'in') as measurements
) x
April 5, 2020 at 1:08 pm
Here are the results:
April 5, 2020 at 2:33 pm
Neither CROSS APPLY nor the multiline version of VALUES is available in 7/2000.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 5, 2020 at 2:38 pm
Is it really SQL 7, 2000 you are using?
@tpd1989 ,
This is the KEY question on this thread. Please confirm that you are actually using SQL 7 or 2000 or not. It DOES make a HUGE difference for this problem.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 5, 2020 at 3:08 pm
Noted, thanks Jeff!
April 5, 2020 at 3:28 pm
Here's the test data using the "old fashioned" way that works in all versions of SQL Server since at least 6.5.
--===== Create the test table from what the OP provided.
IF OBJECT_ID('tempdb..#TempTable','U') IS NOT NULL DROP TABLE #TempTable
;
SELECT d.Details
INTO #TestTable
FROM (
SELECT Details = 'Maximum width is 13''2", maximum length is 30''. Minimum total size for all custom rugs is 25 sq/ft.' UNION ALL
SELECT 'Maximum width is 15'', maximum length is 25''. Minimum total size for all custom rugs is 25 sq/ft.' UNION ALL
SELECT 'Maximum width is 12'', maximum length is 30''. Minimum total size for all custom rugs is 15 sq/ft.'
) d
;
The OP provided two possible outputs but the "data" for only the first one. With that in mind, I'm only solving for the first one using the given data above.
Since all of the rows contain identical data except for the actual numeric values, I'm going to assume that will always be the case until the OP provides more substantial data. Since it's always the same, we can simply REPLACE what we don't want with an empty string and a key comma with a period. Then, PARSENAME does make quick work of this problem. If we actually need to, we can fire up a TALLY table (which does work on 7/2000) to make quick work of this type of thing but I'm going with the faster direct approach instead of a generic approach that can solve world hunger.
Here's the code.
SELECT minW = REPLACE(PARSENAME(d.CleanString,4),'!','"')
,minL = REPLACE(PARSENAME(d.CleanString,3),'!','"')
,minSqF = REPLACE(PARSENAME(d.CleanString,2),'!','"')
FROM (
SELECT CleanString = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
Details
,'Maximum width is ','')
,' maximum length is ','')
,' Minimum total size for all custom rugs is ','')
,',','.')
,'"','!') --Need to do this because PARSENAME doesn't like double-quotes.
+'X' --Simple method to "ignore" the last period.
FROM #TestTable
) d
;
--Jeff Moden
Change is inevitable... Change for the better is not.
April 5, 2020 at 5:36 pm
Hi Jeff, The second code option works exactly as I need it. I Saw Bill's first which also works so I marked that as the answer but wanted to let you know this also worked.
I'll be saving both for future uses.
Also sorry for not replying earlier, the email alerts were being sent to junk mail.
Thank you both so much!
April 5, 2020 at 6:10 pm
Hi Jeff, The second code option works exactly as I need it. I Saw Bill's first which also works so I marked that as the answer but wanted to let you know this also worked.
I'll be saving both for future uses.
Also sorry for not replying earlier, the email alerts were being sent to junk mail.
Thank you both so much!
Thanks for the feedback but... If Bill's code worked, does that mean you're actually using at least SQL Server 2005?
--Jeff Moden
Change is inevitable... Change for the better is not.
April 5, 2020 at 6:34 pm
So I wasn't entirely sure when I made this post but looking now is seems I have Microsoft SQL Server 2017. Sorry about that, I was searching for help and or any insight possible on my issue and it brought be here.
April 5, 2020 at 7:10 pm
Ok... that explains why Bill's good code worked. Thanks for coming back with that.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply