RSS

Using SubString,Left Right and Reverse in SQL Server


DECLARE @IP Varchar(20)
SET @IP='10.161.105.148'
SELECT SUBSTRING(@IP,1,LEN(@IP)-4)
-- This is get the Sub String of the IP

-- Result
--100.1601.105

-- Using Left in Sql Server

SELECT LEFT(@IP,LEN(@IP)-4)
SELECT RIGHT(@IP,LEN(@IP)-4)

Result
-----
10.161.105
61.105.148

DECLARE @MachineIP NVARCHAR(20)
SET @MachineIP='10.2.15.132'
-- Bellow Query is used to Reverse the given String
SELECT Reverse(@MachineIP)
-- This used to get First Three Part of the IP Address
SELECT SUBSTRING(@MachineIP ,1 ,Len(@MachineIP)-(CHARINDEX('.',Reverse(@MachineIP),-1)))

/*------ Result ------
231.51.2.01
10.2.15
---------------------- */

A Simple Example For Group By Having

Create table #temp(name varchar(20))
insert into #temp values('Siva')
insert into #temp values('Raghu')
SELECT Name +' - ' + Cast(count(Name)As varchar) FROM #temp Group by Name
--Having Name='siva'
drop table #temp

Result:
Babu - 2
Raghu - 1
Siva - 2

1 comments:

Unknown said...

Hello There,

Nice tutorial! Let's keep our fingers crossed that this works. I would like to put this all to rest.
Need some help in resolving this dynamic value of either 8 or 9 to DIV(),

Its for the same element, differing only at DIV parameter value, as shown below.. as and when the application is launched using ie 11 and BP 6.0 versions.

/HTML/BODY(1)/DIV(8)/main(1)/DIV(1)/DIV(2)/FORM(1)/FIELDSET(2)/DIV(2)/DIV(1)/LABEL(2)/SPAN(1)

/HTML/BODY(1)/DIV(9)/main(1)/DIV(1)/DIV(2)/FORM(1)/FIELDSET(2)/DIV(2)/DIV(1)/LABEL(2)/SPAN(1)

Couldn't really get through on which occasions its coming as 8 or 9.

Can someone help me understand how to tackle this?
How to handle dynamic DIV values?

Appreciate your time and help on this.

Anyways great write up, your efforts are much appreciated.

Thank you,
Madhu

Post a Comment