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');
Related
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))
When I import my Excel file to SQL Server, it automatically add a default date to my time data. For example, my original data looks like this:
StartTime EndTime
----------------------------
09:00 AM 10:00 AM
But when I load the data in SQL Server, it adds the default date like this
StartTime EndTime
----------------------------------------------------
1899-12-30 09:00:00.000 1899-12-30 10:00:00.000
How do I get rid of this default date and also those trailing zeros?
You can convert to time:
select convert(time, starttime), convert(time, endtime)
You can also be sure that the type in the table is time.
You might be having the datatype as DATETIME. Now, you can convert the datatype to TIME to remove the datevalues. In future, remember to keep the datatype as TIME to avoid these issues.
CREATE TABLE #T (timeval DATETIME)
insert into #T
VALUES ('09:00 AM'),('10:00 AM')
ALTER TABLE #T
ALTER COLUMN timeval TIME
SELECT * FROM #T
+------------------+
| timeval |
+------------------+
| 09:00:00.0000000 |
| 10:00:00.0000000 |
+------------------+
I am using postgresql 8.0 wherein I have a column which is a timestamp in milliseconds from epoch (but stored as text in table). I want to identify the start of hour for this timestamp. I am unable to think of clean way to do
Table structure
id (varchar(52)) | Name(varchar(100)) | UpdateTime (varchar(20))
1 | Robin | 1598051512000
2 | Sally | 1628734800000
My thought process was to use to_timestamp(). However it does not accept a BigInt. So the idea was to use substring() to discard the last 3 characters of the value in updateTime and then pass it to to_timestamp()
e.g. using select substring('1598051512000', 1, length('1598051512000') - 3));
However this seems to be getting convoluted. IS there a cleaner way to get start of hour based on timestamp?
Expected Output for input
1598051512000 = 2020-08-21T23:00:00Z
1628734800000 = 2020-08-12T02:00:00Z
You could use date arithmetics, then date_trunc():
select
t.*,
date_trunc(
'hour',
date '1970-01-01' + '1598051512000'::bigint / 1000 * '1 second'::interval
) UpdateHour
from mytable t
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)
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