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


Derived column


Derived column

Author
Message
komal145
komal145
Say Hey Kid
Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)Say Hey Kid (699 reputation)

Group: General Forum Members
Points: 699 Visits: 1049
i have column value something like below.


{784A4579-8689-438E-ADAA-9DCBC8A88AE7}

I just need "784A4579-8689-438E-ADAA-9DCBC8A88AE7" i.e value between {}

can any one help me with this?

thanks!!
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28718 Visits: 39977
nested replace would probably be the easiest:;
you could substring it as well, because the desired string, including dashes will be 36 chars.


SELECT REPLACE(REPLACE('{784A4579-8689-438E-ADAA-9DCBC8A88AE7}','{',''),'}',''),
SUBSTRING('{784A4579-8689-438E-ADAA-9DCBC8A88AE7}',2,36)



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

dan-572483
dan-572483
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1349 Visits: 1999
If the length of the string might vary - but there will always be brackets - this will work:

declare @string varchar(max)
set @string = '{String you want to remove from the brackets}'
select SUBSTRING(@string, CHARINDEX('{',@string)+1,Charindex('}',@string)-2)


rVadim
rVadim
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1125 Visits: 2306
If this is actually a GUID then you can also do this:



DECLARE @UST VARCHAR(38) = '{784A4579-8689-438E-ADAA-9DCBC8A88AE7}';
SELECT CAST(CAST(@UST AS UNIQUEIDENTIFIER) AS VARCHAR(36));



demonfox
demonfox
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1543 Visits: 1192
Another way to do it using STUFF.


declare @testid varchar(50)=
'{784A4579-8689-438E-ADAA-9DCBC8A88AE7}'

select STUFF(stuff(@testid,len(@testid),len(@testid),''),1,1,'')



~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
gravitysucks
gravitysucks
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 Visits: 232
dan-572483 (8/24/2012)
If the length of the string might vary - but there will always be brackets - this will work:

declare @string varchar(max)
set @string = '{String you want to remove from the brackets}'
select SUBSTRING(@string, CHARINDEX('{',@string)+1,Charindex('}',@string)-2)



Dan,
just out of curiousity-whats the logic behind using '-2' instead of '-1' with charindex.You need to use-2 even if you use len(@string). Whats the logic? I am guessing its because of +1 at the start position-but why? Thanks.
demonfox
demonfox
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1543 Visits: 1192
gravitysucks (8/25/2012)
dan-572483 (8/24/2012)
If the length of the string might vary - but there will always be brackets - this will work:

declare @string varchar(max)
set @string = '{String you want to remove from the brackets}'
select SUBSTRING(@string, CHARINDEX('{',@string)+1,Charindex('}',@string)-2)



Dan,
just out of curiousity-whats the logic behind using '-2' instead of '-1' with charindex.You need to use-2 even if you use len(@string). Whats the logic? I am guessing its because of +1 at the start position-but why? Thanks.


the actual syntax is :
SUBSTRING ( expression ,start , length )

here the length is how many characters of the expression is returned ; i.e the new string length expected.
as CharIndex on } gives the total length of the initial string, -2 is done to get total length of new string.

~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
dan-572483
dan-572483
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1349 Visits: 1999
I realized that if erroneous text were in your column, my code might produce an incorrect result. Here is an improved version:

declare @string varchar(max)
declare @openbracket int
declare @closebracket int
set @string = 'extra{Text You Want}text'
set @openbracket = charindex('{',@string)
set @closebracket = charindex('}',@string)
IF @openbracket >0 and @closebracket >0
BEGIN
select SUBSTRING(@string, @openbracket +1, @closebracket-@openbracket-1) as 'Result'
End
Else
Select 'Invalid Data' as 'Result'


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