June 13, 2012 at 11:13 am
I have the below field...
servername\instance
I want to be listed as two separate fields in my report
servername instance
How do I make these two separate fields? Substring?
June 13, 2012 at 11:50 am
Can use this to make seperate fileds...
select substring(name,1, len(name)-(charindex('\',reverse(name))))
,substring(name,len(name)-(charindex('\',reverse(name)))+2,len(name))
from instancename
Can use this to be same field replacing \ with ' '...
select Replace(name,'\',' ')
from instancename
John
June 13, 2012 at 11:55 am
a neat trick with the PARSENAME function, which is used to chop up object names like ServerName.DatabaseName.SchemaName.ObjectName:
--Results:
/*
ServerName Instance
----------- --------
MyServer SQL2005
*/
SELECT
PARSENAME(Replace(instancename,'\','.'),2) AS ServerName,
PARSENAME(Replace(instancename,'\','.'),1) AS Instance
from(SELECT 'MyServer\SQL2005' AS instancename) x
Lowell
June 13, 2012 at 12:05 pm
The following will also work:
declare @TestString varchar(32) = 'servername\instance';
select
left(@TestString,charindex('\', @TestString) - 1),
right(@TestString, len(@TestString) - charindex('\', @TestString));
June 13, 2012 at 12:06 pm
Great they both worked...thank you everyone!
June 13, 2012 at 10:53 pm
Lowell (6/13/2012)
a neat trick with the PARSENAME function, which is used to chop up object names like ServerName.DatabaseName.SchemaName.ObjectName:
--Results:
/*
ServerName Instance
----------- --------
MyServer SQL2005
*/
SELECT
PARSENAME(Replace(instancename,'\','.'),2) AS ServerName,
PARSENAME(Replace(instancename,'\','.'),1) AS Instance
from(SELECT 'MyServer\SQL2005' AS instancename) x
Lowell - That is really slick!
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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
July 1, 2012 at 5:43 pm
doesnt work for a list of named and default instances tho
July 2, 2012 at 7:20 pm
DECLARE @SomeString VARCHAR(20)
SET @SomeString = 'ServerName\Instance'
If (SELECT LEN(@SomeString) - LEN(REPLACE( @SomeString,'\','')))>0
SELECT LEFT(@SomeString,CHARINDEX('\',@SomeString)-1), right(@SomeString, len(@SomeString) - charindex('\', @SomeString));
ELSE
SELECT @SomeString
July 2, 2012 at 9:03 pm
Thanks
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply