Xyz_100
Xyz_100

Reputation: 57

Remove leading dash from a string

I have this:

-asdfj-
-jhbjbhk---kjn
klsjdn-kad-.d-.--.-ask-n-
-sd-kasd-ksjd-nkasjd

I need this:

asdfj-
jhbjbhk---kjn
klsjdn-kad-.d-.--.-ask-n-
sd-kasd-ksjd-nkasjd

Whenever I have a dash in the first place of the string, I want it to be deleted and whatever is at the second position should take its place (i.e. the first place).

Upvotes: 0

Views: 1113

Answers (2)

saji89
saji89

Reputation: 2251

You can use LTRIMor TRIM string functions:

e.g:

SELECT LTRIM(column_name, '-') FROM table_name;
SELECT TRIM(LEADING '-' FROM column_name) FROM table_name;

You can find the interactive code, with example data: http://sqlfiddle.com/#!15/28e115/6

For more details refer:

Upvotes: 4

user330315
user330315

Reputation:

You can use regexp_replace() for that:

select regexp_replace(the_column, '^-', '')
from the_table;

The regular expression '^-' means exactly one - at the beginning of the string.


If you want to change the data in the table permanently, you can use that in an UPDATE statement:

update the_table
    set the_column = regexp_replace(the_column, '^-', '');
where the_column ~ '^-';

The where clause makes sure only those rows are updated where something is changed.

Upvotes: 2

Related Questions