ABS doesn't work in sqlite - sql

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)))
...

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.

hive date cast issue

Hi in my Hive table I have a column with the date values like this .
cl1
31102019000000
30112019000000
31122019000000
I have tried to convert the column values to date format like this
Select from_unixtime(unix_timestamp(cl1,'yyyy/MM/dd'),'yyyy-MM-dd') from table1;
it prints NUll. Any help will be appreciated.
You said you have dates in dd-mm-yyyy but then posted data that doesn't have any hyphens in at all
Assuming 31102019000000 is 31-oct-2019 00:00:00
Select from_unixtime(unix_timestamp(cl1,'ddMMyyyyHHmmss'),'yyyy-MM-dd') from ...
Match the format string to the data..
Try using this
Select from_unixtime(unix_timestamp(REGEXP_REPLACE(cl1,'0+$',''),'ddMMyyyy'),'yyyy-MM-dd') from table1;
but this would fail for years 2020,2010.
So the below query would be a better alternative
Select from_unixtime(unix_timestamp(cast(cast(cl1/1000000 as bigint) as string),'ddMMyyyy'),'yyyy-MM-dd') from table1;
The UnixTimeStamp which you are trying to convert to date format is out of range.
Your Time stamp corresponds to :
GMT: Saturday, August 2, 2955 1:43:20 AM
Your time zone: Saturday, August 2, 2955 5:43:20 AM GMT+04:00
Relative: In 936 years
It is an open bug in MYSql community. Please check the below URL for reference.
https://forums.mysql.com/read.php?20,385047,385132#msg-385132
MySQL database's built-in functions like UNIX_TIMESTAMP() will return 0 after 03:14:07 UTC on 19 January 2038. (Source: https://en.wikipedia.org/wiki/Year_2038_problem)

How to subtract two dateTime field containing dateTime in ISO format and get the result in hours?

How to subtract two dateTime field containing dateTime in ISO format and get the result in hours?
I have tried subtracting two date fields but it has just subtracted date and not taken time into consideration
to_number(
TRUNC(to_timestamp(T1.attribute_2,'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"'))-
TRUNC(to_timestamp(T2.attribute_2,'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"'))
)
Date 1 2019-04-26 10:00pm
Date 2 2019-04-26 8:00pm
Expected Outcome: Date1- Date 2 = 2(in hrs)
Actual Outcome: Date1- Date 2 should give 0
If you want to take the hours into consideration, then don't truncate the values! TRUNC() removes the time component.
For hours, multiply the difference by 24:
(to_timestamp(T1.attribute_2,'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"')-
to_timestamp(T2.attribute_2,'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"')
) * 24

UTC and offset date time compare in sql server

I am storing UTC datetime in Database
e.g.
2018-06-05 11:37:00.000 (UTC)
2018-06-05 17:07 (+5:30 India standart time)
I am having offset as :
offset as +02:00
How can I compare in sql query that now offset time matched ?
e.g.
2018-06-05 13:37:00.000
My issue is X (IST) date time converted to UTC and now I want to covert to different time zone (Y)
The following functions helped me to resolve the issue:
1. SWITCHOFFSET
2. TODATETIMEOFFSET
SELECT GETDATE()
SELECT SWITCHOFFSET(GETDATE(), '+05:30')
SELECT CONVERT(VARCHAR(19), SWITCHOFFSET(TODATETIMEOFFSET(GETDATE(), '+05:30'),'+00:00'), 120)
SELECT GETUTCDATE()
If I understand your question correctly, you can use the DATEADD function to calculate the new datetime based on the UTC datetime and the offset
For example:
2 hours = 120 minutes
DATEADD(minute, 120, '2018-06-05 11:37:00.000')
Or using hours
DATEADD(hour, 2, '2018-06-05 11:37:00.000')
You can also go the other way using negative numbers
You don't have to use a literal value, you can supply a column name to parameter 3 for use in a query and also use this expression as part of a where clause
Here's a cheat sheet for the DATEADD function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/dateadd-transact-sql?view=sql-server-2017

VARCHAR to DATETIME conversion

I want to convert a varchar column to datetime in SQL server 2014. I've tried the following and I can get the date and minute portions fine, but can't get the hour part correctly. Is it possible to get hour part?
Here is an example:
Select
mt.DatevarcharCol,
RIGHT(mt.DatevarcharCol, LEN(mt.DatevarcharCol) - CHARINDEX(',',mt.DatevarcharCol)) AS 'Number to the right of',
DATEADD(DAY, CAST( SUBSTRING(mt.DatevarcharCol, 1, charindex(',', mt.DatevarcharCol) - 1) AS bigint),(CONVERT(datetime,'12/31/1840'))) AS 'Date part',
((RIGHT(mt.DatevarcharCol, LEN(mt.DatevarcharCol) - CHARINDEX(',',mt.DatevarcharCol))) % 86400)%3600/60 AS Minute,
?? -- calculate hour part
FROM dbo.mytable mt
Result:
DatevarcharCol Number to the right of Date part Minute hour
-------------- ---------------------- ------------------- ------ ----
63402,67524 67524 2014-08-03 00:00:00 45 ??
The hour would be:
(RIGHT(mt.DatevarcharCol, LEN(mt.DatevarcharCol)-CHARINDEX(',',mt.DatevarcharCol)))/60/60%24
The seconds would be:
(RIGHT(mt.DatevarcharCol, LEN(mt.DatevarcharCol)-CHARINDEX(',',mt.DatevarcharCol)))%60
Also, I suggest you don't store comma separated values in a single column. Instead, put each value in its own column.