Getting timestamp without milliseconds

Started by Tadej Kanizarover 20 years ago4 messagesgeneral
Jump to latest
#1Tadej Kanizar
Tadej.Kanizar@sagit-server.com

Hi!

I've got a timestamp field.. and it returns sth like "2006-01-04
21:33:17.156". How can I get the same result, just without the .156? I've
looked at date/time formatting functions, etc, but couldn't find a straight
way to do this :-(

Thanks.

Regards,

Tadej

#2John Sidney-Woollett
johnsw@wardbrook.com
In reply to: Tadej Kanizar (#1)
Re: Getting timestamp without milliseconds

Either date_trunc

eg, SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-02-16 20:00:00

Or format the timestamp as a string

select to_char(now(), 'yyyy-mm-dd HH24:MI:SS');

Hope that helps.

John

Tadej Kanizar wrote:

Show quoted text

Hi!

I've got a timestamp field.. and it returns sth like "2006-01-04
21:33:17.156". How can I get the same result, just without the .156? I've
looked at date/time formatting functions, etc, but couldn't find a straight
way to do this :-(

Thanks.

Regards,

Tadej

#3Scott Marlowe
smarlowe@g2switchworks.com
In reply to: Tadej Kanizar (#1)
Re: Getting timestamp without milliseconds

On Thu, 2006-01-05 at 10:04, Tadej Kanizar wrote:

Hi!

I’ve got a timestamp field.. and it returns sth like “2006-01-04
21:33:17.156”. How can I get the same result, just without the .156?
I’ve looked at date/time formatting functions, etc, but couldn’t find
a straight way to do this L

Here ya go:

test=> create table t1 (ts timestamp);
CREATE TABLE
test=> insert into t1 values (now());
INSERT 2106750874 1
test=> select ts from t1;
ts
----------------------------
2006-01-05 10:15:48.167951
(1 row)

test=> select cast (ts as timestamp(0)) from t1;
ts
---------------------
2006-01-05 10:15:48
(1 row)

test=> select ts::timestamp(0) from t1;
ts
---------------------
2006-01-05 10:15:48
(1 row)

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tadej Kanizar (#1)
Re: Getting timestamp without milliseconds

"Tadej Kanizar" <Tadej.Kanizar@sagit-server.com> writes:

I've got a timestamp field.. and it returns sth like "2006-01-04
21:33:17.156". How can I get the same result, just without the .156?

Cast to timestamp(0).

regards, tom lane