Sql Server 选择没有秒的日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27918193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sql Server select datetime without seconds
提问by Soner Sevinc
I have datetime column value below
我有下面的日期时间列值
2015-01-04 20:37:00.000
I tried below
我在下面试过
cast(cast(MyDateColumn as date) as datetime)+cast(datepart(hour,MyDateColumn ) as float)/24
as MyDateColumn
and
和
CAST(CONVERT(CHAR(16),MyDateColumn,113) AS datetime) as MyDateColumn
These are did not work for me
这些对我不起作用
How can i get above datetime as 01-04.2015 20:37?
我怎样才能超过日期时间为01-04.2015 20:37?
采纳答案by Eduard Uta
In SQL Server this will work:
在 SQL Server 中,这将起作用:
DECLARE @now [datetime];
SET @now = GETDATE();
SELECT
CONVERT([varchar](10), @now, 105) + ' ' +
RIGHT('0' + CONVERT([varchar](2), DATEPART(HOUR, @now)), 2) + ':' +
RIGHT('0' + CONVERT([varchar](2), DATEPART(MINUTE, @now)), 2);
回答by Jodrell
回答by koushik veldanda
In MYSQL it will work
在 MYSQL 中它会工作
SELECT DATE_FORMAT(date, '%Y-%m-%d %H:%i') AS formated_date FROM table;
In MS SQL It will work
在 MS SQL 中它会工作
SELECT FORMAT(getdate(), 'dd-mm-yyyy HH:mm')
回答by Eduard Uta
In SQL Server this should do the trick:
在 SQL Server 中,这应该可以解决问题:
declare @dt datetime = '2015-01-04 20:37:00.000'
select right('0' + cast(DATEPART(MM, @dt) as varchar), 2) + '-'
+ right('0' +cast(DATEPART(DAY, @dt) as varchar), 2) + '.'
+ cast(DATEPART(YEAR, @dt) as varchar) + ' '
+ right('0' +cast(DATEPART(HOUR, @dt) as varchar), 2) + ':'
+ right('0' +cast(DATEPART(MINUTE, @dt) as varchar), 2)
回答by Jean-Michel Donatelli
Here's another way and you get a datetime in return.
这是另一种方式,你会得到一个日期时间作为回报。
SELECT DATEADD(
MILLISECOND,
DATEPART(MILLISECOND, '2016-02-16 13:45:24.573') * -1,
DATEADD(SECOND, DATEPART(SECOND,'2016-02-16 13:45:24.573') * -1,
'2016-02-16 13:45:24.573')
)
回答by Andre DeMattia
this is the way i do it. I needed to get -2 minutes
这就是我这样做的方式。我需要 -2 分钟
select CONVERT(datetime, CONVERT(CHAR(18), DATEADD(minute, -2, getdate()) , 113) + '00')
回答by ARC
Format(Cast(Convert(varchar(15),Cast(timeval as Time),100) as DateTime),'hh:mm tt') As newtime
This will remove seconds from time as well as add AM,PM with time.
这将从时间中删除秒,并随时间添加 AM、PM。