Remove string from multiple rows SQL - sql

I have a table that has "HQ created..." in 100+ rows. Instead of manually updating those rows I want to trim that part off.
I tried:
UPDATE Table SET Desc = LTRIM('HQ created') WHERE Desc LIKE '%HQ created%'
and that changed all 100+ rows to "HQ created" instead of cutting that part off.
(I also tried SET HADEDesc = RTRIM('HQ created'))
A replace will not work as outside of the "HQ created..." the names are different for example "HQ created Date" and "HQ created Time".

update table
set desc = replace(Desc, 'HQ created' , '')
WHERE Desc LIKE '%HQ created%'

Related

Postgresql , updating existing table row with another tables data

I am trying to update a null column using another tables value but it doesn't seems to work right. below codes were tried
SET
"Test name "= "Test"(
SELECT Transformertest.Test,Transformertest.TestID
FROM public.Transformertest WHERE TestID='Tes3')
WHERE test2table.Type='Oil Immersed Transformers'
UPDATE
public.test2table
SET
"Test name" = subquery."Test"
FROM
(
SELECT
"Test"
FROM Transformertest WHERE "TestID"='Tes2'
) AS subquery
WHERE
"Type"='Auto Transformer' AND "Phase"='3' AND "Rated Frequency"='60';
enter image description here
don't use space in column name.
Integers don't need to be quoted
See the result here (enter link description here)
what you need to do here (assuming Phase and Rated Frequency are integers)
remove unnecessary "" and spaces on column names
UPDATE
public.test2table
SET
test_name = subquery.Test
FROM
(
SELECT
test
FROM Transformertest WHERE Test_ID='Tes2'
) AS subquery
WHERE
Type='Auto Transformer' AND Phase=3 AND Rated_Frequency=60;
this should be working now

find diffrences between 2 tables sql and how can i get the changed value?

i have this query
insert into changes (id_registro)
select d2.id_registro
from daily2 d2
where exists (
select 1
from daily d1
where
d1.id_registro = d2.id_registro
and (d2.origen, d2.sector, d2.entidad_um, d2.sexo, d2.entidad_nac, d2.entidad_res,
d2.municipio_res, d2.tipo_paciente,d2.fecha_ingreso, d2.fecha_sintomas,
d2.fecha_def, d2.intubado, d2.neumonia, d2.edad, d2.nacionalidad, d2.embarazo,
d2.habla_lengua_indig, d2.diabetes, d2.epoc, d2.asma, d2.inmusupr, d2.hipertension,
d2.otra_com, d2.cardiovascular, d2.obesidad,
d2.renal_cronica, d2.tabaquismo, d2.otro_caso, d2.resultado, d2.migrante,
d2.pais_nacionalidad, d2.pais_origen, d2.uci )
<>
(d1.origen, d1.sector, d1.entidad_um, d1.sexo, d1.entidad_nac, d1.entidad_res,
d1.municipio_res, d1.tipo_paciente, d1.fecha_ingreso, d1.fecha_sintomas,
d1.fecha_def, d1.intubado, d1.neumonia, d1.edad, d1.nacionalidad, d1.embarazo,
d1.habla_lengua_indig, d1.diabetes, d1.epoc, d1.asma, d1.inmusupr, d1.hipertension,
d1.otra_com, d1.cardiovascular, d1.obesidad,
d1.renal_cronica, d1.tabaquismo, d1.otro_caso, d1.resultado, d1.migrante,
d1.pais_nacionalidad, d1.pais_origen, d1.uci ))
it results in an insersion data that doesn't exist in another table, that's fine. but i want know exactly which field has changed to store it in a log table
You don't mention precisely what you expect to see in your output but basically to accomplish what you're after you'll need a long sequence of CASE clauses, one for each column
e.g. one approach might be to create a comma-separated list of the column names that have changed:
INSERT INTO changes (id_registro, column_diffs)
SELECT d2.id_registro,
CONCAT(
CASE WHEN d1.origen <> d2.origen THEN 'Origen,' ELSE '' END,
CASE WHEN d1.sector <> d2.sector THEN 'Sector,' ELSE '' END,
etc.
Within the THEN part of the CASE you can build whatever detail you want to show
e.g. a string showing before and after values of the columns CONCAT('Origen: Was==> ', d1.origen, ' Now==>', d2.origen). Presumably though you'll also need to record the times of these changes if there can be multiple updates to the same record throughout the day.
Essentially you'll need to decide what information you want to show in your logfile, but based on your example query you should have all the information you need.

SQL WHILE statement error "The column prefix does not match with a table name or alias name used in the query"

I am trying to run the below SQL statement and am getting the below message
The column prefix 'timInvtTran' does not match with a table name or alias name used in the query.
Basically what I'm trying to do is to get the resulting Quantity on Hand (QOH) after each inventory transaction.
An assumption to this is that when summing TranQty you will get the resulting QOH.
I am trying to loop the data and add a running total of TranQty, which would by definition be the QOH. I named my QOH variable as #currQOH .
I know the issue is coming from my SET statement but I can’t figure out why it’s happening.
As a side note, am I on the correct path to getting a running total of TranQty? I don’t want to write to the DB and so I don’t want to create new tables for my running total. I researched high and low and couldn’t find anything.
Any bit of help would be extremely appreciated.
DECLARE #currQOH INT
SET #currQOH=0
WHILE( Select ItemKey from timInvtTran) = 41511
BEGIN
SET #currQOH = #currQOH + timInvtTran.TranQty
BREAK
END
select #currQOH, ItemKey, TranID
from timInvtTran
where timInvtTran.ItemKey = 41511 and substring(tranid,12,2) <>'SH'
order by timinvttran.createdate desc
BTW Happy PI day!
Try this instead
DECLARE #currQOH INT
SET #currQOH= ( select sum(TranQty) from timInvtTran where ItemKey = 41511 )
select #currQOH, ItemKey, TranID
from timInvtTran
where timInvtTran.ItemKey = 41511 and substring(tranid,12,2) <>'SH'
order by timInvttran.createdate desc
Note the case change in the last line: timinvttran -> timInvttran

Updating Every Row With select concatenate

I have a table that I want to update a column in the whole thing. I have this for the concatenation.
(COALESCE(f1,'')+';'+ COALESCE(f2,'')+';'+ COALESCE(f3,''))
Well when I insert this into my column it adds new columns and update just fails. Is there a way to update a value inside MS SQL and use it to change a value?
Thanks
update dbo.tblGeoTable (CombinedEmail)
select (COALESCE(f1,'')+';'+ COALESCE(f2,'')+';'+ COALESCE(f3,''))
from dbo.tblGeoTable
Here is the data
F1 = f1email#email.com
F2 = f2email#email.com
F3 = f3email#email.com
CombinedEmail = f1+f2+f3, but I need the ; in there to seperate them and I need it replaced in the current row that its in.
If I understand correctly what you want try this
UPDATE tblGeoTable
SET CombinedEmail = COALESCE(f1,'')+';'+ COALESCE(f2,'')+';'+ COALESCE(f3,'')
Here is sqlfiddle example
EDIT:
If you want to add instead of replace to the values in CombinedEmail column you can do
UPDATE tblGeoTable
SET CombinedEmail = COALESCE(CombinedEmail,'') + ';' + COALESCE(f1,'')+';'+ COALESCE(f2,'')+';'+ COALESCE(f3,'')

sql to set an xml value

I'm a novice in mySql.
I'm trying to replace a value in the xml column of my table.
my select method works.
SELECT * FROM `comics` WHERE ExtractValue(xml,'comic/pageNumber') = 6
my replace method doesn't. I've been searching for the correct syntax for a bit now...
SET xml.modify(
replace value of ('comic/pageNumber') with 5
)
some background:
this situation comes up when i delete a comic page.
it leaves a gap in the page numbers, after which i would either:
iterate through all the comics and remove any gaps in the page numbers.
or
iterate through all comics with pageNumber larger than the deleted page, and reduce their pageNumber by 1.
How about
UPDATE comics
SET xml = UpdateXML(xml,'comic/pageNumber', '<pageNumber>5</pageNumber>')
WHERE ExtractValue(xml,'comic/pageNumber') = 6
Tested on MySQL version 5.1
UPDATE `comics`
SET xml = UpdateXML(xml,
'comic/pageNumber',
concat('<pageNumber>',(ExtractValue(xml,'comic/pageNumber')+1),'</pageNumber>'))
WHERE ExtractValue(xml,'comic/pageNumber') >= 1
You'd be better off actually storing the fields in the table, rather than a single field with xml in it. Then the following would work. Otherwise there's not much point using a relational database at all.
BEGIN;
DELETE FROM `comics`
WHERE `comicID` = :id AND `pageNumber` = :page;
UPDATE `comics` SET `pageNumber` = `pageNumber` - 1
WHERE `comicID` = :id AND `pageNumber` > :page;
COMMIT;