0

How can I extract part of this string

declare @string varchar(1024)
set @string = 'Total # of bytes             : 128270200832 (119.46GB)'
select Substring (@string, Charindex( ':', @string )+2 , Len(@string))

I only need the numbers after: and without the (119.46GB)

0

3 Answers 3

1

Use replace() :

select replace(replace(Substring (@string, Charindex( ':', @string )+2 , Len(@string)), '(', ''), ')', '');

EDIT : If you don't want values inside () then you can do :

select left(col, charindex('(', col) - 1) 
from ( values (Substring (@string, Charindex( ':', @string )+2 , Len(@string))) 
     ) t(col);
Sign up to request clarification or add additional context in comments.

1 Comment

sorry if i wasn't clear.. i don't need this as well (119.46GB)
0

Seems like STUFF with CHARINDEX and a couple of replaces does the job:

DECLARE @string varchar(1024);
SET @string = 'Total # of bytes             : 128270200832 (119.46GB)';
SELECT REPLACE(REPLACE(STUFF(@string,1,CHARINDEX(':',@string)+1,''),'(',''),')','');

Edit: Based on the new logic you have defined:

SELECT SUBSTRING(@String,V.CI,CHARINDEX('(',@String,V.CI) - V.CI)
FROM (VALUES(CHARINDEX(':',@string)+1))V(CI);

3 Comments

sorry if i wasn't clear.. i don't need this as well (119.46GB)
You just said "without the ()", @Avi . As you can see, this meant to both myself and Yogesh that you meant remove the () characters. You should update your question to be clear.
Are you saying the goal posts have moved again, @Avi ?
0

Try this:

declare @string varchar(1024)
set @string = 'Total # of bytes             : 128270200832 (119.46GB)'
select substring(@string,charindex(':',@string) + 2,(charindex('(',@string) - charindex(':',@string)-2))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.