How can I remove any white space from substring of string?
For example I have this number '+370 650 12345'. I need all numbers to have this format country_code rest_of_the_number or in that example: +370 65012345. How could you achieve that with PostgreSQL?
I could use trim() function, but then it would remove all whitespace.
Assuming the column is named phone_number:
left(phone_number, strpos(phone_number, ' '))
||regexp_replace(substr(phone_number, strpos(phone_number, ' ') + 1), ' ', '', 'g')
It first takes everything up to the first space and then concatenates it with the result of replacing all spaces from the rest of the string.
If you also need to deal with other whitespace than just a space, you could use '\s' for the search value in regexp_replace()
If you are able to assume that a country code will always be present, you could try using a regular expression to capture the parts of interest. Assuming that your phone numbers are stored in a column named content in a table named numbers, you could try something like the following:
SELECT parts[1] || ' ' || parts[2] || parts[3]
FROM (
SELECT
regexp_matches(content, E'^\\s*(\\+\\d+)\\s+(\\d+)\\s+(\\d+)\\s*$') AS parts
FROM numbers
) t;
The following will work even if the country code is absent (see SQL Fiddle Demo here):
SELECT TRIM(REPLACE(REPLACE(REGEXP_REPLACE('+370 650 12345', '^((\+\d+)\s+)?(.*)$', '\1|\3'), ' ', ''), '|', ' '));
Returns: +370 65012345
SELECT TRIM(REPLACE(REPLACE(REGEXP_REPLACE('370 650 12345', '^((\+\d+)\s+)?(.*)$', '\1|\3'), ' ', ''), '|', ' '));
Returns: 37065012345
It looks for a country code (a set of numbers starting with a + sign) at the beginning, and replaces any whitespace following that code with a pipe |. It then replaces all the spaces in the resulting string with the empty string, then replaces occurrences of | with spaces! The choice of | is arbitrary, I suppose it could be any non-digit character.
Related
I have column with software versions. I was trying to remove dot from 2nd occurrence in column, like
select REGEXP_REPLACE('12.5.7.8', '.','');
expected out is 12.578
sample data is here
Is it possible to remove dot from 2nd occurrence
One option is to break this into two pieces:
Get the first number.
Get the rest of the numbers as an array.
Then convert the array to a string with no separator and combine with the first:
select (split_part('12.5.7.8', '.', 1) || '.' ||
array_to_string((REGEXP_SPLIT_TO_ARRAY('12.5.7.8', '[.]'))[2:], '')
)
Another option is to replace the first '.' with something else, then get rid of the '.'s and replace the something else with a '|':
select translate(regexp_replace(version, '^([^.]+)[.](.*)$', '\1|\2'), '|.', '.')
from software_version;
Here is a db<>fiddle with the three versions, including the version a_horse_with_no_name mentions in the comment.
I'd just take the left and right:
concat(
left(str, position('.' in str)),
replace(right(str, -position('.' in str)), '.', '')
)
For a str of 12.34.56.78, the left gives 12. and the right using a negative position gives 34.56.78 upon which the replace then removes the dots
I am a bit new to this site but I have looked an many possible answers to my question but none of them has answered my need. I have a feeling it's a good challenge. Here it goes.
In one of our tables we list what is used to run a report this can mean that we can have a short EXEC [svr1].[dbo].[stored_procedure] or "...From svr1.dbo.stored_procedure...".
My goal is to get the stored procedure name out of this string (column). I have tried to get the string between '[' and ']' but that breaks when there are no brackets. I have been at this for a few days and just can't seem to find a solution.
Any assistance you can provide is greatly appreciated.
Thank you in advance for entertaining this question.
almostanexpert
Considering the ending character of your sample sentences is space, or your sentences end without trailing ( whether space or any other character other than given samples ), and assuming you have no other dots before samples, the following would be a clean way which uses substring(), len(), charindex() and replace() together :
with t(str) as
(
select '[svr1].[dbo].[stored_procedure]' union all
select 'before svr1.dbo.stored_procedure someting more' union all
select 'abc before svr1.dbo.stored_procedure'
), t2(str) as
(
select replace(replace(str,'[',''),']','') from t
), t3(str) as
(
select substring(str,charindex('.',str)+1,len(str)) from t2
)
select
substring(
str,
charindex('.',str)+1,
case
when charindex(' ',str) > 0 then
charindex(' ',str)
else
len(str)
end - charindex('.',str)
) as "Result String"
from t3;
Result String
----------------
stored_procedure
stored_procedure
stored_procedure
Demo
With the variability of inputs you seem to have we will need to plan for a few scenarios. The below code assumes that there will be exactly two '.' characters before the stored_procedure, and that [stored_procedure] will either end the string or be followed by a space if the string continues.
SELECT TRIM('[' FROM TRIM(']' FROM --Trim brackets from final result if they exist
SUBSTR(column || ' ', --substr(string, start_pos, length), Space added in case proc name is end of str
INSTR(column || ' ', '.', 1, 2)+1, --start_pos: find second '.' and start 1 char after
INSTR(column || ' ', ' ', INSTR(column || ' ', '.', 1, 2), 1)-(INSTR(column || ' ', '.', 1, 2)+1))
-- Len: start after 2nd '.' and go until first space (subtract 2nd '.' index to get "Length")
))FROM TABLE;
Working from the middle out we'll start with using the SUBSTR function and concatenating a space to the end of the original string. This allows us to use a space to find the end of the stored_procedure even if it is the last piece of the string.
Next to find our starting position, we use INSTR to search for the second instance of the '.' and start 1 position after.
For the length argument, we find the index of the first space after that second '.' and then subtract that '.' index.
From here we have either [stored_procedure] or stored_procedure. Running the TRIM functions for each bracket will remove them if they exist, and if not will just return the name of the procedure.
Sample inputs based on above description:
'EXEC [svr1].[dbo].[stored_procedure]'
'EXEC [svr1].[dbo].[stored_procedure] FROM TEST'
'svr1.dbo.stored_procedure'
Note: This code is written for Oracle SQL but can be translated to mySQL using similar functions.
I have strings in one of my table in which I need to replace some specials characters like ' _ ? ° and square brackets [ ].
When I try this it's working like expected :
SELECT REGEXP_REPLACE('BIG''EAST_?°[]', '[_?°'']', ' ') FROM DUAL;
I get:
BIG EAST []
Then I add the square brackets in my regex :
SELECT REGEXP_REPLACE('BIG''EAST_?°[]', '[_?°''\[\]]', ' ') FROM DUAL;
I expected this:
BIG EAST
But I get:
BIG'EAST_?°
How can I properly escape the square brackets in my regex?
You need to add a * to match multiple occurrences (and in any order) of characters from your pattern
SELECT REGEXP_REPLACE('BIG''EAST_?°[]', '[_?°''\[\]]*', ' ') FROM DUAL;
Can someone please help me to understand the following code:
REPLACE(LTRIM(RTRIM(dbo.UFN_SEPARATES_COLUMNS(CompletionDetails, 3, ','))), '.', '') AS BuildRequestID,
Does it say remove all trailing and leading spaces, then replace 3 with comma. Next, if there is ., replace it with ' '?
It does not at any point replace 3 with ,.
We can make all this easier to follow by formatting the full expression to cover multiple lines:
REPLACE(
LTRIM(RTRIM(
dbo.UFN_SEPARATES_COLUMNS(CompletionDetails, 3, ',')
))
,'.', ''
) AS BuildRequestID,
Expressions like this have to read from the inside out. So we start with this inner-most part:
dbo.UFN_SEPARATES_COLUMNS(CompletionDetails, 3, ',')
This UFN_SEPARATES_COLUMNS() function is not part of Sql Server, but was added by someone at your organization or as part of the vendor software package for the database you're looking at. But I'm confident based on inferences and the link (found via Google) it will treat CompletionDetails as delimited text, where the delimiter is a comma (based on the 3rd ',' argument) and returns the 3rd field (based on the 2nd 3 argument, where counting starts at 1 rather than 0). As CSV parsers go, this one is particularly naive, so be very careful what you expect from it.
Then we use LTRIM() and RTRIM() to remove both leading and trailing blanks from the field. Not all whitepsace is removed; only space characters. Tabs, line feeds, etc are not trimmed. Sql Server 2017 has a new TRIM() function that can handle wider character sets and do both sides of the string with one call.
The code then uses the REPLACE() function to remove all . characters from the result (replaces them with an empty string).
The code is trimming the leading and trailing spaces via the LTRIM() and RTRIM() functions of whatever is returned from the function dbo.x_COLUMNS... i.e. dbo.x_COLUMNS(CompletionDetails, 3, ','). LTRIM is left, RTRIM is right.
It then is replacing all periods (.) with nothing via the REPLACE() function.
So in summary, it's removing all periods from the string and the leading and trailing spaces.
The LTRIM removes leading spaces. RTRIM removes trailing spaces. REPLACE removes the period.
Declare #Val Char(20) = ' Frid.ay '
Select REPLACE(
LTRIM(
RTRIM(
#Val --dbo.x_COLUMNS(CompletionDetails, 3, ',')
)
), '.', ''
)
Result
BuildRequestID
--------------
Friday
remove all trailing and leading spaces, then replace 3 with comma.
Next, if there is ., replace it with ' '
No it does not say that.
But this does:
REPLACE(REPLACE(LTRIM(RTRIM(CompletionDetails)), '3', ','), '.', ' ')
it's not clear if you want . replaced by ' ' or ''.
I used the 1st case, you can change it as you like.
It's easier to understand like this:
remove all trailing and leading spaces: LTRIM(RTRIM(CompletionDetails))
replace 3 with comma: REPLACE( ?, '3', ',')
replace it with ' ': REPLACE(? , '.', ' ') or REPLACE(? , '.', '')
The string contains many words separated by spaces e.g.
employee_first_nm = "John Walker"
I want to retrieve the first part alone ("John"). this part I have done using the following code:
SUBSTR(employee_first_nm, 1, INSTR(employee_first_nm, ' '));
In some cases the string has only one word e.g. "sonia", this is where I got a problem. Here if there is only one word the function doesn't retrieve any value at all. But I want it to get the full string in this case i.e. "sonia".
Please help
Simply ensure there always will be a space;
... INSTR(employee_first_nm + ' ', ' ')
If there is already a space in the string then stuffing another one on the end makes no difference as the 1st one will be found, if there is no existing space adding one makes your logic work.
(Usually you also need to INSTR(..)-1 to strip the trailing space)
SUBSTR(employee_first_nm, 1, INSTR(CONCAT(employee_first_nm,' '), ' '));
Alternatively, you could check that INSTR returns a correct value:
(case when INSTR(employee_first_nm, ' ') > 0
then SUBSTR(employee_first_nm, 1, INSTR(employee_first_nm, ' '))
else employee_first_nm
end)
Why not harness the power of regular expressions here.
REGEXP_SUBSTR(employee_first_nm, '^[a-zA-Z]*\s?')
This should return the substring you desire.
You can try:
SUBSTRING(select employee_first_nm,0, charindex(' ',employee_first_nm)
Tested with:
select substring('John walker',0, charindex(' ','John walker more'))
returns -> 'John'