热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

PostgreSQL数据库中数字相关函数

PostgreSQL的数字函数主要用于数字操纵和/或数学计算。下表详列的数字函数:名称描述ABS()Returnstheabsolutevalueofnumericexpression.ACOS()Returnsthearccosineofnumericexpression.ReturnsNULLifthevaluei

PostgreSQL的数字函数主要用于数字操纵和/或数学计算。下表详列的数字函数:

名称 描述
ABS() Returns the absolute value of numeric expression.
ACOS() Returns the arccosine of numeric expression. Returns NULL if the value is not in the range -1 to 1.
ASIN() Returns the arcsine of numeric expression. Returns NULL if value is not in the range -1 to 1
ATAN() Returns the arctangent of numeric expression.
ATAN2() Returns the arctangent of the two variables passed to it.
CEIL() Returns the smallest integer value that is not less than passed numeric expression
CEILING() Returns the smallest integer value that is not less than passed numeric expression
COS() Returns the cosine of passed numeric expression. The numeric expression should be expressed in radians.
COT() Returns the cotangent of passed numeric expression.
DEGREES() Returns numeric expression converted from radians to degrees.
EXP() Returns the base of the natural logarithm (e) raised to the power of passed numeric expression.
FLOOR() Returns the largest integer value that is not greater than passed numeric expression.
GREATEST() Returns the largest value of the input expressions.
LEAST() Returns the minimum-valued input when given two or more.
LOG() Returns the natural logarithm of the passed numeric expression.
MOD() Returns the remainder of one expression by diving by another expression.
PI() Returns the value of pi
POW() Returns the value of one expression raised to the power of another expression
POWER() Returns the value of one expression raised to the power of another expression
RADIANS() Returns the value of passed expression converted from degrees to radians.
ROUND() Returns numeric expression rounded to an integer. Can be used to round an expression to a number of decimal points
SIN() Returns the sine of numeric expression given in radians.
SQRT() Returns the non-negative square root of numeric expression.
TAN() Returns the tangent of numeric expression expressed in radians.
ABS(X)

The ABS() function returns the absolute value of X. Consider the following example:

testdb "pun">=# SELECT ABS "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> ABS() "pln">                                                  |
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                       |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
testdb=# "pln"> SELECT ABS(-);
"pun">+---------------------------------------------------------+
| "pln"> ABS() "pln">                                                  |
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                       |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
ACOS(X)

This function returns the arccosine of X. The value of X must range between .1 and 1 or NULL will be returned. Consider the following example:

testdb "pun">=# SELECT ACOS "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> ACOS() "pln">                                                 |
"pun">+---------------------------------------------------------+
| "pln"> 0.000000 "pln">                                                |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
ASIN(X)

The ASIN() function returns the arcsine of X. The value of X must be in the range of .1 to 1 or NULL is returned.

testdb "pun">=# SELECT ASIN "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> ASIN() "pln">                                                 |
"pun">+---------------------------------------------------------+
| "pln"> 1.5707963267949 "pln">                                          "pun">|
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
ATAN(X)

This function returns the arctangent of X.

testdb "pun">=# SELECT ATAN "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> ATAN() "pln">                                                 |
"pun">+---------------------------------------------------------+
| "pln"> 0.78539816339745 "pln">                                         "pun">|
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
ATAN2(Y,X)

This function returns the arctangent of the two arguments: X and Y. It is similar to the arctangent of Y/X, except that the signs of both are used to find the quadrant of the result.

testdb "pun">=# SELECT ATAN2 "pun">( "pun">, "pun">);
"pun">+---------------------------------------------------------+
| "pln"> ATAN2(,) "pln">                                              |
"pun">+---------------------------------------------------------+
| "pln"> 0.46364760900081 "pln">                                         "pun">|
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
CEIL(X) CEILING(X)

These function return the smallest integer value that is not smaller than X. Consider the following example:

testdb "pun">=# SELECT CEILING "pun">(3.46 "pun">);
"pun">+---------------------------------------------------------+
| "pln"> CEILING( "lit">3.46) "pln">                                           |
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                       |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
testdb=# "pln"> SELECT CEIL(- "lit">6.43);
"pun">+---------------------------------------------------------+
| "pln"> CEIL(- "lit">6.43) "pln">                                             |
"pun">+---------------------------------------------------------+
| "pln"> - "pln">                                                      |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
COS(X)

This function returns the cosine of X. The value of X is given in radians.

testdb "pun">=#SELECT COS "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> COS() "pln">                                                 |
"pun">+---------------------------------------------------------+
| "pln"> - "lit">0.44807361612917 "pln">                                        "pun">|
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
COT(X)

This function returns the cotangent of X. Consider the following example:

testdb "pun">=#SELECT COT "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> COT() "pln">                                                  |
"pun">+---------------------------------------------------------+
| "pln"> 0.64209261593433 "pln">                                         "pun">|
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
DEGREES(X)

This function returns the value of X converted from radians to degrees.

testdb "pun">=#SELECT DEGREES "pun">(PI "pun">());
"pun">+---------------------------------------------------------+
| "pln"> DEGREES( "pln">PI()) "pln">                                           |
"pun">+---------------------------------------------------------+
| "pln"> 180.000000 "pln">                                              |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
EXP(X)

This function returns the value of e (the base of the natural logarithm) raised to the power of X.

testdb "pun">=#SELECT EXP "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> EXP() "pln">                                                  |
"pun">+---------------------------------------------------------+
| "pln"> 20.085537 "pln">                                               |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
FLOOR(X)

This function returns the largest integer value that is not greater than X.

testdb "pun">=#SELECT FLOOR "pun">(7.55 "pun">);
"pun">+---------------------------------------------------------+
| "pln"> FLOOR( "lit">7.55) "pln">                                             |
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                       |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
GREATEST(n1,n2,n3,..........)

The GREATEST() function returns the greatest value in the set of input parameters (n1, n2, n3, and so on). The following example uses the GREATEST() function to return the largest number from a set of numeric values:

testdb "pun">=#SELECT GREATEST "pun">( "pun">, "pun">, "pun">, "pun">, "pun">, "pun">, "pun">, "pun">, "pun">, "pun">);
"pun">+---------------------------------------------------------+
| "pln"> GREATEST(,,,,,,,,,) "pln">                      "pun">|
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                      |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
LEAST(N1,N2,N3,N4,......)

The LEAST() function is the opposite of the GREATEST() function. Its purpose is to return the least-valued item from the value list (N1, N2, N3, and so on). The following example shows the proper usage and output for the LEAST() function:

testdb "pun">=#SELECT LEAST "pun">( "pun">, "pun">, "pun">, "pun">, "pun">, "pun">, "pun">, "pun">, "pun">, "pun">);
"pun">+---------------------------------------------------------+
| "pln"> LEAST(,,,,,,,,,) "pln">                         "pun">|
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                       |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
LOG(X) LOG(B,X)

The single argument version of the function will return the natural logarithm of X. If it is called with two arguments, it returns the logarithm of X for an arbitrary base B. Consider the following example:

testdb "pun">=#SELECT LOG "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> LOG() "pln">                                                 |
"pun">+---------------------------------------------------------+
| "pln"> 1.65321251377534 "pln">                                               |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
testdb=# "pln">SELECT LOG(,);
"pun">+---------------------------------------------------------+
| "pln"> LOG(,) "pln">                                            |
"pun">+---------------------------------------------------------+
| "pln"> 16.000000 "pln">                                               |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
MOD(N,M)

This function returns the remainder of N divided by M. Consider the following example:

testdb "pun">=#SELECT MOD "pun">( "pun">, "pun">);
"pun">+---------------------------------------------------------+
| "pln"> MOD(,) "pln">                                               |
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                       |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
PI()

This function simply returns the value of pi. SQL internally stores the full double-precision value of pi.

testdb "pun">=#SELECT PI "pun">();
"pun">+---------------------------------------------------------+
| "pln"> PI() "pln">                                                    |
"pun">+---------------------------------------------------------+
| "pln"> 3.141593 "pln">                                                |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
POW(X,Y) POWER(X,Y)

These two functions return the value of X raised to the power of Y.

testdb "pun">=# SELECT POWER "pun">( "pun">, "pun">);
"pun">+---------------------------------------------------------+
| "pln"> POWER(,) "pln">                                              |
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                      |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
RADIANS(X)

This function returns the value of X, converted from degrees to radians.

testdb "pun">=#SELECT RADIANS "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> RADIANS() "pln">                                             |
"pun">+---------------------------------------------------------+
| "lit">1.570796 "pln">                                                 |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
ROUND(X) ROUND(X,D)

This function returns X rounded to the nearest integer. If a second argument, D, is supplied, then the function returns X rounded to D decimal places. D must be positive or all digits to the right of the decimal point will be removed. Consider the following example:

testdb "pun">=#SELECT ROUND "pun">(5.693893 "pun">);
"pun">+---------------------------------------------------------+
| "pln"> ROUND( "lit">5.693893) "pln">                                          "pun">|
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                       |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
testdb=# "pln">SELECT ROUND( "lit">5.693893,);
"pun">+---------------------------------------------------------+
| "pln"> ROUND( "lit">5.693893,) "pln">                                        "pun">|
"pun">+---------------------------------------------------------+
| "pln"> 5.69 "pln">                                                    |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
SIGN(X)

This function returns the sign of X (negative, zero, or positive) as .1, 0, or 1.

testdb "pun">=#SELECT SIGN "pun">(-4.65 "pun">);
"pun">+---------------------------------------------------------+
| "pln"> SIGN(- "lit">4.65) "pln">                                             |
"pun">+---------------------------------------------------------+
| "pln"> - "pln">                                                      |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
testdb=# "pln">SELECT SIGN();
"pun">+---------------------------------------------------------+
| "pln"> SIGN() "pln">                                                 |
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                       |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
testdb=# "pln">SELECT SIGN( "lit">4.65);
"pun">+---------------------------------------------------------+
| "pln"> SIGN( "lit">4.65) "pln">                                              |
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                       |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
SIN(X)

This function returns the sine of X. Consider the following example:

testdb "pun">=#SELECT SIN "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> SIN() "pln">                                                 |
"pun">+---------------------------------------------------------+
| "pln"> 0.893997 "pln">                                                |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
SQRT(X)

This function returns the non-negative square root of X. Consider the following example:

testdb "pun">=#SELECT SQRT "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> SQRT() "pln">                                                |
"pun">+---------------------------------------------------------+
| "pln">  "pln">                                                       |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)
TAN(X)

This function returns the tangent of the argument X, which is expressed in radians.

testdb "pun">=#SELECT TAN "pun">( "pun">);
"pun">+---------------------------------------------------------+
| "pln"> TAN() "pln">                                                 |
"pun">+---------------------------------------------------------+
| "pln"> 1.619775 "pln">                                                |
"pun">+---------------------------------------------------------+
"pln"> row in "pln"> set "pln"> ( "lit">0.00 sec "pun">)

推荐阅读
  • PHP 5.2.5 安装与配置指南
    本文详细介绍了 PHP 5.2.5 的安装和配置步骤,帮助开发者解决常见的环境配置问题,特别是上传图片时遇到的错误。通过本教程,您可以顺利搭建并优化 PHP 运行环境。 ... [详细]
  • 构建基于BERT的中文NL2SQL模型:一个简明的基准
    本文探讨了将自然语言转换为SQL语句(NL2SQL)的任务,这是人工智能领域中一项非常实用的研究方向。文章介绍了笔者在公司举办的首届中文NL2SQL挑战赛中的实践,该比赛提供了金融和通用领域的表格数据,并标注了对应的自然语言与SQL语句对,旨在训练准确的NL2SQL模型。 ... [详细]
  • 数据库内核开发入门 | 搭建研发环境的初步指南
    本课程将带你从零开始,逐步掌握数据库内核开发的基础知识和实践技能,重点介绍如何搭建OceanBase的开发环境。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文介绍如何使用 Sortable.js 库实现元素的拖拽和位置交换功能。Sortable.js 是一个轻量级、无依赖的 JavaScript 库,支持拖拽排序、动画效果和多种插件扩展。通过简单的配置和事件处理,可以轻松实现复杂的功能。 ... [详细]
  • 探讨一个显示数字的故障计算器,它支持两种操作:将当前数字乘以2或减去1。本文将详细介绍如何用最少的操作次数将初始值X转换为目标值Y。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文详细介绍了如何在 Spring Boot 应用中通过 @PropertySource 注解读取非默认配置文件,包括配置文件的创建、映射类的设计以及确保 Spring 容器能够正确加载这些配置的方法。 ... [详细]
  • This document outlines the recommended naming conventions for HTML attributes in Fast Components, focusing on readability and consistency with existing standards. ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
author-avatar
川崎步
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有