I have huge table where occasionally one column got not rounded values,
e.g. 16345.462500 instead of 16345.460000
I'm not that good in postgres - maybe it's possible to update table rounding those values?
I can't change field type, because some rows (cryptocurrencies) can contain not rounded numbers.
Easiest thing I can think about is PHP script to manually update all fields.
You can use the round function to round to N decimal places - in your case, 2:
UPDATE mytable
SET mycolumn = ROUND(mycolumn, 2)
Related
I have a table with 2 varchar columns (name and value)
and I have such a query:
select * from attribute
where name = 'width' and cast( value as integer) > 12
This query works but I suppose there are could be an issue with execution plan because of index build over value column because it is technically varchar but we convert it to integer.
Are there ways to fix it ?
P.S. I can't change type to int because the database design implies that value could be any type.
Performance should not be your first worry here.
Your statement is prone to failures. You read this as:
read all rows with name = 'width'
of these rows cast all values to integer and only keep those with a value graeter than 12
But the DBMS is free to check conditions in the WHERE clause in any order. If the DBMS does that:
cast all values to integer and only keep the rows with a value graeter than 12
of these rows keep all with name = 'width'
the first step will already cause a runtime error, if there is a non-integer value in that table, which is likely.
So first get your query safe. The following should work:
select *
from
(
select *
from attribute
where name = 'width'
) widths
where cast(value as integer) > 12;
This will still fail, when your width contains non-integers. So, to get this even safe in case of invalid data in the table, you may want to add a check that the value only contains digits in the subquery.
And yes, this won't become super-fast. You sacrifice speed (and data consistency checks) for flexibility with this data model.
What you can do, however, is create an index on both columns, so the DBMS can quickly find all width rows and then have the value directly at hand, before it accesses the table:
create index idx on attribute (name, value);
As far as I know, there is no fail-safe cast function in PostgreSQL. Otherwise you could use this and have a function index instead. I may be wrong, so maybe someone can come up with a better solution here.
Due to project requirements I need to store a number as text, since depending on a column I round it to some decimal places or others, and with a specific format: comma as thousands separator and period as decimal separator.
If for example I had to round to two digits and I have this 12500.987589 I would need to get this another 12,500.98.
The only solution I have found in Snowflake is something similar to this:
SELECT
TO_VARCHAR(TO_NUMBER(TO_VARCHAR(ROUND(12500.987589 ,2)),'9,999,999.99',38,2))
FROM DUAL;
Do you know any option to do this?
Thank you very much and greetings,
Why do you need to use the round function at all? You should be able to use
select to_varchar(12500.987589, '9,999,999.99')
this produces: 12,500.99
Number should be stored as number data type. Formatting of the number is a matter of application layer. Storing them as a text could lead to problems with implicit conversion, arithmetic operations, etc.
If that is not possible you could:
Create a view on top of a table that provides additional column with formatted string
CREATE VIEW v_tab
AS
SELECT *, ... AS number_formatted
FROM tab;
Create a computed column:
ALTER TABLE tab
ADD COLUMN number_formatted VARCHAR(100) AS (...);
The expression to get desired formatting:
SELECT TO_VARCHAR(TO_NUMBER(ROUND(12500.987589,2),38,2),'9,999,999.99')
Assume I have a query that returns a result set of columns A and B from table First_Table. I want to limit the result set to those columns if the value of column X in table Second_Table is 0, and I want to add column C from table First_Table if the value of column X is 1.
The problem is easily resolved using a Python for example whereby I just have a variable as an empty string if value in column X is 0 or it would be equal to the string 'First_Table.ColumnC AS [Dynamic Value],', and I just format the sql in the script accordingly.
If Else solution is not an elegant way because I have multiple columns to add dynamically depending on multiple values...
I am just looking for some ideas on directions.. I have been looking at this for a while, might be bogged up
Dynamic sql is the best way to resolve this as suggested in the comments.
I have a column PRICESTABLE.PRICE with values like the following
12.356898
13.587988
I need to make an update to PRICETABLE to update the prices like these:
12.350000
13.580000
Notice I don't want to ALTER the column, only round the values, thanks!
The following would work for MySQL, SQL Server, and Postgres:
UPDATE PRICESTABLE
SET PRICE = ROUND(PRICE, 2);
You might want to add a WHERE clause to the above update, unless you really want to apply it to the entire table. Also, an alternative here would be to keep all the original precision, and just call ROUND when you need to present the data.
the following function deletes all blanks in a text or varchar column and returns the modified text/varchar as an int:
select condense_and_change_to_int(number_as_text_column) from mytable;
This exact query does work.
Though my goal is to apply this function to all rows of a column in order to consistently change its values. How would I do this? Is it possible with the UPDATE-clause, or do i need to do this within a function itself? I tried the following:
UPDATE mytable
SET column_to_be_modiefied = condense_and_change_to_int(column_to_be_modiefied);
Basically i wanted to input the value of the current row, modify it and save it to the column permanantly.
I'd welcome all ideas regarding how to solve scenarios like these. I'm working with postgresql (but welcome also more general solutions).
Is it possible with an update? Well, yes and sort-of.
From your description, the input to the function is a string of some sort. The output is a number. In general, numbers should be assigned to columns with a number type. The assumption is that the column in question is a number.
However, your update should work. The result will be a string representation of the number.
After running the update, you can change the column type, with something like:
alter table mytable alter column column_to_be_modiefied int;