Sorry..if it is for time difference, pls check the below article
http://www.experts-exchang
Main Topics
Browse All TopicsHello,
How to get time difference between two timestamp columns? I know age() function can be used but it returns INTERVAL but I want that interval in seconds/milliseconds
Thanks for you help in advance
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Sorry..if it is for time difference, pls check the below article
http://www.experts-exchang
Since I have to use in my sql statements, that solution should be using simple SQL (including CASE and built-in functions)
See below sql in sql server, I want relevant query in postgre using its built-in functions
If there is no simple way, I have a workaround where I will extract hour/minute/seconds/ms and convert them to integer using simple logic like below
to convert 1 day in seconds
24*60*60
and similarly to convert it to milliseconds
24*60*60*1000
but the code becomes clumsy anyway thats the way if there is no go
testdb=> select d1,d2,age(d1,d2),
extract(epoch from age(d1,d2)) * 1000 as milliseconds
from (
SELECT '2009-04-01 23:59:59.950000'::timestam
UNION ALL
SELECT '2009-06-29 16:12:38.880000'::timestam
UNION ALL
SELECT '2009-06-29 16:12:38.10'::timestamp as D1,'2009-06-28 16:11:37.00'::timestamp as D2
) as x;
d1 | d2 | age | milliseconds
------------------------+-
2009-04-01 23:59:59.95 | 2009-03-31 11:29:07.45 | 1 day 12:30:52.50 | 131452500
2009-06-29 16:12:38.88 | 2009-06-29 14:39:20.34 | 01:33:18.54 | 5598540
2009-06-29 16:12:38.10 | 2009-06-28 16:11:37 | 1 day 00:01:01.10 | 86461100
(3 rows)
earthman2:
Its simply converting seconds to milleseconds but my requirement is to get the difference in milleseconds
to get more clarity see below two timestamps which has 10 milleseconds timestamps difference
SELECT '2009-06-29 16:12:38.10' as D1,'2009-06-29 16:12:38.00' as D2
when we extract it it gives 0 seconds difference and multiplying it with 1000 gives 0 milliseconds which is wrong
[earthman2@supercomputer ~]$ psql -q testdb
Welcome to psql 8.2.4, the PostgreSQL interactive terminal.
testdb=> SELECT round(1000 * extract( epoch from cast('2009-06-29 16:12:38.10' as timestamp) -cast('2009-06-29 16:12:38.00' as timestamp ))) as milliseconds;
milliseconds
--------------
100
(1 row)
testdb=> SELECT 1000 * extract( epoch from cast('2009-06-29 16:12:38.10' as timestamp) -cast( '2009-06-29 16:12:38.00' as timestamp )) as milliseconds;
milliseconds
------------------
100.000023841858
(1 row)
[earthman2@supercomputer ~]$ /usr/bin/pg_config
BINDIR = /usr/bin
DOCDIR = /usr/doc/postgresql
INCLUDEDIR = /usr/include
PKGINCLUDEDIR = /usr/include/postgresql
INCLUDEDIR-SERVER = /usr/include/postgresql/se
LIBDIR = /usr/lib
PKGLIBDIR = /usr/lib/postgresql
LOCALEDIR = /usr/share/locale
MANDIR = /usr/man
SHAREDIR = /usr/share/postgresql
SYSCONFDIR = /usr/etc/postgresql
PGXS = /usr/lib/postgresql/pgxs/s
CONFIGURE = '--prefix=/usr' '--with-python' '--with-openssl' '--with-gnu-ld' '--enable-nls' '--with-krb5' 'CFLAGS=-march=pentium4 -O2 -mmmx -msse -msse2'
CC = gcc
CPPFLAGS = -D_GNU_SOURCE
CFLAGS = -march=pentium4 -O2 -mmmx -msse -msse2 -Wall -Wmissing-prototypes -Wpointer-arith -Winline
-Wdeclaration-after-statem
CFLAGS_SL = -fpic
LDFLAGS = -Wl,-rpath,'/usr/lib'
LDFLAGS_SL =
LIBS = -lpgport -lssl -lcrypto -lkrb5 -lz -lreadline -ltermcap -lcrypt -ldl -lm
VERSION = PostgreSQL 8.2.4
in config.log
configure:1700: checking whether to build with 64-bit integer date/time support
configure:1732: result: no
Business Accounts
Answer for Membership
by: geek_vjPosted on 2009-11-01 at 22:59:46ID: 25717477
You can use datediff function. The syntax is like this:
SELECT DATEDIFF(ms, date1, date2) from tablename