I am issuing the following SELECT on SQLA (Teradata version: 15.10.01.11):
select cast('2018-05-31' as date format 'yyyy-mm-dd') - interval '6' month;
And I am getting: SELECT Failed. 2665: Invalid Date.
Any help would be much appreciated.
Br,
Shardul
According to Standard SQL subtracting 6 months from 2018-05-31 results in 2017-11-31 which obviously doesn't exist.
Interval arithmetic can't be used for month/years (Feb 29th), better use ADD_MONTHS:
ADD_MONTHS(DATE '2018-05-31', -6)
There also oADD_MONTHS, which might return a different result for months end dates, e.g.
ADD_MONTHS(DATE '2017-11-30', 6) -> 2018-05-30
oADD_MONTHS(DATE '2017-11-30', 6) -> 2018-05-31
Related
I was searching for a way to group data by interval (ex: every 30 minutes) using the date defined in that table, so i need to convert that date time to milliseconds so that i can divide it by the interval i need like in this query
SELECT FLOOR(UNIX_TIMESTAMP(timestamp)/(15 * 60 * 1000)) AS timekey
FROM table
GROUP BY timekey;
This query is running perfectly on SQL Server but on informix it's giving me the error
Routine (unix_timestamp) can not be resolved.
As it's not defined in IBM Informix server.
So i need a direct way to get epoch unix time from timestamp DATETIME YEAR TO FRACTION(3) column in IBM informix server like 'UNIX_TIMESTAMP' in SQL server.
If the timestamp column is of type DATETIME YEAR TO SECOND or similar, then you can convert it to a DECIMAL(18,5) number of seconds since the Unix Epoch, aka 1970-01-01 00:00:00Z (UTC; time zone offset +00:00) using a procedure such as this:
{
# "#(#)$Id: tounixtime.spl,v 1.6 2002/09/25 18:10:48 jleffler Exp $"
#
# Stored procedure TO_UNIX_TIME written by Jonathan Leffler (previously
# jleffler#informix.com and now jleffler#us.ibm.com). Includes fix for
# bug reported by Tsutomu Ogiwara <Tsutomu.Ogiwara#ctc-g.co.jp> on
# 2001-07-13. Previous version used DATETIME(0) SECOND TO SECOND
# instead of DATETIME(0:0:0) HOUR TO SECOND, and when the calculation
# extended the shorter constant to DATETIME HOUR TO SECOND, it added the
# current hour and minute fields, as documented in the Informix Guide to
# SQL: Syntax manual under EXTEND in the section on 'Expression'.
# Amended 2002-08-23 to handle 'eternity' and annotated more thoroughly.
# Amended 2002-09-25 to handle fractional seconds, as companion to the
# new stored procedure FROM_UNIX_TIME().
#
# If you run this procedure with no arguments (use the default), you
# need to worry about the time zone the database server is using because
# the value of CURRENT is determined by that, and you need to compensate
# for it if you are using a different time zone.
#
# Note that this version works for dates after 2001-09-09 when the
# interval between 1970-01-01 00:00:00+00:00 and current exceeds the
# range of INTERVAL SECOND(9) TO SECOND. Returning DECIMAL(18,5) allows
# it to work for all valid datetime values including fractional seconds.
# In the UTC time zone, the 'Unix time' of 9999-12-31 23:59:59 is
# 253402300799 (12 digits); the equivalent for 0001-01-01 00:00:00 is
# -62135596800 (11 digits). Both these values are unrepresentable in
# 32-bit integers, of course, so most Unix systems won't handle this
# range, and the so-called 'Proleptic Gregorian Calendar' used to
# calculate the dates ignores locale-dependent details such as the loss
# of days that occurred during the switch between the Julian and
# Gregorian calendar, but those are minutiae that most people can ignore
# most of the time.
}
CREATE PROCEDURE to_unix_time(d DATETIME YEAR TO FRACTION(5)
DEFAULT CURRENT YEAR TO FRACTION(5))
RETURNING DECIMAL(18,5);
DEFINE n DECIMAL(18,5);
DEFINE i1 INTERVAL DAY(9) TO DAY;
DEFINE i2 INTERVAL SECOND(6) TO FRACTION(5);
DEFINE s1 CHAR(15);
DEFINE s2 CHAR(15);
LET i1 = EXTEND(d, YEAR TO DAY) - DATETIME(1970-01-01) YEAR TO DAY;
LET s1 = i1;
LET i2 = EXTEND(d, HOUR TO FRACTION(5)) -
DATETIME(00:00:00.00000) HOUR TO FRACTION(5);
LET s2 = i2;
LET n = s1 * (24 * 60 * 60) + s2;
RETURN n;
END PROCEDURE;
Some of the commentary about email addresses is no longer valid – things have changed in the decade and a half since I wrote this.
Can somebody tell me how to subtract hours to a datetime in OSB/Xquery?
For example current date minus 3 hours.
There is a function available in XQuery called "subtract-dayTimeDurations"
E.g. :
subtract-dayTimeDurations( dayTimeDuration $srcval1,dayTimeDuration $srcval2)
Result will be returned as below:
op:subtract-dayTimeDurations(xf:dayTimeDuration("P2DT12H"), xf:dayTimeDuration("P1DT10H30M")) returns a dayTimeDuration value corresponding to 1 day and 1.5 hours.
From the result you can pick your required input.
You can explore combination of dayTimeDuration() with duration(). This way it's flexible.
here is how i would add 5 days in 2015-01-01
let $date := string(xs:date("2015-01-01") + xdt:dayTimeDuration(xs:duration(concat('P',abs(5),'D'))))
Here's my question.
I have a table (tblCourses) that has three columns: Course, StartDate, and EndDate
I want to create a query that gives me all the dates that are between StartDate and EndDate.
So if I have
Course: A
StartTime: 1/10/15
EndTime: 1/15/15
I want the query to show:
Course; NewDate
A _ 1/10/15
A _ 1/11/15
A _ 1/12/15
A _ 1/13/15
A _ 1/14/15
A _ 1/15/15
I've done a lot of google searching, and I've looked through stackoverlow forums, but I can't seem to find a solution. Any help would be GREATLY appreciated!
As Yermo states in his answer, one approach would be to use a [Dates] table that contains every valid date covering a period of time. Another approach would be to use a [Numbers] table containing integer values from zero to (some sufficiently large number), e.g.,
n
--
1
2
3
...
19
20
Once that table has been created you could use a query like
SELECT tblCourses.Course, DateAdd("d", Numbers.n, tblCourses.StartDate) AS NewDate
FROM tblCourses, Numbers
WHERE DateAdd("d", Numbers.n, tblCourses.StartDate) <= tblCourses.EndDate
which would return
Course NewDate
------ ----------
A 2015-01-10
A 2015-01-11
A 2015-01-12
A 2015-01-13
A 2015-01-14
A 2015-01-15
This is actually better solved in code than in the SQL query.
The reason it's not straight forward is you are trying to generate rows in a result set that are not in the table.
As a quick hack you could create another of dates and fill it to cover a few years. Then you could do a straight join which would get you the result you are looking for.
Is there a function in Teradata, that "makes" a date by giving day, month any year as parameters?
SO if I have integer parameters p1_day, p2_month, and p3_year (which are, by the way, attributes), is there a function like
date_function (p1_day, p2_month, and p3_year) -> for example '2013-12-11'
In TD13.10+ you can create a simple SQL UDF:
-- (year, month, day) to date, "invalid date" error if no valid date
REPLACE FUNCTION ymd_to_date(y INTEGER, m INTEGER, d INTEGER)
RETURNS DATE
SPECIFIC ymd_to_date
RETURNS NULL ON NULL INPUT
CONTAINS SQL
DETERMINISTIC
COLLATION INVOKER
INLINE TYPE 1
RETURN
((y - 1900) * 10000 + (m * 100) + d (DATE))
Dieter
One example in teradata:
BTEQ -- Enter your SQL request or BTEQ command:
sel cast ('11'||'-'||'12'||'-'||'12' as date format 'dd-mm-yy');
sel cast ('11'||'-'||'12'||'-'||'12' as date format 'dd-mm-yy');
*** Query completed. One row found. One column returned.
*** Total elapsed time was 1 second.
(((('11'||'-')||'12')||'-')||'12')
----------------------------------
11-12-12
So you can simply achieve your desired result by following query:
sel cast (cast(p1_day as char(2))||'-'||cast(p2_month as char(2))||'-'||
cast(p3_year as char(2)) as date format 'dd-mm-yy');
P.S. : There doesn't seem any such built-in function in teradata.
As far i as i know there is no predefined date function where you pass the integers and it returns the date.. however you can write you know UDF..
To add to Deiter's function, look at the below queries. Teradata stores dates as Integers with an offset from 1900.
SELECT CAST(CURRENT_DATE AS INTEGER);
---- 1150818
SELECT CURRENT_DATE;
--- 2015-08-18
---- the format of storage is (<year> - 1900)mmdd
SELECT (2015 -1900)*100000 + (8 * 100)+ 18;
------ 1150818
115 years from 1900, month followed by date.
Say I have an interval like
4 days 10:00:00
in postgres. How do I convert that to a number of hours (106 in this case?) Is there a function or should I bite the bullet and do something like
extract(days, my_interval) * 24 + extract(hours, my_interval)
Probably the easiest way is:
SELECT EXTRACT(epoch FROM my_interval)/3600
If you want integer i.e. number of days:
SELECT (EXTRACT(epoch FROM (SELECT (NOW() - '2014-08-02 08:10:56')))/86400)::int
To get the number of days the easiest way would be:
SELECT EXTRACT(DAY FROM NOW() - '2014-08-02 08:10:56');
As far as I know it would return the same as:
SELECT (EXTRACT(epoch FROM (SELECT (NOW() - '2014-08-02 08:10:56')))/86400)::int;
select floor((date_part('epoch', order_time - '2016-09-05 00:00:00') / 3600)), count(*)
from od_a_week
group by floor((date_part('epoch', order_time - '2016-09-05 00:00:00') / 3600));
The ::int conversion follows the principle of rounding.
If you want a different result such as rounding down, you can use the corresponding math function such as floor.
If you convert table field:
Define the field so it contains seconds:
CREATE TABLE IF NOT EXISTS test (
...
field INTERVAL SECOND(0)
);
Extract the value. Remember to cast to int other wise you can get an unpleasant surprise once the intervals are big:
EXTRACT(EPOCH FROM field)::int
If you want to display your result only in date type after adding the interval then, should try this
Select (current_date + interval 'x day')::date;
I'm working with PostgreSQL 11, and I created a function to get the hours betweeen 2 differents timestamps
create function analysis.calcHours(datetime1 timestamp, datetime2 timestamp)
returns integer
language plpgsql as $$
declare
diff interval;
begin
diff = datetime2 - datetime1;
return (abs(extract(days from diff))*24 + abs(extract(hours from diff)))::integer;
end; $$;
select date 'now()' - date '1955-12-15';
Here is the simple query which calculates total no of days.