How to consolidate date and time dimensions to a DateTime? - sql

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

Related

How to compare two Timestamp In kusto

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

BigQuery: Format ISO Date

I'm trying to parse a timestamp which is in ISO Date 8601 format.
Example: 2021-04-10T14:11:00Z
This information is stored inside a JSON object and for that reason I'm extracting that data as a string:
The format I'm looking for is a yy-MM-dd hh:mm format and for that I've tried the following
SQL CODE
SELECT document_id,
json_extract(data, '$.Pair') as pair,
PARSE_TIMESTAMP('%y-%m-%d %H:%M', json_extract(data, '$.AlertTime')) as alerttime,
COUNT(document_id) as alert_count
FROM `tradingview-alerts-26eb8.alltables.TradingView_000_raw_latest` as alert_view
GROUP BY alerttime, document_id, pair
Errors
The code from above causes the following error:
Failed to parse input string '"2021-04-10T03:17:00Z"
The reason for this is the T in the middle of the date, I believe,
In order to discard that I tried this change:
SUBSTR(json_extract(data, '$.AlertTime'), 1, 10))
But with that I'm getting an error on a different row:
Failed to parse input string '"2021-04-1'
I'm wondering if it is because of how the date is being presented (year-month-date) the date not having 2 digits? such as 2021-04-01 instead of 2021-04-1.
However if I try with
SUBSTR(json_extract(data, '$.AlertTime'), 1, 11))
The error I'm getting is
Failed to parse input string '"2021-04-10'
You need to include those ISO symbols into format specifier as constants:
select parse_timestamp('%FT%TZ', '2021-04-12T17:38:10Z')
| f0_ |
---------------------------
| 2021-04-12 17:38:10 UTC |
UPD: If you have fractional seconds, you can include optional milliseconds element %E*S instead of time element %T. For non-UTC timestamps there should also be timezone element: %Ez. So, the possible solution could be:
with a as (
select '2021-04-12T20:44:06.95841Z' as ts_str union all
select '2021-04-12T23:44:07.83738+03:00' union all
select '2021-04-12T23:44:08+03:00'
)
select parse_timestamp('%FT%H:%M:%E*S%Ez', regexp_replace(ts_str, 'Z$', '+00:00')) as ts
from a
| ts |
|--------------------------------|
| 2021-04-12 20:44:06.958410 UTC |
| 2021-04-12 20:44:07.837380 UTC |
| 2021-04-12 20:44:08 UTC |
I think you can use timestamp => datetime func.
Like this
datetime(timestamp(2021-11-29T00:00:00.000Z))

PostgreSQL date value changes to next day after 5 PM PST

Whenever I create data after 5 PM PST (Pacific Standard Time), the manufactured_date column gets changed to next date.
For example, I entered new data today which is 2020-11-05 at 5:15 pm PST, the value that gets stored in manufactured_date column is 2020-10-06
Query select * from cars gives me below result
id | car_name | manufactured_date
---------------------------------
1 | Audi | 11-06-2020
2 | BMW | 11-06-2020
Here are the properties of cars table
column name | data type
-----------------------------
id | serial
car_name | varchar
manufactured_date | date
The insert query is
insert
into
cars (car_name, manufactured_date)
values ('Audi', '11-05-2020');
How can I eliminate this problem?
Use a case expression:
select (case when (now() now() at time zone 'PST')::time >= '17:00:00'::time
then manufactured_date + interval '1 day'
else manufactured_date
end)
The problem is most likely that the server is running in UTC and that is column is based on UTC instead of PST timezone.
to validate this you can just execute this and it should return the current timestamp according to server timezone:
select now()
This is really interesting! I couldn't reproduce your problem on my VM, even after playing with the OS time-zone settings and with the PGTZ environment variable.
Can you please try not counting on implicit data type conversions? Adding the DATE directive before what is currently a string literal might help:
INSERT INTO cars (car_name, manufactured_date)
VALUES ('Audi', DATE '2020-11-05');

adding a VARCHAR to DATE as MINUTES

I'm pretty sure this is an easy one for you guys but it's driving me crazy.
I have a column with dates in a "YYYY-MM-DD" format and a column with small intergers values between 0 and 29. So I want to add the 2 columns together and get something like this:
Date | INT | NEW timestamp
2016-01-01 | 2 | 2016-01-01 00:02:00
2016-10-15 | 21 | 2015-10-15 00:21:00
so I tried the obvious like:
"Date" + "INT" as "NEW timestamp"
and stuff like
VARCHAR_FORMAT("INT",'MI')
or even
VARCHAR_FORMAT("Date",'YYYY-MM-DD HH24:MI:SS') + VARCHAR_FORMAT("INT",'MI')
but keep getting errors. I am doing this in dashDB
One option is to use:
select add_minutes(cast("date" as timestamp),"int") from yourTable
Another simple version is:
select cast("date" as timestamp) + "int" minutes from yourTable
On db2 iseries
select TIMESTAMP_FORMAT(Date , 'YYYY-MM-DD') + INT minute as Newtimestamp from yourtable
or
select cast(cast(Date as date) as timestamp) + int minute from yourtable
you can try this
Date_add(cast(`Date` as date),interval Int minute)

select values of a specific month from table contain timestamp column

For example I have a following table(tbl_trans) like below
transaction_id transaction_dte
integer timestamp without time zone
---------------+----------------------------------
45 | 2014-07-17 00:00:00
56 | 2014-07-17 00:00:00
78 | 2014-04-17 00:00:00
so how can I find the tottal no.of transaction in 7th month from tbl_trans ?
so the expected output is
tot_tran month
--------+-------
2 | July
select count(transaction_id) tot_tran
,to_char(max(transaction_dte),'Month') month from tbl_trans
where extract (month from transaction_dte)=7
PostgreSQL Extract function explained here
Reference : Date/Time Functions and Operators
select count(transaction_id),date_part('month',transaction_dte)
from tbli_trans where date_part('month',transaction_dte)=7
EXTRACT(MONTH FROM TIMESTAMP transaction_dte)
OR
date_part('month', timestamp transaction_dte)
You only need to add the word timestamp if your timestamp is saved in a string format
Properly looked up what the difference between the 2 is now:
The extract function is primarily intended for computational
processing. For formatting date/time values for display.
The date_part function is modeled on the traditional Ingres equivalent
to the SQL-standard function extract.
Use Datepart function.
where datepart(transaction_dte, mm) = 7