可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How do I convert hh:mm:ss to hh:mm in SQL Server?
select Count(Page) as VisitingCount,Page,CONVERT(VARCHAR(8),Date, 108) from scr_SecuristLog
where Date between '2009-05-04 00:00:00' and '2009-05-06 14:58'
and [user] in(select USERNAME
from scr_CustomerAuthorities )
group by Page,Date order by [VisitingCount] asc
回答1:
In general, the set of timestamps is not well-ordered, this means you cannot get a "last" timestamp whose time part up to minutes is 2009-05-06 14:58
.
In SQL Server
, which keeps the time part of a datetime as a number of 1/300
second fractions after midnight, this "last" timestamp would be 2009-05-06 14:58:59.997
, but this is not guaranteed to be compatible with future releases of with other TIMESTAMP
storage methods.
That means you'll need to split your BETWEEN
condition into two conditions, one of which being strict less
than the next minute:
select Count(Page) as VisitingCount,Page,CONVERT(VARCHAR(8),Date, 108) from scr_SecuristLog
where Date >= '2009-05-04 00:00:00'
AND Date < DATEADD(minute, 1, '2009-05-06 14:58')
and [user] in(select USERNAME
from scr_CustomerAuthorities )
group by Page,Date order by [VisitingCount] asc
This solution will efficiently use indexes on Date
回答2:
CONVERT(VARCHAR(5),Date, 108)
-- Gets only HH:mm
回答3:
SELECT Convert(varchar(5), GetDate(), 108)
Using varchar(5) will automatically truncate the date to remove the seconds.
回答4:
I dont think there is a built in function; usually do something like this
SET @time = '07:45'
SET @date = CONVERT(DATETIME,@time)
SELECT @date
SELECT LEFT(CONVERT(VARCHAR,@date,108),5)
回答5:
For this specific need you should use the between method as noted by Quassnoi's answer. However, the general problem can be solved with:
select dateadd(second, -datepart(second, @date), @date)
回答6:
One way would be to use the RIGHT() function to crop the Date. Something like:
RIGHT(CONVERT(VARCHAR(8),Date, 108),5)
This will only work if number of characters is constant e.g. there is a leading zero if applicable. (Sorry havn't got SQL server here to test).
A better way is to use the T-SQL datepart function to split and then re-concatinate the date parts so:
DARTPART("hh", CONVERT(VARCHAR(8),Date, 108))+":"+DARTPART("mi", CONVERT(VARCHAR(8),Date, 108))
References:
http://msdn.microsoft.com/en-us/library/ms174420.aspx
http://msdn.microsoft.com/en-us/library/ms187928.aspx
http://msdn.microsoft.com/en-us/library/ms177532.aspx
回答7:
To get hh:mm format from today's date:
select getdate()
select convert(varchar(5),getdate(),108)
To get hh:mm format from any datetime column in a table data:
select date_time_column_name from table_name where column_name = 'any column data name'
select convert(varchar(5),date_time_column_name,108) from table_name
where column_name = 'any column data name'
select creationdate from employee where Name = 'Satya'
select convert(varchar(5),creationdate,108) from employee
where Name = 'Satya'
回答8:
SELECT CAST(CAST(GETDATE() AS Time(0)) AS VARCHAR(5))