trim (....)

Hi,
I have a column value is abc(def)

but I want to get column value abc

select col from t
return abc(def)
select fun(col) from t
return abc
LVL 15
fsze88Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

this can be done multiple ways

SELECT 

LEFT(Col,3),

SUBSTRING(Col,1,3),

SUBSTRING(Col,1,CHARINDEX('(',Col,1)-1) from t

Open in new window

0
Mandeep SinghDatabase AdministratorCommented:
Hi
try this

select SUBSTRING('abc(def)',1,3)
0
Habib PourfardSoftware DeveloperCommented:
or you could write:

DECLARE @Text VARCHAR(MAX) = 'abc(ad)'

SELECT SUBSTRING(@Text,0, CHARINDEX('(',@Text))

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

fsze88Author Commented:
I would like to remind.
in case of

DECLARE @Text VARCHAR(MAX) = 'abc'

SELECT SUBSTRING(@Text,0, CHARINDEX('(',@Text))


It would also return 'abc'

Thanks
0
awking00Commented:
>>select fun(col) from t
return abc<<
Can you explain how you would expect to return abc? Did you mean return fun?
Are you trying to get the column value without the parentheses and whatever is in between them? If so, could you have values like abc(def)ghi where you want to return abcghi or just the abc?
0
Scott PletcherSenior DBACommented:
Sounds like you want a function to do this.  Here it is, with sample uses:


CREATE FUNCTION dbo.GetLeadingString (
    @string varchar(8000),
    @delimiter char(1) = '('
)
RETURNS varchar(8000)
AS
BEGIN
RETURN (
    SELECT LEFT(@string, CHARINDEX(@delimiter, @string + @delimiter) - 1)
)
END --FUNCTION
GO


SELECT string, dbo.GetLeadingString ( string, '(' )
FROM (
    SELECT 'abc(def)' AS string UNION ALL
    SELECT 'abc'
) AS test_data

SELECT string, dbo.GetLeadingString ( string, '[' )
FROM (
    SELECT 'abcd[ef]g' AS string UNION ALL
    SELECT 'abcd'
) AS test_data
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Commented:
>>I would like to remind in case of
DECLARE @Text VARCHAR(MAX) = 'abc'
SELECT SUBSTRING(@Text,0, CHARINDEX('(',@Text))
It would also return 'abc'<<

CASE WHEN CHARINDEX('(',@Text) = 0 THEN @Text
           ELSE SUBSTRING(@Text,0, CHARINDEX('(',@Text))
END
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.