Updating column value to replace a particular character in Teradata - sql

I need help with replacing column value to replace all '%' delimiter with '~'.
Example:
CTP%3A1c5d8384-5980-4bee-88f5-aed7594594ba%7ESV%3ANA%7ECUST%3Anew%7ECSR%3AGREATER+BOSTON+MARKET%7ECSD%3ANORTHEAST+DIVISION%7EZIP%3A02140%7EBFT%3AAll+digital+buyflow%7Cdotcom%7EBPT%3ANA%7EIDV%3ANA%7EMRC%3ANA%7ESIKE%3ANA%7ESIKP%3ANA%7ESIKS%3ANA%7ERAF%3Ano
CTP%3A7ef7b00a-10dc-41e9-8691-0a01463703c9%7ESV%3ANA%7ECUST%3Aexisting%7ECSR%3ABIG+SOUTH+REGION%7ECSD%3ACENTRAL+DIVISION%7EZIP%3A30101%7EBFT%3AAll+digital+buyflow%7Cdotcom%7EBPT%3ANA%7EIDV%3ANA%7EMRC%3ANA%7ESIKE%3ANA%7ESIKP%3ANA%7ESIKS%3ANA%7ERAF%3Ano
CTP%3A9ed9ba57-4227-444a-9385-101bbc0df3bc%7ESV%3ANA%7ECUST%3Anew%7ECSR%3ABIG+SOUTH+REGION%7ECSD%3ACENTRAL+DIVISION%7EZIP%3A30047%7EBFT%3AAll+digital+buyflow%7Cdotcom%7EBPT%3ANA%7EIDV%3ANA%7EMRC%3ANA%7ESIKE%3ANA%7ESIKP%3ANA%7ESIKS%3ANA%7ERAF%3Ano
CTP%3Ae46e7133-340d-41b9-9cdf-2baea14c86b6%7ESV%3ANA%7ECUST%3Aexisting%7ECSR%3ASEATTLE+MARKET%7ECSD%3AWEST+DIVISION%7EZIP%3A98223%7EBFT%3AAddOnChannel%7EBPT%3ANA%7EIDV%3ANA%7EMRC%3ANA%7ESIKE%3ANA%7ESIKP%3ANA%7ESIKS%3ANA%7ERAF%3Ano
Expected Output:
CTP~3A1c5d8384-5980-4bee-88f5-aed7594594ba~7ESV~3ANA~7ECUST~3Anew~7ECSR~3AGREATER+BOSTON+MARKET~7ECSD~3ANORTHEAST+DIVISION~7EZIP~3A02140~7EBFT~3AAll+digital+buyflow~7Cdotcom~7EBPT~3ANA~7EIDV~3ANA~7EMRC~3ANA~7ESIKE~3ANA~7ESIKP~3ANA~7ESIKS~3ANA~7ERAF~3Ano
CTP~3A7ef7b00a-10dc-41e9-8691-0a01463703c9~7ESV~3ANA~7ECUST~3Aexisting~7ECSR~3ABIG+SOUTH+REGION~7ECSD~3ACENTRAL+DIVISION~7EZIP~3A30101~7EBFT~3AAll+digital+buyflow~7Cdotcom~7EBPT~3ANA~7EIDV~3ANA~7EMRC~3ANA~7ESIKE~3ANA~7ESIKP~3ANA~7ESIKS~3ANA~7ERAF~3Ano
CTP~3A9ed9ba57-4227-444a-9385-101bbc0df3bc~7ESV~3ANA~7ECUST~3Anew~7ECSR~3ABIG+SOUTH+REGION~7ECSD~3ACENTRAL+DIVISION~7EZIP~3A30047~7EBFT~3AAll+digital+buyflow~7Cdotcom~7EBPT~3ANA~7EIDV~3ANA~7EMRC~3ANA~7ESIKE~3ANA~7ESIKP~3ANA~7ESIKS~3ANA~7ERAF~3Ano
CTP~3Ae46e7133-340d-41b9-9cdf-2baea14c86b6~7ESV~3ANA~7ECUST~3Aexisting~7ECSR~3ASEATTLE+MARKET~7ECSD~3AWEST+DIVISION~7EZIP~3A98223~7EBFT~3AAddOnChannel~7EBPT~3ANA~7EIDV~3ANA~7EMRC~3ANA~7ESIKE~3ANA~7ESIKP~3ANA~7ESIKS~3ANA~7ERAF~3Ano
I have tried to use OREPLACE but it didn't work.
UPDATE A
SET ORDER_INFO = OREPLACE(ORDER_INFO,'%','~')
WHERE ORDER_INFO NOT LIKE '%~%' AND CTP_SESSION_ID IS NULL;

UPDATE A
SET ORDER_INFO = OREPLACE(ORDER_INFO,'%','~')
WHERE ORDER_INFO NOT LIKE '%~%'
AND CTP_SESSION_ID IS NULL;

Related

JSONB change params value

I have a table that contains a json type in one row called whole_params, and I would like to change values if json param called exampleParam equals value1 I want to change it to value2
UPDATE table_name t
SET whole_params = json_set(whole_params, '{exampleParam}')::jsonb)
WHERE t.whole_params ->> 'exampleParam' = 'value1';
I wrote something like that but I do not know how to change that 'value1', any ideas?
You just need to use jsonb_set() instead of json_set() with a slight change as
UPDATE table_name t
SET whole_params = jsonb_set(whole_params, '{exampleParam}','"value2"')
WHERE t.whole_params ->> 'exampleParam' = 'value1';

SET Date in Column A of type TIMESTAMP in DB2

I am running following DB2 query:
UPDATE Table
SET DATE(COLA) = '2017-03-31'
WHERE DATE(COLA) = '2017-03-29';
COLA is of type TIMESTAMP.
Sample Value:
COLA:
2017-03-29-00.00.00.000000
Expected Updated Value:
COLA:
2017-03-31-00.00.00.000000
When I am running above query, I am getting
SQL Code SQLCODE = -104,ERROR: ILLEGAL SYMBOL "(". SOME SYMBOLS THAT
MIGHT BE LEGAL ARE: = .
Could anyone help me out for this scenario?
Thanks a lot!
I believe your problem is in the SET portion. Try this:
UPDATE Table
SET COLA = DATE('2017-03-31') -- TIMESTAMP ('2017-03-31-00.00.00.000000')
WHERE DATE(COLA) = '2017-03-29';
You want to update the column, not update the result of a function performed on the column.
just do it:
UPDATE Table
SET COLA = '2017-03-31-00.00.00.000000'
WHERE DATE(COLA) = '2017-03-29';

Update all rows where contains 5 keys

I have Ticket table that has some columns like this :
ID : int
Body : nvarchar
Type : int
I have many rows where the Body column has value like this :
IPAddress = sometext, ComputerName = sometext , GetID = sometext, CustomerName=sometext-sometext , PharmacyCode = 13162900
I want update all rows' Type column where the Body column has at least five of the following keys:
IPAddress, ComputerName, GetID, CustomerName, PharmacyCode
You could do it with a simple update statement like that
UPDATE Ticket
SET Type = 4
WHERE Body LIKE '%IPAddress%'
and Body LIKE '%ComputerName%'
and Body LIKE '%GetID%'
and Body LIKE '%CustomerName%'
and Body LIKE '%PharmacyCode%'
if you know the 'keys' are always in the same order you could concatenate the LIKE conditions like so
UPDATE Ticket
SET Type = 4
WHERE Body LIKE '%IPAddress%ComputerName%GetID%CustomerName%PharmacyCode%'
If you have the possibility to change the data model it would be much better to explode this key & value column into an own table and link it back to this table as it is done in a proper relational model.
If you could calculate number of key value pair by number of = present in your string you could use this query
Update tblname set col=val where len(colname) - len(replace(colname,'=','')>5
The where part actually gives number of equal signs present in your string.

How should I add text to my column's select statement

This is my sql code
SELECT sessionname, left(comment,4))
FROM moma_reporting.comments where name like '%_2016_02_%'
and comment = '1200'
My output will be :
"WE247JP_2016_02_07__14_48_18";"1200"
"FORD49_2016_02_03__12_42_24";"1200"
"1-GRB-804_2016_02_06__08_20_15";"1200"
What i want to do is to add text to column -comment so it will look like this:
"WE247JP_2016_02_07__14_48_18";"1200-QC"
"FORD49_2016_02_03__12_42_24";"1200-QC"
"1-GRB-804_2016_02_06__08_20_15";"1200-QC"
How i can do this ?
Just concat it:
SELECT sessionname, left(comment,4))||'-QC'
FROM moma_reporting.comments
where name like '%_2016_02_%'
and comment = '1200'
Unrelated, but: left(comment,4)) is useless. The condition and comment = '1200' will never return comments that are longer then 4 characters.

Delete specific word at the beginning

in my table within some columns i got strings which starting always with /PicsDB
like this below:
/PicsDB/Something 2015/Some thing bla/Some thing other/img34234.jpg
what i want to achieve is to for each row delete starting string /PicsDB
so using above string the final result should be:
/Something 2015/Some thing bla/Some thing other/img34234.jpg
How to achieve that?
Can i just simply do ? :
UPDATE my_table SET path = replace(path, '/PicsDB', '');
Just use substring:
UPDATE my_table SET path = substring(path, 8, 9999);
where path like '/PicsDB%'