Replace some parts of string and update the column in STANDARD SQL - sql

I've a year column in a table called 'Movie', some of the year values are not in the standard format(few of them are like 'I 1999', 'XV 199', etc.(space is common in those years)). So I wanted to remove ' ', 'X', 'V', 'I' and replace them with '' so that I can convert them to the standard format of the year.
I was able to do the replacing part by this command:
SELECT (REPLACE(REPLACE(REPLACE((REPLACE(year, ' ', '')),'X',''),'V',''),'I','')) FROM Movie
But I'm unable to update my column(Getting error, command not executed). Below is the command I tried.
UPDATE Movie
SET year = (REPLACE(REPLACE((REPLACE(year, ' ', '')),'X',''),'V',''),'I',''))
WHERE year LIKE '% %'
Please let me know how can I update my 'year' column. Please note I can only use STANDARD SQL commands.
Update: Attached the screenshot of the error. I am executing the SQL commands using pandas.read_sql_query() method. All standard SQL commands works nicely. But error encountered in this case.please note I've removed extra parenthesis.

You are overusing parentheses, so something does not match.
Use this:
SET year = REPLACE(REPLACE(REPLACE(REPLACE(year, ' ', ''), 'X', ''), 'V', ''), 'I', '')

Related

Salesforce Marketing cloud Automation to remove leading zeros

I have a text field with data- 00000001000 and -00000001000
I am trying to remove all the leading zeros from string field.
I tried using REPLACE function:
REPLACE(LTRIM(REPLACE([dollarbalance], '0', ' ')), ' ', '0')
...but it is not giving expected result.
In Query Studio query runs but in Automation it is failing. Also in Query Studio minus sign is not getting ignored and I am getting the same result for value -00000001000
Thanks
I tried
REPLACE(LTRIM(REPLACE([dollarbalance], '0', ' ')), ' ', '0')
and
substring(DOLLARBALANCE, patindex('%[^0]%',DOLLARBALANCE),5)
Since you haven't provided any details about the datatypes inbound or outbound, I'd suggest experimenting outside of SFMC, since this really isn't a question specific to the platform.
declare #dollarbalance varchar(50) = '- 000000000'
select
#dollarbalance dollarbalanceRaw
, REPLACE(LTRIM(REPLACE(#dollarbalance, '0', ' ')), ' ', '0') dollarbalanceTemp
, convert(decimal(19,2),#dollarbalance) dollarbalanceDec
, convert(decimal(19,0),#dollarbalance) dollarbalanceNoDec
dollarbalanceRaw
dollarbalanceTemp
dollarbalanceDec
dollarbalanceNoDec
- 000000000
-0000000000
0.00
0
Try it yourself: fiddle
Reference:
Convert function

SQL SELECT WHERE without underscore and more

I want to select where 2 strings but without taking
underscore
apostrophe
dash..
Hello !
I want to select an option in my SQL database who look like this :
Chef d'équipe aménagement-finitions
With an original tag who look like this
chef-déquipe-aménagement-finitions
Some results in database had a - too
SELECT *
FROM table
WHERE REPLACE(name, '-', ' ') = REPLACE('chef-déquipe-aménagement-finitions', '-', ' ')
didnt work because of missing '
And a double replace didn't work too.
I want the string be able to compare without taking
underscore
apostrophe
dash
and all things like that
is this possible ?
Thanks for your help
Have good day !
Depends on your rdbms, but here's how I would perform in MySQL 8. If using a different version or rdbms, then first determine how to escape the single quote and modify as needed.
with my_data as (
select 'Chef d''équipe aménagement-finitions' as name
)
select name,
lower(replace(replace(name, '\'', ''), ' ', '-')) as name2
from my_data;
name
name2
Chef d'équipe aménagement-finitions
chef-déquipe-aménagement-finitions
Sql-server and Postgres version:
lower(replace(replace(name, '''', ''), ' ', '-')) as name
After posting, this, I re-read and noticed you are also looking to replace other characters. You could either keep layering the replace function, or, look into other functions.

How to remove space from phone number (SQL)

I have phone numbers in the following format:
03 12345678 and 0412 3456789
I need to remove the space from the numbers so that I can join to another table where number format is 0312345679 and 04123456789. I do not want to update the table.
I have tried to run the following query for the home number format, but keep getting an error:
SELECT
REPLACE(p.Home_Phone_Num, ' ', '') AS Home_Num
FROM table
The error:
Syntax error: expected something between the 'SELECT' keyword and the 'REPLACE' keyword.
Thanks
This looks like a Teradata error message. This database does not have a replace() function - instead, you need oreplace():
select oreplace(p.Home_Phone_Num, ' ', '') as Home_Num from mytable
To remove single characters there's no need for oReplace, use oTranslate instead:
oTranslate (p.Home_Phone_Num, ' ', '') AS Home_Num
This might also replace additional characters
oTranslate (p.Home_Phone_Num, ' -/()', '') AS Home_Num

Replacing a space-looking character in SQL Server

I have a database backup that I restored in SQL Server 2014 and I am using SQL Server Management Studio 18 (15.0.18206.0)
In a column of type text strings of XML files are stored. Yes, I know that the datatype text is obsolete. But I cannot change the datatype in the old program so easily.
Now I want to remove the string <SCHNITTLINIE>.
In the cell, it looks like this: ..._MIN> <SCHNITTLINIE> <S...
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), '<SCHNITTLINIE>', '') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITTLINIE%'.
As a result, I get: ...MIN> <S...
So far so good.
I'd like to remove the "spaces" as well.
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), ' <SCHNITTLINIE> ', '') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITTLINIE%'
Now he finds the string ' ' but not anymore.
At the beginning of the cell is a <?xml version="1.0". So I tried the following to test the spaces:
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), 'xml ', 'xxx') AS TEXT)
FROM .[dbo].[XMLdatei].
WHERE xml LIKE '%SCHNITT%'.
As a result I got the following: <?xxxversion="1.0"
The following also works:
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), 'xml' + char(32), 'xxx') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITT%'
Check, the test was successful. :)
Only with the string ' <SCHNITTLINIE> ' this does not work.
How can I find out what the characters before < or after > are?
How can I remove them without knowing what the characters are?
Thanks for your help.
Have a nice weekend.
Christoph

REPLACE not working on sql table column

I've tried select REPLACE(' this is a user name', ' ', '') and it gives me 'thisisausername' which is supposed to be.
My problem is, when I try to use REPLACE on selecting a table column, it doesn't work!
My query:
SELECT REPLACE(UserName, ' ', '') as UserName FROM MY_TABLE
it still gives me usernames with spaces! Am I doing something stupid?
#AlexK. it's 160 for unicode(left(field, 1))
160 is Unicode NO-BREAK SPACE so that's what you need to replace:
replace(UserName, char(160), '')
You could update everything replacing char(160) with a whitespace ' ' and then just use your original query in the future (perhaps also ensuring such values cannot be entered in the future)