How to compare two Timestamp In kusto - kql

I have one Timestamp column in Table and I want to compare that Timestamp value with ingestion_time()
For example:
Timestamp ingestion_time
1970-01-01 00:00:00.000 2021-01-01 00:20:10.000
So in this case I want to compare only date value like (1970-01-01!=2021-01-01) then set true
how to do this ?

You can use startofday(), or bin().
For example:
T
| extend result = startofday(Timestamp) != startofday(ingestion_time())

Related

Convert date to dateTtime format in SQL

I am trying to convert a date column (ie. 2012-10-02) to the first day of the year with time (ie. 2012-01-01T00:00:00) in sql.
Is there a way to do so in the SELECT query?
for BigQuery use below
select timestamp_trunc('2012-10-02', year)
with output
2012-01-01 00:00:00 UTC
Note - if you column is of date type - the output will be
2012-01-01T00:00:00
and finally, you can use datetime_trunc instead of timestamp_trunc and you will get expected result - 2012-01-01T00:00:00
Look at the YEAR() function.
It would allow you to extract just the year, and then just as the date and time you need.

how to round timestamp to day in HIVE?

I have a string value like '2020-10-01T02:02:50.918+03:00'. How can I get value like this: 2020-10-01 00:00:00.000 in timestamp datatype in Hive?
Use substr to get yyyy-MM-dd, then use timestamp construct.
Demo:
select timestamp(substr('2020-10-01T02:02:50.918+03:00',1,10))
Result:
2020-10-01 00:00:00.0

How to consolidate date and time dimensions to a DateTime?

I have data something like this:
category_id | date (string) | time (string)
1 2011-08-21 09:16:06
2 2012-09-29 10:18:26
I now want to consolidate date and time dimensions to a DateTime as timestamp format. How can I do this?
Did you try date_parse()?
select date_parse(date || time, '%Y-%m-%d%H:%i:%s')

SELECT query with LIKE fails

I'm using a SQL SELECT query to bring back all rows from a specific date.
The column I'm using is called TimeStamp (datetime)
(An example of data from this column = 01/02/2018 07:55:55)
What I would like is to return all rows from a specific date eg 24/06/2019
I have tried
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02/01/2018 07:55:55'
which returns one row (which is correct as there is only one row containing this data)
If I then try
SELECT top 20 TimeStamp
from Report
where TimeStamp LIKE '02/01/2018%'
I get no results, I have also tried escaping the forward slashes
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02\/01\/2018%'
Most databases support a string function called left(). If I assume that your "timestamp" is a string, then:
where left(timestamp, 10) = '01/02/2018'
However, it should be stored as a date or date/time. If so, then you can do:
where timestamp >= '2018-02-01' and
timestamp < '2018-02-02'
Note the use of standard formatted dates (YYYY-MM-DD). That is the way most databases implement date literals.
In SQL Server, you can also use:
where convert(date, timestamp) = '2018-02-01'
Both this and the previous version will use an index on timestamp, so both are reasonable solutions.
this should work
SELECT TimeStamp FROM report where convert(Date, TimeStamp) = '2019-06-24'
or select timestamp from report where timestamp between '2019-06-24' and '2019-06-25'. This will get you everything between 2019-06-24 00:00:00 and 2019-06-25 00:00:00 thus all records with date 2019-06-24
Convert timestamp value to date.
SELECT TimeStamp
FROM report
WHERE CAST(TimeStamp AS DATE) = '2019-06-24'

ABS doesn't work in sqlite

I have a database which contains some dates alarms:
id timealarm status
1 2014-10-23 13:30:00 +0000 1
2 2014-10-23 13:29:00 +0000 1
All I'm trying to do, is to send the current date to the sql and perform the abs so that take the value (date) nearest the current date:
SELECT * FROM records WHERE status = '1' ORDER BY ABS('2014-10-23 13:27:09 +0000' - timealarm) LIMIT 1
in this case this syntax returns me the id 1, instead of id 2, Why and how to solve this problem?
EDIT
The field timealarm is a VARCHAR
When you try to subtract strings from each other, the database converts them into numbers.
So you end up subtracting 2014 from 2014, which results in the same value for all rows.
To be able to compute differnce, convert the date strings into some number (this requires dropping the time zone information, which is not understood by SQLite's date functions):
...
ORDER BY abs(julianday('2014-10-23 13:27:09') - julianday(substr(timealarm, 1, 19)))
...