Postgres Type Conversion Cast

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/yufenghyc/article/details/45869509

--1 Example
postgres=# Select 1/4;
? column?
----------
0
(1 row)

In PG If you want to do division and want to keep decimals, the method above does not work, because the "/" Operation results are rounded, and the fractional part is truncated.

--2 Type Conversions
postgres=# Select round (1:: Numeric/4:: Numeric,2);
Round
-------
0.25
(1 row)

Note: After the type conversion, you can retain the fractional part.


--3 can also be converted by the cast function
postgres=# Select Round (CAST (1 as numeric)/cast (4 as numeric), 2);
Round
-------
0.25
(1 row)


--4 about the use of the cast function
postgres=# SELECT substr (CAST (1234 as text), 3,1);
Substr
--------
3
(1 row)

Postgres Type Conversion Cast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.