0

I wonder why I've got empty string as a result when I'm especting something completely else... I use trim function to cut phone number from string:

select trim(leading  '509960405' from '509960405509960404');

Why the result isn't 509960404 as expected?

4

2 回答 2

2

@Paul clarified the behaviour of trim().
The solution you presented in the comment is potentially treacherous:

SELECT replace('509960405509960404','509960405','')

Replaces all occurrences of '509960405' not just the first. For example:

SELECT replace('509960405509960404','50996040' ,'');

Results in 54. I suspect that's not what you want.
Use a regular expressions with regexp_replace():

SELECT regexp_replace('509960405509960404','^509960405' ,'');

^ .. glues the pattern to the start of the string ("left-anchored").
regexp_replace() is more expensive than a simple replace() but also more versatile.

于 2013-04-30T21:30:49.627 回答
2

trim去除与字符列表匹配的任何字符。字符串中的所有字符都在“前导”字符列表中。你写的东西可以很容易地写成

select trim(leading '04569' from '509960405509960404');

它从字符串的开头删除任何 0、4、5、6 或 9 个字符。由于您的字符串仅包含 0、4、5、6 或 9 个字符,因此会将它们全部删除。

于 2013-04-30T16:43:58.017 回答