[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

trim (....)

Posted on 2014-08-20
7
Medium Priority
?
246 Views
Last Modified: 2014-08-24
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
0
Comment
Question by:fsze88
7 Comments
 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40275598
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
 
LVL 6

Expert Comment

by:Mandeep Singh
ID: 40275604
Hi
try this

select SUBSTRING('abc(def)',1,3)
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 40275642
or you could write:

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

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

Open in new window

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 15

Author Comment

by:fsze88
ID: 40275888
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
 
LVL 32

Expert Comment

by:awking00
ID: 40276185
>>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
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40276892
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
 
LVL 32

Expert Comment

by:awking00
ID: 40276935
>>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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question