postgresql update row to fixed digit number - sql

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.

Related

How do I update a SQL table to strip away the left 3 characters in a 9 character string?

I have a table with values like this in columnA:
ColumnA
800
800602041
800602044
800602050
800602057
800602093
800602099
800602131
800602132
800602133
I need to strip away the leading 800 but leave the rest of the numbers intact. I tried doing an update with wildcards but it failed. I have several tables with thousands of records that need this correction.
The result would look like this:
ColumnA
800
602041
602044
602050
602057
602093
602099
602131
602132
602133
I cannot lose the single 800 record which is why I was trying an update query with wildcards. I only want to update the rows with a 9 digit value that begins with 800.
Kindly try using below-mentioned query.
Consider table name as #a and column name as colA.
Update #a set colA = case when len(ltrim(rtrim(cola)))<=3 then colA else right(colA, len(ltrim(rtrim(colA)))-3) end
Select * from #a
A typical method is:
(case when columnA like '800%' then substring(columnA, 4, 6) else columnA) as new_columnA
Note: This preserves values when the value does not start with 800. And substring() might be spelled substr() in your database.
Since the data values are stored as numbers, I ended up treating it like a math problem.
BEGIN TRAN
SET XACT_ABORT ON
UPDATE sometable
SET ColumnA = ColumnA - 800000000
WHERE ColumnA LIKE '8006%'
It is not sophisticated but it worked.
UPDATE YourTable
SET ColumnA = SUBSTRING(ColumnA, 4, LEN(ColumnA))
WHERE LEN(ColumnA) = 9 AND ColumnA LIKE '800%'
A mass update would look like this with the information you have provided. Update ColumnA and take the entire value starting at the 4th character. Only do this when the length of ColumnA is equal to 9 and is prefixed with 800.
Filter the records by checking the string length which are exactly 9 digits and which starts with characters '800'. Same can be achieved with help of LEFT/RIGHT function.
UPDATE data_table
SET columnA = right(columnA,6)
WHERE LEN(columnA) = 9 AND left(columnA,3) = '800'
Note: Consider "data_table" as your actual table and "columnA" as the column you want to update.

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

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;

Procedure to apply formatting to all rows in a table

I had a SQL procedure that increments through each row and and pads some trailing zeros on values depending on the length of the value after a decimal point. Trying to carry this over to a PSQL environment I realized there was a lot of syntax differences between SQL and PSQL. I managed to make the conversion over time but I am still getting a syntax error and cant figure out why. Can someone help me figure out why this wont run? I am currently running it in PGadmin if that makes any difference.
DO $$
DECLARE
counter integer;
before decimal;
after decimal;
BEGIN
counter := 1;
WHILE counter <> 2 LOOP
before = (select code from table where ID = counter);
after = (SELECT SUBSTRING(code, CHARINDEX('.', code) + 1, LEN(code)) as Afterward from table where ID = counter);
IF before = after
THEN
update table set code = before + '.0000' where ID = counter;
ELSE
IF length(after) = 1 THEN
update table set code = before + '000' where ID = counter;
ELSE IF length(after) = 2 THEN
update table set code = before + '00' where ID = counter;
ELSE IF length(after) = 3 THEN
update table set code = before + '0' where ID = counter;
ELSE
select before;
END IF;
END IF;
counter := counter + 1;
END LOOP
END $$;
Some examples of the input/output of the intended result:
Input 55.5 > Output 55.5000
Input 55 > Output 55.0000
Thanks for your help,
Justin
There is no need for a function or even an update on the table to format values when displaying them.
Assuming the values are in fact numbers stored in a decimal or float column, all you need to do is to apply the to_char() function when retrieving them:
select to_char(code, 'FM999999990.0000')
from data;
This will output 55.5000 or 55.0000
The drawback of the to_char() function is that you need to anticipate the maximum number of digits of that can occur. If you have not enough 9 in the format mask, the output will be something like #.###. But as too many digits in the format mask don't hurt, I usually throw a lot into the format mask.
For more information on formatting functions, please see the manual: https://www.postgresql.org/docs/current/static/functions-formatting.html#FUNCTIONS-FORMATTING-NUMERIC-TABLE
If you insist on storing formatted data, you can use to_char() to update the table:
update the_table
set code = to_char(code::numeric, 'FM999999990.0000');
Casting the value to a number will of course fail if there a non-numeric values in the column.
But again: I strong recommend to store numbers as numbers, not as strings.
If you want to compare this to a user input, it's better to convert the user input to a proper number and compare that to the (number) values stored in the database.
The string matching that you are after doesn't actually require a function either. Using substring() with a regex will do that:
update the_table
set code = code || case length(coalesce(substring(code from '\.[0-9]*$'), ''))
when 4 then '0'
when 3 then '00'
when 2 then '000'
when 1 then '0000'
when 0 then '.0000'
else ''
end
where length(coalesce(substring(code from '\.[0-9]*$'), '')) < 5;
substring(code from '\.[0-9]*$') extracts everything the . followed by numbers that is at the end of the string. So for 55.0 it returns .0 for 55.50 it returns .50 if there is no . in the value, then it returns null that's why the coalesce is needed.
The length of that substring tells us how many digits are present. Depending on that we can then append the necessary number of zeros. The case can be shortened so that not all possible length have to be listed (but it's not simpler):
update the_table
set code = code || case length(coalesce(substring(code from '\.[0-9]*$'), ''))
when 0 then '.0000'
else lpad('0', 5- length(coalesce(substring(code from '\.[0-9]*$'), '')), '0')
end
where length(coalesce(substring(code from '\.[0-9]*$'), '')) < 5;
Another option is to use the position of the . inside the string to calculate the number of 0 that need to be added:
update the_table
set code =
code || case
when strpos(code, '.') = 0 then '0000'
else rpad('0', 4 - (length(code) - strpos(code, '.')), '0')
end
where length(code) - strpos(code, '.') < 4;
Regular expressions are quite expensive not using them will make this faster. The above will however only work if there is always at most one . in the value.
But if you can be sure that every value can be cast to a number, the to_char() method with a cast is definitely the most robust one.
To only process rows where the code columns contains correct numbers, you can use a where clause in the SQL statement:
where code ~ '^[0-9]+(\.[0-9][0-9]?)?$'
To change the column type to numeric:
alter table t alter column code type numeric

SQL Server: How to display a specific character based on position in a column

So I'm attempting to display a single character based on its position in a string from one column. Since this is grid data, there is a simple math to it. The grid has 24 rows 'A-X', and 44 columns.
So lets say I want to see the value in D9. I already know the expected value should be a 'A1', so that means the character length is '2'. If I do the math: (A + B + C = 3 x 44, + 9). That two-character value for D9 starts at the 141st position of that string in Col2. I attempted to use SUBSTRING with no success
SELECT
Col1 , SUBSTRING('Col2',141,2)
FROM Table1
Query result displays data in Col1, but for Col2 its just blank. What am I missing?
Asked too soon. Figured out I had to remove the ' from the column name
SELECT
Col1 , SUBSTRING('Col2',141,2)
FROM Table1
Didn't work
SELECT
Col1 , SUBSTRING(Col2,141,2)
FROM Table1
Works

Update 2 fields - push last digit from 1 field to the beginning of another field

I have 2 fields in my SQL table. One has 5 digits (ex: 12345) and the 2nd field has 2 digits (ex: 99) and I need to know if there is a way to take the LAST digit from the first field and push it to the beginning of the 2nd field. So the first field would be 1234 and the 2nd field would be 599 ???
UPDATE YourTable
SET Column2 = RIGHT(Column1, 1) + COALESCE(Column2, ''),
Column1 = LEFT(Column1, LEN(Column1)-1)
WHERE COALESCE(LEN(Column1), 0) > 1;
Here is a "typical" way to do this.
update t
set col1 = left(col1, length(col1) - 1),
col2 = concat(right(col1, 1), col2);
The exact details depend on the database.
Notes:
This sort of assumes the values are stored as strings, although the logic will convert back and forth from numbers.
The left() and right() functions may differ by database, although most databases do support them.
concat() may be replaced by an appropriate operator, depending on the database ('||', '+', '&', come to mind).
length() is sometimes called len().
And if it's integer columns:
update tablename set
c1 = c1/10,
c2 = mod(c1,10)*dpower(10,ceiling(dlog10(c2)))+c2