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)
Related
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');
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
How can I create a table with an attribute whose date is less than today's date in PostgreSQL?
This is what I am trying to do:
CREATE TABLE Player(
dob DATE CHECK(dob < today's date) NOT NULL
/*other attributes and constraints omitted*/
);
Is this possible?
Thank you in advance!
Do:
check (dob < current_date)
It needs to be current_date not now() as a date will always (almost) be lower than now which is a date plus a time
select
current_date,
current_date::timestamp,
now(),
current_date < now(),
current_date < current_date;
date | timestamp | now | ?column? | ?column?
------------+---------------------+-------------------------------+----------+----------
2017-04-04 | 2017-04-04 00:00:00 | 2017-04-04 11:54:39.703732+00 | t | f
All credit goes to #Gordon Linoff for this answer.
He suggested I use the now() function which PostgreSQL provides.
The now() function returns the current date and time.
See https://www.postgresql.org/docs/8.0/static/functions-datetime.html for more information.
My resulting table looks like this:
CREATE TABLE Player(
dob DATE CHECK(dob < now()) NOT NULL
/*other attributes and constraints omitted*/
);
If I do:
SELECT PRESERV_STARTED
FROM HARVESTED_L;
I will get values like:
23-12-1999 00:00:00
21-03-2000 22:01:37
...
And so on. (PRESERV_STARTED has type DATE)
What I want is only to select the date with time part, where the time is not 00:00:00, so that I can omit those.
There is a lot of info about a solution to this, saying I can do something like:
select cast(AttDate as time) [time]
from yourtable
And for older versions of sql server:
select convert(char(5), AttDate, 108) [time]
from yourtable
And yet other proposals are:
SELECT CONVERT(VARCHAR(8),GETDATE(),108)
I tried all of these, among a few others, but no luck.
So my question is, having a date like: 23-12-1999 00:00:00, how do I select the time part?
What comes most intuitive to me (mixing with the proposals I found) is something like:
SELECT CONVERT(VARCHAR(8), GETDATE(PRESERV_STARTED), 108) AS timePortion
FROM HARVESTED_L;
I get an error from this code, saying "Missing expression". In fact, this is the error I get from most of the proposals I tried.
I am using Oracle SQL Developer version 4.1.1.19
In Oracle you can just format the date(time) however you like:
SELECT TO_CHAR(preserv_started, 'HH24:MI:SS')
FROM harvested_l
If I understand correctly you want to select only the rows for which the time part of the date column is not 00:00:00. You don't have to get the time part in order to do this. You can use TRUNC function which (by default) returns date with the time part truncated. Here's an example:
SQL> select * from t;
ID D
---------- -------------------
1 2016-01-01 00:00:00
2 2016-01-01 00:01:00
3 2016-01-01 00:01:23
3 rows selected.
SQL> select * from t where d <> trunc(d);
ID D
---------- -------------------
2 2016-01-01 00:01:00
3 2016-01-01 00:01:23
2 rows selected.
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