How to get the absolute value of an interval
Consider the following statement:
select interval '-1 hours'
I couldn't figure out how to get the absolute value of an interval, i.e. to toggle or remove the sign if negative. The only thing that came to my mind is the following:
select abs(extract(epoch from interval '-1 hours'))
But I wonder if there is a more elegant way (a way that preserves the interval type)?
postgresql
add a comment |
Consider the following statement:
select interval '-1 hours'
I couldn't figure out how to get the absolute value of an interval, i.e. to toggle or remove the sign if negative. The only thing that came to my mind is the following:
select abs(extract(epoch from interval '-1 hours'))
But I wonder if there is a more elegant way (a way that preserves the interval type)?
postgresql
add a comment |
Consider the following statement:
select interval '-1 hours'
I couldn't figure out how to get the absolute value of an interval, i.e. to toggle or remove the sign if negative. The only thing that came to my mind is the following:
select abs(extract(epoch from interval '-1 hours'))
But I wonder if there is a more elegant way (a way that preserves the interval type)?
postgresql
Consider the following statement:
select interval '-1 hours'
I couldn't figure out how to get the absolute value of an interval, i.e. to toggle or remove the sign if negative. The only thing that came to my mind is the following:
select abs(extract(epoch from interval '-1 hours'))
But I wonder if there is a more elegant way (a way that preserves the interval type)?
postgresql
postgresql
edited Nov 21 '18 at 9:23
moooeeeep
asked Aug 17 '12 at 11:20
moooeeeepmoooeeeep
21.4k1073132
21.4k1073132
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
A CASE
expression would look more self-explanatory. Example:
SELECT
i,
(CASE WHEN (i < INTERVAL '0') THEN (-i) ELSE i END) AS abs_i
FROM
(VALUES
(INTERVAL '-2 h'),
(INTERVAL '2 m')
) AS foo (i)
which produces:
i | abs_i
-----------+----------
-02:00:00 | 02:00:00
00:02:00 | 00:02:00
add a comment |
You can find the greatest value between i and -i. For example:
SELECT greatest(-'1 hour'::interval, '1 hour'::interval);
add a comment |
It way isn't more elegant than yours, but it returns an interval type
select interval '-1 hours'*sign(extract(epoch from interval '-1 hours'))
you're right, I've corrected my answer
– khomyakoshka
Aug 17 '12 at 12:03
add a comment |
There's a discussion on the pgsql-general mailing-list: Absolute value of intervals on why a built-in abs(interval)
function is not provided with PostgreSQL.
In short, there's no consensus about what it should do in some cases, when considering the componentized nature of the interval type.
But anyone can create their function implementing their own idea about what it should compute, for instance, building on the expression from LisMorski's answer:
CREATE FUNCTION abs(interval) RETURNS interval AS
$$ select case when ($1<interval '0') then -$1 else $1 end; $$
LANGUAGE sql immutable;
Simple SQL functions are generally inlined during query execution, so the performance should be comparable to having the expression inside the query.
Example:
#= select abs(interval '-2 days +3 minutes');
abs
------------------
2 days -00:03:00
# select abs(now()-clock_timestamp());
abs
-----------------
00:00:00.000146
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12004806%2fhow-to-get-the-absolute-value-of-an-interval%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
A CASE
expression would look more self-explanatory. Example:
SELECT
i,
(CASE WHEN (i < INTERVAL '0') THEN (-i) ELSE i END) AS abs_i
FROM
(VALUES
(INTERVAL '-2 h'),
(INTERVAL '2 m')
) AS foo (i)
which produces:
i | abs_i
-----------+----------
-02:00:00 | 02:00:00
00:02:00 | 00:02:00
add a comment |
A CASE
expression would look more self-explanatory. Example:
SELECT
i,
(CASE WHEN (i < INTERVAL '0') THEN (-i) ELSE i END) AS abs_i
FROM
(VALUES
(INTERVAL '-2 h'),
(INTERVAL '2 m')
) AS foo (i)
which produces:
i | abs_i
-----------+----------
-02:00:00 | 02:00:00
00:02:00 | 00:02:00
add a comment |
A CASE
expression would look more self-explanatory. Example:
SELECT
i,
(CASE WHEN (i < INTERVAL '0') THEN (-i) ELSE i END) AS abs_i
FROM
(VALUES
(INTERVAL '-2 h'),
(INTERVAL '2 m')
) AS foo (i)
which produces:
i | abs_i
-----------+----------
-02:00:00 | 02:00:00
00:02:00 | 00:02:00
A CASE
expression would look more self-explanatory. Example:
SELECT
i,
(CASE WHEN (i < INTERVAL '0') THEN (-i) ELSE i END) AS abs_i
FROM
(VALUES
(INTERVAL '-2 h'),
(INTERVAL '2 m')
) AS foo (i)
which produces:
i | abs_i
-----------+----------
-02:00:00 | 02:00:00
00:02:00 | 00:02:00
answered Aug 17 '12 at 11:44
LisMorskiLisMorski
30916
30916
add a comment |
add a comment |
You can find the greatest value between i and -i. For example:
SELECT greatest(-'1 hour'::interval, '1 hour'::interval);
add a comment |
You can find the greatest value between i and -i. For example:
SELECT greatest(-'1 hour'::interval, '1 hour'::interval);
add a comment |
You can find the greatest value between i and -i. For example:
SELECT greatest(-'1 hour'::interval, '1 hour'::interval);
You can find the greatest value between i and -i. For example:
SELECT greatest(-'1 hour'::interval, '1 hour'::interval);
answered Apr 30 '14 at 1:47
Daniel HernándezDaniel Hernández
655410
655410
add a comment |
add a comment |
It way isn't more elegant than yours, but it returns an interval type
select interval '-1 hours'*sign(extract(epoch from interval '-1 hours'))
you're right, I've corrected my answer
– khomyakoshka
Aug 17 '12 at 12:03
add a comment |
It way isn't more elegant than yours, but it returns an interval type
select interval '-1 hours'*sign(extract(epoch from interval '-1 hours'))
you're right, I've corrected my answer
– khomyakoshka
Aug 17 '12 at 12:03
add a comment |
It way isn't more elegant than yours, but it returns an interval type
select interval '-1 hours'*sign(extract(epoch from interval '-1 hours'))
It way isn't more elegant than yours, but it returns an interval type
select interval '-1 hours'*sign(extract(epoch from interval '-1 hours'))
edited Aug 17 '12 at 12:01
answered Aug 17 '12 at 11:46
khomyakoshkakhomyakoshka
1,143616
1,143616
you're right, I've corrected my answer
– khomyakoshka
Aug 17 '12 at 12:03
add a comment |
you're right, I've corrected my answer
– khomyakoshka
Aug 17 '12 at 12:03
you're right, I've corrected my answer
– khomyakoshka
Aug 17 '12 at 12:03
you're right, I've corrected my answer
– khomyakoshka
Aug 17 '12 at 12:03
add a comment |
There's a discussion on the pgsql-general mailing-list: Absolute value of intervals on why a built-in abs(interval)
function is not provided with PostgreSQL.
In short, there's no consensus about what it should do in some cases, when considering the componentized nature of the interval type.
But anyone can create their function implementing their own idea about what it should compute, for instance, building on the expression from LisMorski's answer:
CREATE FUNCTION abs(interval) RETURNS interval AS
$$ select case when ($1<interval '0') then -$1 else $1 end; $$
LANGUAGE sql immutable;
Simple SQL functions are generally inlined during query execution, so the performance should be comparable to having the expression inside the query.
Example:
#= select abs(interval '-2 days +3 minutes');
abs
------------------
2 days -00:03:00
# select abs(now()-clock_timestamp());
abs
-----------------
00:00:00.000146
add a comment |
There's a discussion on the pgsql-general mailing-list: Absolute value of intervals on why a built-in abs(interval)
function is not provided with PostgreSQL.
In short, there's no consensus about what it should do in some cases, when considering the componentized nature of the interval type.
But anyone can create their function implementing their own idea about what it should compute, for instance, building on the expression from LisMorski's answer:
CREATE FUNCTION abs(interval) RETURNS interval AS
$$ select case when ($1<interval '0') then -$1 else $1 end; $$
LANGUAGE sql immutable;
Simple SQL functions are generally inlined during query execution, so the performance should be comparable to having the expression inside the query.
Example:
#= select abs(interval '-2 days +3 minutes');
abs
------------------
2 days -00:03:00
# select abs(now()-clock_timestamp());
abs
-----------------
00:00:00.000146
add a comment |
There's a discussion on the pgsql-general mailing-list: Absolute value of intervals on why a built-in abs(interval)
function is not provided with PostgreSQL.
In short, there's no consensus about what it should do in some cases, when considering the componentized nature of the interval type.
But anyone can create their function implementing their own idea about what it should compute, for instance, building on the expression from LisMorski's answer:
CREATE FUNCTION abs(interval) RETURNS interval AS
$$ select case when ($1<interval '0') then -$1 else $1 end; $$
LANGUAGE sql immutable;
Simple SQL functions are generally inlined during query execution, so the performance should be comparable to having the expression inside the query.
Example:
#= select abs(interval '-2 days +3 minutes');
abs
------------------
2 days -00:03:00
# select abs(now()-clock_timestamp());
abs
-----------------
00:00:00.000146
There's a discussion on the pgsql-general mailing-list: Absolute value of intervals on why a built-in abs(interval)
function is not provided with PostgreSQL.
In short, there's no consensus about what it should do in some cases, when considering the componentized nature of the interval type.
But anyone can create their function implementing their own idea about what it should compute, for instance, building on the expression from LisMorski's answer:
CREATE FUNCTION abs(interval) RETURNS interval AS
$$ select case when ($1<interval '0') then -$1 else $1 end; $$
LANGUAGE sql immutable;
Simple SQL functions are generally inlined during query execution, so the performance should be comparable to having the expression inside the query.
Example:
#= select abs(interval '-2 days +3 minutes');
abs
------------------
2 days -00:03:00
# select abs(now()-clock_timestamp());
abs
-----------------
00:00:00.000146
answered Jun 27 '17 at 19:01
Daniel VéritéDaniel Vérité
39.4k1189107
39.4k1189107
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12004806%2fhow-to-get-the-absolute-value-of-an-interval%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown