Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SUBSTRING / TRIM help? 1

Status
Not open for further replies.

wickyd

Technical User
Feb 14, 2002
57
ZA
Good day folx

I am new to SQL and I need some help with the following:


What is the SQL that will transform

abc(red) 10
abc(black) 20
abc(blue) 15
a(red) 2
a(green) 4
a(orange) 5


into ...


abc 45
a 11

Thank you.

Kind regards
wickyd
 
Code:
SELECT LEFT(col1, CHARINDEX('(', col1) - 1), col2
FROM mytable

--James
 
BTW, this assumes you have the parentheses in every value in the field. If you don't then do a check with:

Code:
SELECT CASE WHEN CHARINDEX('(', col1) <> 0 THEN LEFT(col1, CHARINDEX('(', col1) - 1) ELSE col1 END
...

--James
 
My apologies, I am not using MS SQL server, I am using DBISAM, which doesn't support what you have suggested.

Any pure SQL way to do this? The only LEFT I can use is LEFT OUTER JOIN

Thank you.
 
Maybe in DBISAM the function LEFT is presented like LTRIM or TRIM or so?
 
You could try this:

Code:
SELECT SUBSTRING(col1, 1, CHARINDEX('(', col1) - 1)

I can only really advise on SQL Server as this is what I know (and after all this is a SQL Server forum!)

--James
 
I know this is not the correct place, but I know how helpful this forum is :)

DBISAM does not support CHARINDEX, but it does support TRIM.

Argh, we are getting closer :)
 
If you could provide me with a multiple replace, I could then simply replace each colour string with a null string .. what do you think?

select code, replace(&quot;(red)&quot; with &quot;&quot;)stripedcolour from stock

the replace simply needs to be expanded to include 5 to 10 colours.

 
CHARINDEX and TRIM (in SQL Server at least) do two different things.

CHARINDEX finds an occurance of one string in another. In my query, I use it to find where the first '(' was and just take the first part of the value up to that point (using the LEFT or SUBSTRING function).

LTRIM/RTRIM get rid of leading/trailing spaces from a value. In SQL Server, there is no TRIM function.

It looks like you need to find a function which does the same as the CHARINDEX function. Let us know if you come up with one.

--James
 
You can nest REPLACEs like this:

Code:
SELECT REPLACE(REPLACE(col1, '(red)', ''), '(blue)', '')

But using this seems a horribly ugly solution to your problem, makes maintenance hard (what happens when another colour comes up?) and - most of all - will perform badly.

--James
 
Thank you so much for the pointers

Here goes:

SELECT CODE, SUBSTRING(CODE FROM 1 FOR POS(&quot;(&quot; IN CODE) - 1)STRIPPEDCODE FROM STOCK
WHERE POS(&quot;(&quot; IN CODE) > 0

POS returns the position of one string within another string. I have to use the WHERE clause as DBISAM does not support the CASE element.

The accounting software I use, uses DBISAM. Their support team told me that what I was asking them for was impossible.

Thank you for proving them wrong.
 
Try this ???

SUBSTRING(column_reference or string constant FROM start_index [FOR length])
POS(string constant IN column_reference or string constant)
REPLACE(string constant WITH new string constant IN
column_reference or string constant)

Tip: search on the web 'dbisam susbtring' to find a manual, documentation or examples. I found the above here
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top