求一个SQL函数,将日期转换成数字,如分,秒

Goblin_SSD 2009-11-16 02:11:24
如题,数据库用的是postgre
...全文
1858 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunhope808 2009-11-16
  • 打赏
  • 举报
回复
我们诚恳地邀请大家加入到我们Java群组,热爱Java的同仁们,一起来探讨其中的难题和奥妙,每周
我们会发出一个趣味性的小题目供大家探讨。也会继续上传JAVA连载教程,期待你们的加入!
群组地址:http://www.rjgc.net/uchome/space.php?do=mtag&tagid=73
xuexijava 2009-11-16
  • 打赏
  • 举报
回复
mark
Goblin_SSD 2009-11-16
  • 打赏
  • 举报
回复
非常感谢大家,尤其是crazylaa大哥!
crazylaa 2009-11-16
  • 打赏
  • 举报
回复
笨了哦,用:
select date_part('epoch',now()-to_date('2009-11-16 00:00:00','yyyy-mm-dd hh:mi:ss')) from test1,直接算秒差。。。
crazylaa 2009-11-16
  • 打赏
  • 举报
回复
select date_part('day',now()-to_date('2009-11-16 00:00:00','yyyy-mm-dd hh:mi:ss'))*86400.0
+date_part('hour',now()-to_date('2009-11-16 00:00:00','yyyy-mm-dd hh:mi:ss'))*3600
+date_part('minute',now()-to_date('2009-11-16 00:00:00','yyyy-mm-dd hh:mi:ss'))*60
+date_part('second',now()-to_date('2009-11-16 00:00:00','yyyy-mm-dd hh:mi:ss')) from test1

结果:58511.375
老张-AI 2009-11-16
  • 打赏
  • 举报
回复
Goblin_SSD 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 crazylaa 的回复:]
是不是想要取得时间戳的分钟或秒啊?
SQL codecreatetable test1(atimestamp);insertinto test1values(now());select*from test1;
结果:2009-11-1615:18:40.078select to_char(a,'mi')from test1;
结果:18select to_number(to_char(a,'mi'),'00')from test1;
结果:18select to_number(to_char(a,'ss'),'00')from test1;
结果:40
[/Quote]
补充一下,就像你举的例子,能不能把15:18:40.078
一起换算了啊,而不是单纯的算小时的或是分钟的!
Goblin_SSD 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 crazylaa 的回复:]
是不是想要取得时间戳的分钟或秒啊?
SQL codecreatetable test1(atimestamp);insertinto test1values(now());select*from test1;
结果:2009-11-1615:18:40.078select to_char(a,'mi')from test1;
结果:18select to_number(to_char(a,'mi'),'00')from test1;
结果:18select to_number(to_char(a,'ss'),'00')from test1;
结果:40
[/Quote]
谢谢,大家!麻烦再帮忙看下载to_char的参数中使用mi 它算出的是两个时间分钟的时间差(小时就没算的,就像1:50:20-00:30:20=20),我就想计算两个时间的时间差然后换算成秒(int型的)
update
current_browse_web_page
set
stay_time=to_number(to_char(now()-browse_time,'mi'),'00')

crazylaa 2009-11-16
  • 打赏
  • 举报
回复
是不是想要取得时间戳的分钟或秒啊?
create table test1(a timestamp);
insert into test1 values(now());
select * from test1;
结果:2009-11-16 15:18:40.078
select to_char(a,'mi') from test1;
结果:18
select to_number(to_char(a,'mi'),'00') from test1;
结果:18
select to_number(to_char(a,'ss'),'00') from test1;
结果:40
wenbinglove 2009-11-16
  • 打赏
  • 举报
回复
不好意思 我看错问题咯

没看清是数据库中 抱歉 数据中还真没试过
Goblin_SSD 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wenbinglove 的回复:]
不闲麻烦

先转换为 字符串  然后 截取  在 转换为 Int 类型

  期待 其他 答案
[/Quote]
能说具体点么?你说的函数是用to_char和to_number吗?数据库的函数用得不是很熟,格式那些不是很清楚。
wenbinglove 2009-11-16
  • 打赏
  • 举报
回复
不闲麻烦

先转换为 字符串 然后 截取 在 转换为 Int 类型

期待 其他 答案
youjianbo_han_87 2009-11-16
  • 打赏
  • 举报
回复
不知,这个查询下他的API就知道了吧

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧