16

I've a set of data in a postgresql DB, where one of these columns store string data in float format, but now I need remove the decimal component of the string. How can I do this using an sql update statement in my BD console? Is that possible?

for example:

"25.3" -> "25"

If it does not possible how can I do this? Thanks in advance.

1

2 Answers 2

35

You would be better suited casting the columns that were text, to numeric, to integer, so that rounding is taken into consideration e.g.

SELECT '25.3'::numeric::integer AS num1, '25.5'::numeric::integer AS num2

which would return integers of 25 and 26 respectively.

If you were not concerned with the digits following the point, the floor(column_name::numeric)::integer function or a substring, as mentioned, should be fine.

Sign up to request clarification or add additional context in comments.

2 Comments

how can you keep '25.5'::numeric::integer as 25 ?
use the floor function SELECT floor(25.5)::integer
-3

Since it is a string, you can use string functions to drop the after decimal digits. If you do not want to round them off, just drop the decimal part then use -

update table_name 
   set column_name = substring(column_name from 1 for position('.' in column_name)-1);

If you want rounding off, then you can use the cast as mentioned by @mlinth.

1 Comment

I solved my problem with the slow way, using like % operator, but i'll accept you answer, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.