abs value, power, sqrt
abs value, power, sqrt
Hi folks, I previously posted this to pgsql-novice but haven't gotten an answer, so I thought I'd try here. I need a way to get abs value of a floating point number and (possibly separate function for) abs value of an integer in a select statement. Is there a function to do this? If not, how can I write a function to do it? Also how do I do squares and square roots? I saw some \|/ type notation for square root in an SQL book, but I didn't get postgres to take that. thanks, Michael _________________________________________________________ DO YOU YAHOO!? Get your free @yahoo.com address at http://mail.yahoo.com
Re: [SQL] abs value, power, sqrt
Michael Olivier wrote:
> I need a way to get abs value of a floating point number and
> (possibly separate function for) abs value of an integer in a select
> statement.
You had to look up the manuals at numerical *operators* -- no need to
define YAF ;-)
=> select @-17.0;
17
> Also how do I do squares and square roots? I saw some \|/ type
> notation for square root in an SQL book, but I didn't get postgres
> to take that.
try e.g.
=> select 2*2, 2^2;
4 4
and:
=> select |/2;
1.4142135623731
without the backslash (originally quoting in a shell?) it seems to
work ...
Hope it helps
Ulf
--
======================================================================
%%%%% Ulf Mehlig
%%%%!%%% Projekt "MADAM"
%%%% %!% %%%% ----------------------------------------------------
---| %%% MADAM: MAngrove | Center for Tropical Marine
||--%!% Dynamics | Biology
|| And | Fahrenheitstrasse 1
_ /||\_/\_ Management |
/ / \ \ ~~~~~~~~~~~~~~~~~ | 28359 Bremen/Germany
~~~~~~~~~~~~~~~~~~~~
Re: [SQL] abs value, power, sqrt
| PostgreSQL User's Guide | ||
|---|---|---|
| Prev | Next |
Chapter 5. Operators
Postgres provides a large number of built-in operators on system types. These operators are declared in the system catalog pg_operator. Every entry in pg_operator includes the name of the procedure that implements the operator and the class OIDs of the input and output types.
To view all variations of the “||” string concatenation operator, try
SELECT oprleft, oprright, oprresult, oprcode FROM pg_operator WHERE oprname = '||'; oprleft|oprright|oprresult|oprcode -------+--------+---------+------- 25| 25| 25|textcat 1042| 1042| 1042|textcat 1043| 1043| 1043|textcat (3 rows)
Table 5-1. Postgres Operators
| Operator | Description | Usage |
|---|---|---|
| < | Less than? | 1 < 2 |
| <= | Less than or equal to? | 1 <= 2 |
| <> | Not equal? | 1 <> 2 |
| = | Equal? | 1 = 1 |
| > | Greater than? | 2 > 1 |
| >= | Greater than or equal to? | 2 >= 1 |
| || | Concatenate strings | 'Postgre' || 'SQL' |
| !!= | NOT IN | 3 !!= i |
| ~~ | LIKE | 'scrappy,marc,hermit' ~~ '%scrappy%' |
| !~~ | NOT LIKE | 'bruce' !~~ '%al%' |
| ~ | Match (regex), case sensitive | 'thomas' ~ '*.thomas*.' |
| ~* | Match (regex), case insensitive | 'thomas' ~* '*.Thomas*.' |
| !~ | Does not match (regex), case sensitive | 'thomas' !~ '*.Thomas*.' |
| !~* | Does not match (regex), case insensitive | 'thomas' !~ '*.vadim*.' |
Table 5-2. Postgres Numerical Operators
| Operator | Description | Usage |
|---|---|---|
| ! | Factorial | 3 ! |
| !! | Factorial (left operator) | !! 3 |
| % | Modulo | 5 % 4 |
| % | Truncate | % 4.5 |
| * | Multiplication | 2 * 3 |
| + | Addition | 2 + 3 |
| - | Subtraction | 2 - 3 |
| / | Division | 4 / 2 |
| : | Natural Exponentiation | : 3.0 |
| ; | Natural Logarithm | (; 5.0) |
| @ | Absolute value | @ -5.0 |
| ^ | Exponentiation | 2.0 ^ 3.0 |
| |/ | Square root | |/ 25.0 |
| ||/ | Cube root | ||/ 27.0 |
Table 5-3. Postgres Geometric Operators
| Operator | Description | Usage |
|---|---|---|
| + | Translation | '((0,0),(1,1))'::box + '(2.0,0)'::point |
| - | Translation | '((0,0),(1,1))'::box - '(2.0,0)'::point |
| * | Scaling/rotation | '((0,0),(1,1))'::box * '(2.0,0)'::point |
| / | Scaling/rotation | '((0,0),(2,2))'::box / '(2.0,0)'::point |
| # | Intersection | '((1,-1),(-1,1))' # '((1,1),(-1,-1))' |
| # | Number of points in polygon | # '((1,0),(0,1),(-1,0))' |
| ## | Point of closest proximity | '(0,0)'::point ## '((2,0),(0,2))'::lseg |
| && | Overlaps? | '((0,0),(1,1))'::box && '((0,0),(2,2))'::box |
| &< | Overlaps to left? | '((0,0),(1,1))'::box &< '((0,0),(2,2))'::box |
| &> | Overlaps to right? | '((0,0),(3,3))'::box &> '((0,0),(2,2))'::box |
| <-> | Distance between | '((0,0),1)'::circle <-> '((5,0),1)'::circle |
| << | Left of? | '((0,0),1)'::circle << '((5,0),1)'::circle |
| <^ | Is below? | '((0,0),1)'::circle <^ '((0,5),1)'::circle |
| >> | Is right of? | '((5,0),1)'::circle >> '((0,0),1)'::circle |
| >^ | Is above? | '((0,5),1)'::circle >^ '((0,0),1)'::circle |
| ?# | Intersects or overlaps | '((-1,0),(1,0))'::lseg ?# '((-2,-2),(2,2))'::box; |
| ?- | Is horizontal? | '(1,0)'::point ?- '(0,0)'::point |
| ?-| | Is perpendicular? | '((0,0),(0,1))'::lseg ?-| '((0,0),(1,0))'::lseg |
| @-@ | Length or circumference | @-@ '((0,0),(1,0))'::path |
| ?| | Is vertical? | '(0,1)'::point ?| '(0,0)'::point |
| ?|| | Is parallel? | '((-1,0),(1,0))'::lseg ?|| '((-1,2),(1,2))'::lseg |
| @ | Contained or on | '(1,1)'::point @ '((0,0),2)'::circle |
| @@ | Center of | @@ '((0,0),10)'::circle |
| ~= | Same as | '((0,0),(1,1))'::polygon ~= '((1,1),(0,0))'::polygon |
The time interval data type tinterval is a legacy from the original date/time types and is not as well supported as the more modern types. There are several operators for this type.
Table 5-4. Postgres Time Interval Operators
| Operator | Description | Usage |
|---|---|---|
| #< | Interval less than? | |
| #<= | Interval less than or equal to? | |
| #<> | Interval not equal? | |
| #= | Interval equal? | |
| #> | Interval greater than? | |
| #>= | Interval greater than or equal to? | |
| <#> | Convert to time interval | |
| << | Interval less than? | |
| | | Start of interval | |
| ~= | Same as | |
| <?> | Time inside interval? |
Users may invoke operators using the operator name, as in:
select * from emp where salary < 40000;Alternatively, users may call the functions that implement the operators directly. In this case, the query above would be expressed as:
select * from emp where int4lt(salary, 40000);
psql has a \dd command to show these operators.
| Prev | Home | Next |
| Geometric Types | Functions |
Re[2]: [SQL] abs value, power, sqrt
Hello Bob, sabato, 21 novembre 98, you wrote: BS> On Thu, 19 Nov 1998, Michael Olivier wrote: BS> :Hi folks, BS> : BS> :I previously posted this to pgsql-novice but haven't gotten an answer, BS> :so I thought I'd try here. BS> : BS> :I need a way to get abs value of a floating point number and (possibly BS> :separate function for) abs value of an integer in a select statement. BS> :Is there a function to do this? If not, how can I write a function to BS> :do it? BS> : BS> :Also how do I do squares and square roots? I saw some \|/ type notation BS> :for square root in an SQL book, but I didn't get postgres to take that. ------------------------------------------------------------------------ funct PostgreSQL result remarks ------------------------------------------------------------------------ ABS: select @ -12 12 absolute value MODULE: select 100 % 3 1 module POWER: select dpow(12,2) 144 power ROUND: select dround(12.7) 13 round SQRT: select dsqrt(144) 12 square root TRUNC: select dtrunc(1.2) 1 truncate int value ROOT3: select dcbrt(27) 3 cube root LOG: select dlog1(100) 4.60517018598809 logarithm EXP: select dexp(2) 7.38905609893065 exponent ------------------------------------------------------------------------ Jose'