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


String Function Help


String Function Help

Author
Message
VegasL
VegasL
Old Hand
Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)

Group: General Forum Members
Points: 371 Visits: 171
Hello,

Total Newbie to SQL Server, so pardon any non synchronous sql lingo.

In a table, i have column "IP Address"

which has values of ip address such as

102.89.93.101
100.100.10.1
123.94.9.121

etc

what I want SQL Server to do is return the first 3 values after the second dot.

So it should return

102.89.93
100.100.10
123.94.9

How to make possible?

Thanks in advance for any help.
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17411 Visits: 6431
Many ways. Choose your poison:


DECLARE @T TABLE (IP VARCHAR(30))

INSERT INTO @T
SELECT '102.89.93.101'
UNION ALL SELECT '100.100.10.1'
UNION ALL SELECT '123.94.9.121'

SELECT PARSENAME(IP, 4) + '.' +
PARSENAME(IP, 3) + '.' +
PARSENAME(IP, 2)
FROM @T

SELECT LEFT(IP, LEN(IP)-CHARINDEX('.', REVERSE(IP)))
FROM @T





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
VegasL
VegasL
Old Hand
Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)

Group: General Forum Members
Points: 371 Visits: 171
Thanks Dwain.

I tried

select LEFT([IP],len([IP]-charindex('.',reverse(([Ip]))
from [dbo].[TestTable]

but am getting

Incorrect syntax near the keyword 'from'.


[IP] is column name.

Since I'll probably have a list of over 100k I don't think the other options would be practical.
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17411 Visits: 6431
VegasL (11/18/2012)
Thanks Dwain.

I tried

select LEFT([IP],len([IP]-charindex('.',reverse(([Ip]))
from [dbo].[TestTable]

but am getting

Incorrect syntax near the keyword 'from'.


[IP] is column name.

Since I'll probably have a list of over 100k I don't think the other options would be practical.



You need to make sure your parentheses are properly matched:


SELECT LEFT([IP], LEN([IP])-CHARINDEX('.', REVERSE([IP])))
FROM [dbo].[TestTable]





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
BriPan
BriPan
SSC-Addicted
SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)

Group: General Forum Members
Points: 413 Visits: 296
( problem in your query


select
LEFT([IP],len([IP])-charindex('.',reverse(([Ip]))))
from tablename


VegasL
VegasL
Old Hand
Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)Old Hand (371 reputation)

Group: General Forum Members
Points: 371 Visits: 171
Thanks for you're help Brian. That worked. I am now trying to show which IP's are used more than once, so I tried


select LEFT([IP],len([IP])-charindex('.',reverse(([Ip]))))
from tablename
group by [ip]
having count ([ip])>1
order by ([ip]) desc

It does display the [ip] 's used more than once but it is also displaying some that are only used once. Any thoughts?
BriPan
BriPan
SSC-Addicted
SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)

Group: General Forum Members
Points: 413 Visits: 296
your query seems ok and working fine

may be you are selecting LEFT([IP],len([IP])-charindex('.',reverse(([Ip])))) which may leads you to confusion

you can try putting ip in select and find where's the problem

select LEFT([IP],len([IP])-charindex('.',reverse(([Ip])))),ip
from @t
group by [ip]
having count ([ip])>1
order by ([ip]) desc


or else post some sample data in which you are facing problem.
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