Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Min and Max Number from string


Min and Max Number from string

Author
Message
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
Hello everyone

I need help to develop logic. In my query i have Alpha Numeric string with commas and hyphens.so from the string i need to populate min and max number

see below for DDL


create table #temp
(Code varchar(50))

insert into #temp values ('1,11-12ABCDEF(Y)')
insert into #temp values ('1,11ABCDEF(Y)')
insert into #temp values ('1,3-4AB1-3CD1-2,4EF(Y)')
insert into #temp values ('1,3-4MTWRF(Y)')
insert into #temp values ('1,3ABCDEF(Y)')
insert into #temp values ('1,3MTWRF(Y)')



below is the desired output

code min max
1,11-12ABCDEF(Y) 1 12
1,11ABCDEF(Y) 1 11
1,3-4AB1-3CD1-2,4EF(Y) 1 4
1,3-4MTWRF(Y) 1 4
1,3ABCDEF(Y) 1 3
1,3MTWRF(Y) 1 3


please help me to develop this logic.
Thanks
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8586 Visits: 18163
The Pattern Splitter made by Chris Morris is able to help you in this situation. You can read about it in this article: http://www.sqlservercentral.com/articles/String+Manipulation/94365/
And you'll end up with something like this:

SELECT Code, MIN( Item) minvalue, MAX( Item) maxvalue
FROM #temp t
CROSS APPLY dbo.PatternSplitCM(Code, '%[0-9]%')
WHERE Matched = 1
GROUP BY Code




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45443 Visits: 39944
yogi123 (4/30/2014)
Hello everyone

I need help to develop logic. In my query i have Alpha Numeric string with commas and hyphens.so from the string i need to populate min and max number

see below for DDL


create table #temp
(Code varchar(50))

insert into #temp values ('1,11-12ABCDEF(Y)')
insert into #temp values ('1,11ABCDEF(Y)')
insert into #temp values ('1,3-4AB1-3CD1-2,4EF(Y)')
insert into #temp values ('1,3-4MTWRF(Y)')
insert into #temp values ('1,3ABCDEF(Y)')
insert into #temp values ('1,3MTWRF(Y)')



below is the desired output

code min max
1,11-12ABCDEF(Y) 1 12
1,11ABCDEF(Y) 1 11
1,3-4AB1-3CD1-2,4EF(Y) 1 4
1,3-4MTWRF(Y) 1 4
1,3ABCDEF(Y) 1 3
1,3MTWRF(Y) 1 3


please help me to develop this logic.
Thanks


Would the smallest number be "1" and the largest number be "14" for the following?

7,3-4AB1-3CD1-2,14EF(Y)

--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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394


Would the smallest number be "1" and the largest number be "14" for the following?

7,3-4AB1-3CD1-2,14EF(Y)


Yes you correct the max number is 14

is anyone have solution for this?
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