1

Say I have a string looking like this ",LI,PA,LK"; I want to remove the first char, so it looks like "LI,PA,LK";

In Java my code to handle this, will look like this:

public String returnSubs(String val) {

    int index = val.indexOf(",");       
    String res = val.substring(index+1, val.length());

    return res;
}

I want to achieve the exact same thing in SQL, having this query

    select patientID, case when liver is not null then 'LI' else '' end 
         || case when kidney_r is not null then ',KR' else '' end
         || case when kidney_l is not null then ',KL' else ''end
         || case when heart is not null then ',HE' else '' end
         || case when liver_domino is not null then ',LI-Dom' else '' end
         || case when lung_r is not null then ',LungR' else '' end
         || case when pancreas is not null then ',PA' else '' end
         || case when liver_split is not null then ',Lsplit' else '' end
         || case when lung_l is not null then ',LungL' else '' end
         || case when intestine is not null then ',Intestine' else '' end
         into organType                       
from offers
where patientID > 1
; 

Also, the string I get from the query above, could look like LI, PA, KL, (notice the comma is at the end, and not the begining)

I see that I can use the SUBSTRING and/or INSTR of SQL. But I'm not really sure how. I am creating a procedure where this will be handled

Thanks for any help

4 Answers 4

2

Oracle has a function trim() that does exactly what you want:

trim(leading ',' from col)

You can use this in either an update or select.

Note: You appear to be storing multiple values in a comma-delimited list. That is a very bad way to model data. You do not want to overload what strings are by storing multiple values. Oracle has many better alternatives -- association tables, nested tables, JSON, and XML come to mind.

Sign up to request clarification or add additional context in comments.

Comments

1

You could also use LTRIM here:

SELECT
    LTRIM(organTypes, ',') AS col_out
FROM offers;

Some databases, such as MySQL, offer functions like CONCAT_WS which concatenate with a separator while ensuring that no dangling separators are added to the resulting output. Oracle does not have this, but LTRIM should be sufficient here.

Comments

1

even this will work:

 substr(',LI,PA,LK',2)

Comments

0

In SQL SERVER:

SUBSTRING(VAL,2,LEN(VAL))

VAL--> COLUMN NAME
2--> IT SKIPS 1ST VALUE
LEN-->LENGTH OF THE COLUMN 

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.