I have some values in my data that have leading spaces in the string character. Looks like when the data was inputted someone left a space by mistake. How can I remove that leading space? I would assume it would be a regex solution.
You can use Bigquery TRIM function to remove spaces on a STRING value, example :
SELECT
' Original String_',
TRIM(' Original String_') AS trimmed,
LTRIM(' Original String_') AS left_trim,
RTRIM(' Original String_', "_") AS right_trim
TRIM removes left and right spaces
LTRIM removes only left spaces
RTRIM removes only right spaces
Related
I try to remove all whitespaces inside a string. For this, I use TRIM() function. Unfortunately it doesn't work as expected, inner whitespaces (between 35 and 'A') remain untouched:
select TRIM('Hopkins 35 A Street') as Street
Column type is nvarchar. The funny thing is that this function works fine (using example from above) when executed on W3Schools (TRIM function example): https://www.w3schools.com/sql/func_sqlserver_trim.asp.
I can use replace on this string and replace ' ' into '' without a problem. I work on SQL Server 18.7.1 (2020)
if you use TRIM like this you are only removing leading and trailing spaces from a string. To remove also spaces in between you should change to:
select TRIM(' ' FROM 'Hopkins 35 A Street') as Street
UPDATE: if you meant to remove all spaces you should use
SELECT REPLACE('Hopkins 35 A Street', ' ', '')
TRIM is only intended to make a double space become a single one
"Trimming" means the removal of whitespace from the start and/or the end of a string value. It never means (and has never meant to mean) the removal of whitespace within a string value (enclosed by non-whitespace characters).
You can indeed use the TRIM function with a FROM in its argument to specify other characters than whitespace to trim. In that case, the TRIM function will remove the specified characters from the start and the end of the string, but not within the string (enclosed by other characters).
In other words: the specified characters will be treated as if they were whitespace as well, but specifying them so will not affect the trimming behavior/algorithm itself.
Check out the sample on Microsoft Docs:
SELECT TRIM( '.,! ' FROM ' # test .') AS Result;
produces this result: # test
TRIM function will only remove only the leading and tailing spaces in the data. It cannot remove all the spaces in the data. I mean it cannot remove all the spaces if there are any spaces in the data like 'Hello World'. TRIM cannot remove the space between the word Hello and World and make it look like 'HelloWorld'. If you want to remove all the spaces, you can use the REPLACE function. In the REPLACE function you can replace the space with any character/number/symbol. If you don't need any you can simply remove the space with ''. like
SELECT REPLACE('Hopkins 35 A Street', ' ', '')
I'm trying to remove few special characters from a comment column in my table. I used the below statement but it seems to remove the ']' even though it is in the ^[not] list.
UPDATE TEST
set comments=REGEXP_REPLACE(
comments,
'[^[a-z,A-Z,0-9,[:space:],''&'','':'',''/'',''.'',''?'',''!'','']'']]*',
' '
);
The table data contains the following:
[SYSTEM]:Do you have it in stock? 😊
My requirement is to have:
[SYSTEM]:Do you have it in stock?
You have two mistakes in you regex:
Do not put characters in quotes and don't split them with comma.
Remove inner square brackets.
And place closing square brackets first in the list, just after initial circumflex. Fixed regex:
UPDATE TEST set comments=REGEXP_REPLACE(comments,'[^]a-zA-Z0-9[:space:]&:/.?!]*',' ');
My try, I just removed the commas, put the "accepted" characters after the initial "not"(no brackets).
A special case are the brackets: https://dba.stackexchange.com/a/109294/6228
select REGEXP_REPLACE(
'[ION] are varză murată.',
'[^][a-zA-Z0-9[:space:]&:/,.?!]+',
' ')
from dual;
Result:
[ION] are varz murat .
Here's my sql :
V_FORMAT_VAL := REPLACE( TRIM(IN_CATS_XY) , ' ' , '' );
But this is an different format , than what is shown here :
TRIM( [ LEADING | TRAILING | BOTH [ trim_character ] string1 )
I don't understand the code above. What is the point of doing a REPLACE if TRIM already takes care of spaces ?
TRIM only deals with spaces at the start and end of the string. In truth there is no point in doing the TRIM since REPLACE will replace all spaces throughout the string, the most efficient way would be:
V_FORMAT_VAL := REPLACE(IN_CATS_XY, ' ');
As Replace will remove the string by default if no replacement string is offered.
TRIM removes spaces from the beginning and end, then REPLACE removes any remaining spaces that were in the string.
Trim takes care of spaces at the start and end of the string, or both. The replace is there to get rid of any spaces that appear inside of the string.
Basically, it would take a string like:
one two three
and turn it into
onetwothree
while also eliminating starting and end spacing.
trim() eliminates the spaces at the beginning and at the end of a string. replace() removes all spaces.
Actually I think trim() is useless in this snippet.
just try this:
select '>'||trim(' a nice string ')||'<' from dual ;
and this
select '>'||replace(' a nice string ', ' ')||'<' from dual ;
and see the differences.
I am writing a sql, and I am using regexp_replace function for this.
My aim is to replace the characters like '|', '\' etc with '-'.
The problem which I am facing is that it replaces the '+', which is at the beginning.
For eg:
The phone number is: +49 |0| 941 78878544
I have to repalce the '|' with '-'.
My code is this:SELECT regexp_replace(phone,'\D','-') FROM PHONE_TBL WHERE EMPLID = employee;
I get the output as: -49--0--941-78878544
This code replaces the space , along with the '+' in the beginning.
I want the '+' to remain, if it is there in the beginning, and if phone numbers have spaces among them, that also should remain.
For '+', I have figured out i should match the beginning of the string, then,must check for non numeric digit, and then escape, but not able to code.
And for space in between, similar approach.
Any help in this, thanks.
If you want to replace all instances of '\' and '|' with '-', use the following:
SELECT REGEXP_REPLACE(phone,'[\|\]','-')
FROM phone_tbl
WHERE emplid = employee;
The square brackets define a set of characters to match. The '\|' means match a pipe character; the '\' means match a backslash character. If you want to replace more characters other than backslash and pipe then you add them between the square brackets.
This is actually a (not very elegant) solution to a problem I had trying to return a float in to a varchar column when doing a union from a dummy_table with some fudged data totals.
In order to get the float to a string, I used the following function in SQL server 2008 (which I found on SO):
str(sum(<float_column>),25,5)
This left lots of leading spaces and trailing zeroes, which was undesirable.
I used the following strings to test the formatting with a dummy table (note that there are 24 preceding spaces that don't show):
' 8.50000'
' 0.50000'
' 8.00000'
I used the following functions to get the desired format:
select replace(rtrim(replace(replace(rtrim(replace(ltrim(
' 8.0000'),'0',' ')),' ','0'),'.',' ')),' ','.') from dummy_table;
This works in the following order:
LTRIM trims empty space to the left.
Replace all zeroes with empty spaces.
RTRIM trims these empty spaces from the right.
Replace all spaces with zeroes.
Replace the dot with space.
Trim all space from the right (to get rid of trailing dot if exists).
Replace spaces with dots (to put dot in the correct place if exists).
If you guys can think of a nicer way to do it, let me know!
round should work for this.
select round(8.5000000000,5)
http://sqlfiddle.com/#!3/1fa93/8845