如何在sqlserver 2005中分隔时间和日期 [英] how to separate time and date in sqlserver 2005

查看:92
本文介绍了如何在sqlserver 2005中分隔时间和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在sqlserver 2005中分隔时间和日期



它在我的表格中显示为 16012014 081500



只是我想显示只有时间08:15使用sql查询



请帮忙谢谢。

解决方案

你可以做

 选择 转换 TIME ,yourDateTimeColumn) as  TheTime 





哎呀 - 下面的海报是对的 - 你可以使用



 选择 转换 VARCHAR  8 ),yourDateTimeColumn, 108  as  theTime 





抱歉。


尝试使用datepart f在第一个你必须得到小时然后分钟并在查询中连接这一点例如选择DATEPART(hh,OrderDate)+:+ DATEPART(mm,OrderDate)这样

你也可以使用转换函数

  CONVERT  VARCHAR (< span class =code-digit> 24 ),GETDATE(), 108 



但是这里你也得到秒数


16012014 081500

这不是正确的日期时间格式。

对于正确的格式,您可以执行以下操作:



  DECLARE   @ SomeDateTime   datetime  
SET @ SomeDateTime = ' 2014-01-16 08 :15:00'

SELECT CONVERT CHAR 5 ), @ SomeDateTime 108 )[时间


how to separate time and date in sqlserver 2005

its displaying in my table as 16012014 081500

Just i want to dispaly only time as 08:15 using sql query

Please help thanks.

解决方案

You can do

select Convert(TIME, yourDateTimeColumn) as TheTime



Oops - the poster below is right - instead you can use

select Convert(VARCHAR(8),yourDateTimeColumn,108) as theTime



sorry.


Try to use datepart function where first u have to get hour and then minute and concat this in query e.g select DATEPART(hh,OrderDate) + ":" + DATEPART(mm,OrderDate) like this
Also you can use convert function

CONVERT(VARCHAR(24),GETDATE(),108)


but here u get seconds also


16012014 081500
That isn't correct date time format.
For a correct format you can do something like below:

DECLARE @SomeDateTime datetime
SET @SomeDateTime = '2014-01-16 08:15:00'

SELECT CONVERT(CHAR(5), @SomeDateTime, 108) [time]


这篇关于如何在sqlserver 2005中分隔时间和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆