SQL Server: Update a Column, concatenate char with that column value - sql

I have a table that contains a column that I need to update if the length of that value is equal to 1, the thing is that if this is true I need to get that value and concatenate a 0 before it, for example:
If the value of the column is equal to "5" I need to update that row to "05", I need to do this for all the rows that match this criteria.
I tried this:
UPDATE WS
SET WS.used_brand=CONCAT('0',(Select WS.used_brand FROM WS)) WHERE LEN(WS.used_brand) = 1;
It doesn't work because of the inner select, how can I fix this?.
Thanks.

I think this does what you want:
UPDATE WS
SET WS.used_brand = CONCAT('0', WS.used_brand)
WHERE LEN(WS.used_brand) = 1;
Note: Many databases support LPAD() or a similar function for padding values on the left.
In SQL Server, you would more likely write this as:
UPDATE WS
SET WS.used_brand = '0' + WS.used_brand
WHERE LEN(WS.used_brand) = 1;

You don't need a nested SELECT statement - you can reference the column just like this:
UPDATE WS
SET WS.used_brand=CONCAT('0', WS.used_brand)
WHERE LEN(WS.used_brand) = 1;

Related

sql update add value one row to another row within the same table

SQL update add value one row to another row with in the same table
UPDATE
artigoArmazem
SET
StkActual= StkActual + StkActual2
FROM
(Select StkActual FROM artigoArmazem where artigo='110044' and Localizacao ='VIA1' and lote='<L01>' ) AS StkActual2
artigoArmazem
You just add them:
UPDATE artigoArmazem
SET StkActual = StkActual + StkActual2
WHERE artigo = '110044' AND -- do not use quotes if the value is really a number
Localizacao ='VIA1' AND
lote = '<L01>';
UPDATE works one row at a time, so you are fine. Things get trickier if you want to combine values from multiple rows.

postgresql update row to fixed digit number

I am beginner in sql query and I am trying to update my rows like that:
1--->0001
15-->0015
254-->0254
1458-->1458
My column's type is text and there are lots of columns so I cannot handle with
update table1 set col1 = 0001 where col1 = 1;
and so on..
This seems easy question but after research,I could not find a solution. all I need is something like
foreach row in col1
if((int)row>0 and < 10)
then row = "000" + row;
All texts are infact integer value but I have to keep them as text. Whats sql query of above code?
Thanks
You can use the lpad() function:
update table1
set col1 = lpad(col1, 4, '0')
where length(col1) < 4;
But the real question is: why are you storing numbers as text values? That is almost always a bad choice.

SQL add percentage to value in calculation

Ive seen a few examples on here to work out how to add a percentage to a value in sql, but they don't match if I actually do the calculation.
Ive tried "Value + (value * percentage)"
Any other examples I can try, I have a value column and a percentage column which will be used to work out the increased/decreased amount.
So you need:
SELECT Value,Percentage,OtherColumns.....,VALUE + (value*percentage)/100 as newValue
FROM YourTable
If you need an update, it works the same way:
UPDATE YourTable
SET VALUE = VALUE + (value*percentage)/100
You can add where clause to filter only a desired records
If you need to add a percentage to an existing value add 1 to the percentage and multiply by the value.
Update table set value = value*(1+45%)
100*110% = 110...
9*(1+5%) = 9.45...
8*(1+100%) = 16
2*(1+0%) = 2
This assumes the value in the table is decimal to begin with if integer, then you will end up with some rounding issues.

copy part of string from one column into another

I've got a column with some string data that somewhere has 'T##' (## being a two digit number) I want to copy this into another column, how do I do that?
something like this:
abc-T03-def -> 03
For Microsoft SQL Server:
update YourTable
set NewColumn = substring(OldColumn, patindex('%T[0-9][0-9]%', OldColumn) + 1, 2)
where patindex('%T[0-9][0-9]%', OldColumn) <> 0

DB2 Increment a value by a certain amount

I need to write a query that increments a value in a table by 3 when run.
I would like to do something like this but this doesn't work.
UPDATE table
SET value = (SELECT value
FROM table
WHERE condition = true) + 3
WHERE condition = true
As in the title this is a DB2 database, any ideas?
EDIT: Actually this does work, could also do the + 3 in the select. I just had some stuff in the wrong place with the casting I had to do
Thanks in advance
I think what you are looking for is simply
UPDATE table SET value = value + 3 WHERE condition = TRUE
Does that work?
If you want all rows where condition = true to have (for example) 3+ the max value of any row for which the condition is true, use this:
UPDATE table SET value =
(
SELECT MAX(value)
FROM table WHERE condition = true
) + 3
WHERE condition = true