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

T-SQL Expand / Collapse
Author
Message
Posted Sunday, March 24, 2013 12:41 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 27, 2014 9:32 AM
Points: 9, Visits: 95
Hi SQL Gurus,

I need some help .. I want only the last part of IP from the string.For example :-

jdbc:inetpool:inetdae:10.25.81.123:17100?database=us_nnnn

i need "123" .

The ip address will be dynamic/change.

Any help will be highly appreciated.

Thanks,

Viv



Roshan
Post #1434693
Posted Sunday, March 24, 2013 2:17 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:37 AM
Points: 35,263, Visits: 31,753
There are a ton of ways to do this but they all usually depend on certain things being there unless you happen to be pretty good at writing AI code.

Assuming that the only periods in the whole string will be a part of the IP address and the IP address will always be immediately followed by a colon, this is one of the simplest methods.

--===== Setup the test.  This is NOT a part of the solution.
DECLARE @SomeString VARCHAR(8000);
SELECT @SomeString = 'jdbc:inetpool:inetdae:10.25.81.123:17100?database=us_nnnn';

--===== Demonstrate the solution which could be modified to work on a table column.
SELECT SUBSTRING(PARSENAME(@SomeString,1),1,CHARINDEX(':',PARSENAME(@SomeString,1))-1);




--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1434699
Posted Sunday, March 24, 2013 8:16 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
I haven't tested any of these options for performance. On a small data set all of them will probably perform OK. But if you are going to use this in production or on a very large table then it would be necessary to create some performance metrics against the real data.


--some options
;WITH sampledata
AS
(
SELECT * FROM
(VALUES
('jdbc:inetpool:inetdae:10.25.81.123:17100?database=us_nnnn'),
('jdbc:inetpool:inetdae:10.25.81.456:17100?database=us_nnnn'),
('jdbc:inetpool:inetdae:10.25.81.789:17100?database=us_nnnn'),
('jdbc:inetpool:inetdae:10.25.81.012:17100?database=us_nnnn'),
('jdbc:inetpool:inetdae:10.25.81.321:17100?database=us_nnnn')
) AS data([IP])
)
SELECT
--if the IP part 4 is always 3 digits and no periods in the text following the IP
SUBSTRING(IP,LEN(IP)+2-CHARINDEX('.',REVERSE(IP)),3) AS [IP4-1]
--if everything before and after the IP is always the same
,STUFF(REVERSE(STUFF(REVERSE(IP),1,23,'')),1,31,'') AS [IP4-2]
--will find the IP part even if the other text changes assuming just the 3 periods in the IP
,STUFF(REVERSE(STUFF(REVERSE(IP),1,CHARINDEX(':',REVERSE(IP)),'')),1,CHARINDEX('.',IP,(CHARINDEX('.',IP,CHARINDEX('.',IP,(CHARINDEX('.',IP)+1)))+1)),'') AS [IP4-3]
FROM
sampledata



--this will work even if the other text is variable.
--assumes the only periods are in the IP address...if not just change
--the itemnumber in the where clause (see second example)
;WITH sampledata
AS
(
SELECT * FROM
(VALUES
('jdbc:inetpool:inetdae:10.25.81.123:17100?database=us_nnnn'),
('jdbc:inetpool:inetdae:10.25.81.456:17100?database=us_nnnn'),
('jdbc:inetpool:inetdae:10.25.81.789:17100?database=us_nnnn'),
('jdbc:inetpool:inetdae:10.25.81.012:17100?database=us_nnnn'),
('jdbc:inetpool:inetdae:10.25.81.321:17100?database=us_nnnn')
) AS data([IP])
)
SELECT
LEFT(Item,3) AS IP4
FROM
sampledata s
CROSS APPLY
dbo.DelimitedSplit8K(s.IP,'.') dsk
WHERE
ItemNumber = 4

;WITH sampledata
AS
(
SELECT * FROM
(VALUES
('jdbc.inetpool.inetdae:10.25.81.123:17100?database=us_nnnn'),
('jdbc.inetpool.inetdae:10.25.81.456:17100?database=us_nnnn'),
('jdbc.inetpool.inetdae:10.25.81.789:17100?database=us_nnnn'),
('jdbc.inetpool.inetdae:10.25.81.012:17100?database=us_nnnn'),
('jdbc.inetpool.inetdae:10.25.81.321:17100?database=us_nnnn')
) AS data([IP])
)
SELECT
LEFT(Item,3) AS IP4
FROM
sampledata s
CROSS APPLY
dbo.DelimitedSplit8K(s.IP,'.') dsk
WHERE
ItemNumber = 6



 
Post #1434728
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse