Not able to filter records based on date filter in Informix - datetime

I want to put filter on an Informix query:
WHERE agentstatedetail.eventdatetime < '1753-01-01 00:00:00' - INTERVAL(3) DAY TO DAY
but it fails ...
Please tell where it goes wrong.

As noted in a comment, the solution is to ensure that the string is interpreted as a DATETIME value. The simple way to do that is to use the DATETIME literal notation:
DATETIME(1753-01-01 00:00:00) YEAR TO SECOND
To demonstrate:
CREATE TABLE agentstatedetail
(
eventdatetime DATETIME YEAR TO SECOND NOT NULL PRIMARY KEY,
eventname VARCHAR(64) NOT NULL
);
INSERT INTO agentstatedetail VALUES('1752-12-25 12:00:00', 'Christmas Day, Noon, 1752');
INSERT INTO agentstatedetail VALUES('1752-12-31 12:00:00', 'New Year''s Eve, Noon, 1752');
INSERT INTO agentstatedetail VALUES('1753-01-01 12:00:00', 'New Year''s Day, Noon, 1753');
SELECT * FROM agentstatedetail WHERE agentstatedetail.eventdatetime < '1753-01-01 00:00:00' - INTERVAL(3) DAY TO DAY;
This is your original WHERE clause embedded into a minimal SELECT statement. It yields the error:
SQL -1261: Too many digits in the first field of datetime or interval.
(NB: It would have been helpful to include the error message in the question.)
Here's an alternative version of the query, with the DATETIME literal in place:
SELECT * FROM agentstatedetail
WHERE agentstatedetail.eventdatetime < DATETIME(1753-01-01 00:00:00) YEAR TO SECOND -
INTERVAL(3) DAY TO DAY
;
Output from the sample data:
1752-12-25 12:00:00|Christmas DAY, Noon, 1752
I observe that the value calculated is a constant; you could rewrite the code as:
SELECT * FROM agentstatedetail
WHERE agentstatedetail.eventdatetime < DATETIME(1752-12-29 00:00:00) YEAR TO SECOND
I suspect that the value is passed as a parameter somewhere along the line.
Alternatively, you can cast the string to a DATETIME value and you'd get the same result:
SELECT * FROM agentstatedetail
WHERE agentstatedetail.eventdatetime < CAST('1753-01-01 00:00:00' AS DATETIME YEAR TO SECOND) -
INTERVAL(3) DAY TO DAY
;
or:
SELECT * FROM agentstatedetail
WHERE agentstatedetail.eventdatetime < '1753-01-01 00:00:00'::DATETIME YEAR TO SECOND -
INTERVAL(3) DAY TO DAY

Related

Carry Forward values in Presto

I am using the below query to pivot my data and generate a CSV but the problem is I have a dataset in which the data points are coming in a scattered way with each timestamp.
with map_date as (
SELECT
vin,
epoch,
timestamp,
date,
map_agg(signalName, value) as map_values
from hive.vehicle_signals.vehicle_signals_flat
where date(date) = date('2020-03-12')
and date(cast(from_unixtime(epoch) as timestamp) - interval '0' hour) = current_date - interval '2' day
and vin = '000011'
and signalName in ('timestamp','epoch','msgId','usec','vlan','vin','msgName','value')
GROUP BY vin, epoch, timestamp, date
order by timestamp desc
)
SELECT
epoch
, timestamp
, CASE WHEN element_at(map_values, 'value') IS NOT NULL THEN map_values['value'] ELSE NULL END AS value
, vin
, current_date - interval '2' day AS date
from map_date
I get the following CSV as a result. Is there a way I can carry forward the value until a new value is found at a newer timestamp? Like in the image below the value '14.3' comes and the next value '16.5' comes after a few timestamps, How can I carry the value '14.3' till row 7th and repeat the logic on the entire column. How can I make my output field look like column 'G' in the image using Presto?
Thanks in advance!!
You can use a mysql #variable to store the last value, for example:
SELECT
epoch
, timestamp
, CASE WHEN element_at(map_values, 'value') IS NOT NULL THEN #last_value:= map_values['value'] ELSE #last_value END AS value
, vin
, current_date - interval '2' day AS date
from map_date, (select #last_value:=0) v
The last part, (select #last_value:=0) v is to initialize the #last_value variable.
A basic tutorial
https://www.mysqltutorial.org/mysql-variables/
More advanced tutorial with additional info
https://www.xaprb.com/blog/2006/12/15/advanced-mysql-user-variable-techniques/

Teradata date subtract issue in PERIOD column

I have a teradata table ABC . I have a column in the table which is of PERIOD data type ( Name of the column is ef_dtm) . I need to update the starting bound of the period column(subtract it by 1 day) whenever starting bound of the period column is '12/31/9999'.
I am using the below query . But it is saying
INVALID Interval Literal.
Can you suggest me an update query?
Nonsequenced validtime
update ABC
set ef_dtm = PERIOD(CAST(end(ef_dtm) as Date) -INTERVAL '-1' DAY , end(ef_dtm))
where begin(ef_dtm) = '12/31/9999'
The error is because of part INTERVAL '-1' DAY
It should be INTERVAL -'1' DAY i.e. minus - outside the '1'
Your query has 2 more problems.
No need to cast period begin to DATE as INTERVAL arithmetic works on TIMESTAMP
DATE literals are wrong. It should be YYYY-MM-DD; Moreover it should be TIMESTAMP corresponding to period column datatype.
Correct query is as below.
nonsequenced validtime
UPDATE ABC
SET ef_dtm = PERIOD(begin(ef_dtm) + INTERVAL -'1' DAY, end(ef_dtm))
WHERE begin(ef_dtm) = TIMESTAMP '1999-12-31 00:00:00.000000';
OR
nonsequenced validtime
UPDATE ABC
SET ef_dtm = PERIOD(begin(ef_dtm) - INTERVAL '1' DAY, end(ef_dtm))
WHERE begin(ef_dtm) = TIMESTAMP '1999-12-31 00:00:00.000000';
DEMO
Create Table:
CREATE TABLE ABC ( ef_dtm period(timestamp(6)) AS validtime ) NO PRIMARY INDEX;
Insert Data:
INSERT INTO abc(period (TIMESTAMP '1999-12-31 00:00:00.000000', TIMESTAMP '1999-12-31 23:59:00.000000'));
After select
ef_dtm
------------------------------------------------------------
('1999-12-31 00:00:00.000000', '1999-12-31 23:59:00.000000')
Update Data:
nonsequenced validtime
UPDATE ABC
SET ef_dtm = PERIOD(begin(ef_dtm) + INTERVAL -'1' DAY, end(ef_dtm))
WHERE begin(ef_dtm) = TIMESTAMP '1999-12-31 00:00:00.000000';
After SELECT
ef_dtm
------------------------------------------------------------
('1999-12-30 00:00:00.000000', '1999-12-31 23:59:00.000000')

save datetime value 12 in Oracle database

When I try to insert datetime value 12:58 AM into the oracle table it gets inserted as 00:58. How can I insert datetime value as 12 in my oracle db? I've set my Oracle time format as 24 hr time. Any suggestions would help.
Insert statement :
INSERT INTO TABLE
(
DATE_CREATED,
PLANNED_START,
PLANNED_COMPLETION
)
VALUES
(
sysdate,
TO_CHAR(p_planned_Start_Date, 'DD-MM-YYYY HH24:MI:SS'),
TO_CHAR(end_date_, 'DD-MM-YYYY HH24:MI:SS')
);
For the 24-hour time, you need to use HH24 instead of HH.
For the 12-hour time, the AM/PM indicator is written as A.M. (if you want periods in the result) or AM (if you don't). For example:
INSERT INTO TEST (LD_DATE) Values (TO_DATE('08/30/2016', 'MM/DD/YYYY '));
And select it as below:
SELECT LD_DATE,
TO_CHAR(LD_DATE, 'DD-MM-YYYY HH24:MI:SS') "Date 24Hr",
TO_CHAR(LD_DATE, 'DD-MM-YYYY HH:MI:SS AM') "Date 12Hr"
FROM test
;

Get DateTime corresponding to last Thu or Fri or Mon etc in SQL Server 2012

I have a certain DATETIME value, and I would like to get the DATETIME value for a given weekday 'n' (where n is an integer from 1 thru to 7) that is just before the given date.
Question: How would I do this given a value for currentDate and a value for lastWeekDay?
For example, if given date is 06/15/2015 in mm/dd/yyyy format, then what is the date for a weekday of 6 that came just before 06/15/2015. In this example, given date is on Monday and we want the date for last Friday (i.e. weekday =6).
declare #currentDate datetime, #lastWeekDay int;
set #currentDate = getdate();
set #lastWeekDay = 6;--this could be any value from 1 thru to 7
select #currentDate as CurrentDate, '' as LastWeekDayDate --i need to get this date
UPDATE 1
In addition to the excellent answer by Anon, I also found an alternate way of doing it, which is as given below.
DECLARE #currentWeekDay INT;
SET #currentWeekDay = DATEPART(WEEKDAY, #currentDate);
--Case 1: when current date week day > lastWeekDay then subtract
-- the difference between the two weekdays
--Case 2: when current date week day <= lastWeekDay then go back 7 days from
-- current date, and then add (lastWeekDay - currentWeekDay)
SELECT
#currentDate AS CurrentDate,
CASE
WHEN #currentWeekDay > #lastWeekDay THEN DATEADD(DAY, -1 * ABS(CAST(#lastWeekDay AS INT) - CAST(#currentWeekDay AS INT)), #currentDate)
ELSE DATEADD(DAY, #lastWeekDay - DATEPART(WEEKDAY, DATEADD(DAY, -7, #currentDate)), DATEADD(DAY, -7, #currentDate))
END AS LastWeekDayDate;
Calculate how many days have passed since a fixed date, modulo 7, and subtract that from the input date. The magic number '5' is because Date Zero (1900-01-01) is a Monday. Shifting that forward 5 days makes the #lastWeekDay range [1..7] map to the range of weekdays [Sunday..Saturday].
SELECT DATEADD(day,-DATEDIFF(day,5+#lastWeekDay,#currentDate)%7,#currentDate)
I avoid the DATEPART(weekday,[...]) function because of SET DATEFIRST

Select weekend or weekday data from a table based on date param

How can I select data from a table based on weekday or weekend, like
if date is a weekday then select only historical weekday data from the table &
if date is a weekend then select only historical weekend data.
I have tried to do that in this way but no luck
DECLARE #MyDate DATE = '08/17/2013'
SELECT datename(dw,#MyDate)
SELECT * FROM MyTable
WHERE
datename(dw,DateColumnInTable) IN (
CASE WHEN (datename(dw,#MyDate) IN ('Saturday','Sunday')) THEN '''Saturday'',''Sunday'''
ELSE 'Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'
END )
Any I can see lots of data in my table for saturday and sunday but this query is giving me blank record set.
Here's one way:
DECLARE #MyDate DATE = '08/17/2013'
IF (DATEPART(weekday, #MyDate) IN (1,7))
SELECT *
FROM MyTable
WHERE DATEPART(weekday, DateColumnInTable) IN (1,7)
ELSE
SELECT *
FROM MyTable
WHERE DATEPART(weekday, DateColumnInTable) BETWEEN 2 AND 6
If you would like to do it in one clause you can do something like the following, but it may perform worse:
SELECT *
FROM MyTable
WHERE (DATEPART(weekday, #MyDate) IN (1,7) AND DATEPART(weekday, DateColumnInTable) IN (1,7))
OR (DATEPART(weekday, #MyDate) BETWEEN 2 AND 6 AND DATEPART(weekday, DateColumnInTable) BETWEEN 2 AND 6)

Resources