sql server中获取当前时间问题

leehan 2008-02-21 12:18:29
我想在sql server中获得一个当前时间值,要求至少能获得到秒,能到毫秒级更好,有什么方法能实现?现在用GETDATE()方法获得的时间只能到分钟。另外对这个时间能够格式化吗?现在显示出的是"mm dd yyyy hh:mmPM"这样的格式,我希望能够像通常习惯的"yyyy-mm-dd hh:mm:ss"这种形式,小时采用24小时制,这些在sql server中能实现吗?请大家赐教。
...全文
616 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shelf0 2012-04-06
  • 打赏
  • 举报
回复
谢谢 学习了
yujiang930 2008-02-21
  • 打赏
  • 举报
回复
select convert(char(4),datepart(yyyy,getdate()))
+ '-' + rtrim(ltrim((case len(datepart(mm,getdate())) when 1 then '0' + convert(char(2),datepart(mm,getdate())) else convert(char(2),datepart(mm,getdate())) end)))
+ '-' + rtrim(ltrim((case len(datepart(dd,getdate())) when 1 then '0' + convert(char(2),datepart(dd,getdate())) else convert(char(2),datepart(dd,getdate())) end)))
+ char(10)
+ rtrim(ltrim((case len(datepart(hh,getdate())) when 1 then '0' + convert(char(2),datepart(hh,getdate())) else convert(char(2),datepart(hh,getdate())) end)))
+ ':' + rtrim(ltrim((case len(datepart(n,getdate())) when 1 then '0' + convert(char(2),datepart(n,getdate())) else convert(char(2),datepart(n,getdate())) end)))
+ ':' + rtrim(ltrim((case len(datepart(ss,getdate())) when 1 then '0' + convert(char(2),datepart(ss,getdate())) else convert(char(2),datepart(ss,getdate())) end)))
+ ':' + convert(varchar(3),datepart(ms,getdate()))
dawugui 2008-02-21
  • 打赏
  • 举报
回复
石头,还在啊?

select convert(varchar(23),getdate(),120)
dawugui 2008-02-21
  • 打赏
  • 举报
回复
getdate()
-狙击手- 2008-02-21
  • 打赏
  • 举报
回复
select convert(char(24),getdate(),121),getdate()

/8
------------------------ ------------------------------------------------------
2008-02-21 00:30:18.673 2008-02-21 00:30:18.673

(所影响的行数为 1 行)
*/
leehan 2008-02-21
  • 打赏
  • 举报
回复
用枪手和老乌龟的方法已解决,用枪手的第三个参数设为121可显示到毫秒,用乌龟的设为120可显示到秒。散步的鱼一起学习啦。

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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