Informix: datetime manipulation - datetime

I have 2 fields in a table test1:
onlydate DATE
onlytime DATETIME HOUR TO MINUTE
and 1 field in a table test2:
dateandtime DATETIME YEAR TO SECOND
Now I need to append the value of onlydate and onlytime field and set it to dateandtime field. How do I do that?

The basic issues are:
Converting DATE to DATETIME -- use EXTEND.
Converting DATETIME to INTERVAL -- use subtraction.
Assembling these two concepts and applied to SELECT only:
create temp table td(dateonly date not null, timeonly datetime hour to minute);
insert into td values('2010-05-31', '06:30');
select extend(dateonly, year to second) +
(timeonly - datetime(00:00) hour to minute) from td;
The result is what you want:
DATETIME YEAR TO SECOND
2010-05-31 06:30:00
Subtracting midnight from timeonly converts it into an INTERVAL HOUR TO MINUTE; you can add a DATETIME YEAR TO SECOND and an INTERVAL HOUR TO MINUTE, getting a DATETIME YEAR TO SECOND. You cannot add two DATETIME values.
So, strictly answering your question, you'd write:
INSERT INTO Test2(DateAndTime)
SELECT EXTEND(DateOnly, YEAR TO SECOND) +
(TimeOnly - DATETIME(00:00) HOUR TO MINUTE) AS DateAndTime
FROM Test1;
(I run with DBDATE=Y4MD- so that the date literal shown works as expected. To insert the DATE constant reliably regardless of the setting of DBDATE, use MDY(5,31,2010).)

You can concatenate both values as text, and cast it to datetime like:
update datetime_test
set dateandtime = (dateonly || ' ' || timeonly || ':00')::
datetime year to second

Related

Negative dates in sqllite database

I am working locally with an sqllite DB. I have imported some records from teradata where there was a date field in the format of 'YYYY-MM-DD'. When i imported the records the date switched from a date to a number. I know this is a feature of sqllite and that one can access it via date(sqllite_date) when selecting it in a where clause.
My problem is that the dates now appear to be a bit odd. For example the year appears to be negative.
Is there anyway to recover this to the correct format?
Below is an example of converting a number in the database into a date
SELECT date(18386)
# -4662-03-28
SELECT datetime('now')
# 2021-02-11 10:41:52
SELECT date(sqllite_date) FROM mydb
# Returns -4662-03-28
# Should return 2020-05-04
I am very new to this area so apologies if this is a basic question. Thank you very much for your time
In SQLite you can store dates as TEXT, REAL or INTEGER.
It seems that you stored the dates in a column with INTEGER or REAL affinity.
In this case, if you use the function date(), it considers a value like 18386 as a Julian day, meaning the number of days since noon in Greenwich on November 24, 4714 B.C.
This is why date(18386) returns 4662-03-28B.C.
But I suspect that the date values that you have are the number of days since '1970-01-01'.
In this case, 18386 days after '1970-01-01' is '2020-05-04'.
So you can get the dates in the format YYYY-MM-DD if you add the value of your column as days to '1970-01-01':
SELECT date('1970-01-01', datecolumn || ' day') FROM tablename
Or by transforming your date values to seconds and treat them as UNIX time (the number of seconds since '1970-01-01 00:00:00 UTC'):
SELECT date(datecolumn * 24 * 3600, 'unixepoch') FROM tablename
Replace datecolumn with the name of your column.

How get values from database scadalts from last day

I need to get data from DB scadalts from last day.
I have data in table pointValues where is column pointValue and ts but is not timestamp.
Column ts is type BIGINT(20)
Checking ts is unixtime
SELECT
pointValue,
ts,
from_unixtime(ts),
YEAR(from_unixtime(ts)),
MONTH(from_unixtime(ts)),
DAY(from_unixtime(ts))
FROM
pointValues;
The result null is wrong is not unixtime.
I don't know how to create condition where because - I don't know how to interpret value in column ts.
Column ts should be interpreted with greater accuracy.
eg:
SELECT
pointValue,
ts,
from_unixtime(ts/1000),
YEAR(from_unixtime(ts/1000)),
MONTH(from_unixtime(ts/1000)),
DAY(from_unixtime(ts/1000))
FROM
pointValues;
And we may get values from last day eg:
SELECT
pointValue,
ts,
YEAR(from_unixtime(ts/1000)),
MONTH(from_unixtime(ts/1000)),
DAY(from_unixtime(ts/1000))
FROM
pointValues
WHERE
YEAR(from_unixtime(ts/1000)) = YEAR(NOW() - INTERVAL 1 day) and
MONTH(from_unixtime(ts/1000)) = MONTH(NOW() - INTERVAL 1 day) and
DAY(from_unixtime(ts/1000)) = DAY(NOW() - INTERVAL 1 day)
Thanks
Maybe it will be useful also

How to deal with date in SQLite?

I have a table in Sqlite DB having two fields Id and Date (Date is of type Text).
I have stored a few dates in the table from c#. now i want to get the records matching specific day, month and year.
The query i have tried is:
select strftime('%m', Date) from testTbl Where id = 3;
also:
select Date(substr(Date, 0, 10)) as daa from testTbl Where id = 3;
but the result of these two quires is always null.. can anyone help me to sort this out?
Proposed (immediate) fix
Use the following select
select substr(Date, 0, 10) as daa from testTbl Where id = 3;
Cause of the issue
The problem (if you surround the above substr with a Date function) is that you're using a Text type that is not in the expected format
Time Strings
A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
Alternative (better) approach
Anyway IMHO, it would be better to create the column with a Date type and to insert values in the following way
insert into testTbl values (DateTime("2015-12-31"),3);
so that you'll be able to do
SELECT strftime('%m/%d/%Y',Date) from testTbl where id = 3;
or also
SELECT Date from testTbl where Date > DateTime('2016-01-01');
from C# the parameterized command would be similar to
"insert into testTbl values (DateTime(?),?);"
with a parameter value myDate.ToString("yyyy-MM-dd")
Anyway you can actually get the month string with substr(Date,0,2) and the year with substr(Date,5,4) with your current format. I'm simply suggesting an alternative that I would find more standard (using the built-in Date format)

How to add/subtract date/time components using a calculated interval?

I'd like to get this to work in Teradata:
Updated SQL for better example
select
case
when
current_date between
cast('03-10-2013' as date format 'mm-dd-yyyy') and
cast('11-03-2013' as date format 'mm-dd-yyyy')
then 4
else 5
end Offset,
(current_timestamp + interval Offset hour) GMT
However, I get an error of Expected something like a string or a Unicode character blah blah. It seems that you have to hardcode the interval like this:
select current_timestamp + interval '4' day
Yes, I know I hardcoded it in my first example, but that was only to demonstrate a calculated result.
If you must know, I am having to convert all dates and times in a few tables to GMT, but I have to account for daylight savings time. I am in Eastern, so I need to add 4 hours if the date is within the DST timeframe and add 5 hours otherwise.
I know I can just create separate update statements for each period and just change the value from a 4 to a 5 accordingly, but I want my query to be dynamic and smart.
Here's the solution:
select
case
when
current_date between
cast('03-10-2013' as date format 'mm-dd-yyyy') and
cast('11-03-2013' as date format 'mm-dd-yyyy')
then 4
else 5
end Offset,
(current_timestamp + cast(Offset as interval hour)) GMT
You have to actually cast the case statement's return value as an interval. I didn't even know interval types existed in Teradata. Thanks to this page for helping me along:
http://www.teradataforum.com/l081007a.htm
If I understand correctly, you want to multiply the interval by some number. Believe it or not, that's literally all you need to do:
select current_timestamp as right_now
, right_now + (interval '1' day) as same_time_tomorrow
, right_now + (2 * (interval '1' day)) as same_time_next_day
Intervals have always challenged me for some reason; I don't use them very often. But I've had this little example in my Teradata "cheat sheet" for quite a while.
Two remarks:
You could return an INTERVAL instead of an INT
The recommended way to write a date literal in Teradata is DATE 'YYYY-MM-DD' instead of CAST/FORMAT
select
case
when current_date between DATE '2013-03-10' and DATE '2013-11-03'
then interval '4' hour
else interval '5'hour
end AS Offset,
current_timestamp + Offset AS GMT

SQLite : obtain current week

I'm trying to obtain the current week for date comparison in SQLite.
I have no problem for last month, last year, today, yesterday... but don't find the solution to have the current week.
I tried lot of things like:
SELECT tastings.* FROM tastings
WHERE (DATE(tastings.date) > DATE('now','weekday 1','+ 7 days'))
Can you help me ? Thanks.
This code gives you the week number where the first day of week is monday. It also works well for last and first weeks of the year.
strftime('%W', 'now', 'localtime', 'weekday 0', '-6 days')
I guess you want compare 2 date, Assume you have a table named _testTbl and have 3 column _id INTEGER, _name TEXT, _recordDate TEXT
you want name that record this week
you can use below code:
SELECT * FROM _testTbl
WHERE _recordDate > datetime('now', 'start of day', 'weekday 6', '-7 day')
note that this week start by saturday (sunday 0, monday 1, ..., saturday 7)
this t-sql means:
datetime is a sqlite date and time function.
first parameter is given time: 'now' means the current time.
second parameter take the time to start of day.
third parameter take time to the next weekday number (in this case, saturday).
fourth parameter take time to start of week
What is stored inside the tastings.date column? Note that SQLite does not have “timestamp” type affinity, so probably you store Text (some representation of the date) or integer (julian day, epoch…)
All time and date functions expect a valid time string and convert that time string to another string format. If tastings.date contains a week number then use:
AND cast(tastings.date AS TEXT) = strftime('%W','now')
This helps me to compare the 2 dates using the week of the year.
AND ( strftime('%W', tastings.date) = strftime('%W', 'now') )
Thanks you.

Resources