Sql返回日期时间没有秒和毫秒

时间:2014-08-07 14:10:00

标签: sql sql-server

我的这段代码返回日期时间,但是用秒毫秒,我不想要毫秒和秒。我试过但不工作,帮忙。以这种格式。 输出:

代码:

Set @DateFrom = Convert(date,(Select min(ReceivedMessages.ReceivedDateTime) from ReceivedMessages))
      Set @DateTo = Convert(date,(Select max(ReceivedMessages.ReceivedDateTime) from ReceivedMessages))

      SELECT [ID]
      ,LEFT(REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 113), ' ','/'), 11) + ' ' +
       RIGHT(REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 113), ' ','/'), 12)
       as RecievingDate
      ,[FromMobileNo]
      ,[Message],
       [IsComplaint]
      FROM [CmsSMSDb].[dbo].[ReceivedMessages] 
      where Convert(date,ReceivedDateTime)>= @DateFrom AND Convert(date,ReceivedDateTime)<= @DateTo
      AND IsComplaint = 2

3 个答案:

答案 0 :(得分:3)

试试这个,但用时间替换GETDATE()

SELECT CONVERT(VARCHAR(16), GETDATE(), 121)

答案 1 :(得分:1)

declare @dtime datetime;
set @dtime = getdate();

select REPLACE(CONVERT(varchar(11), @dtime, 113), ' ', '/') 
     + RIGHT(CONVERT(varchar(17), @dtime, 113), 6)

答案 2 :(得分:0)

您可以将日期时间舍入到最近的分钟,如下所示:

DECLARE @CaptureDate datetime
SELECT @CaptureDate = DATEADD(minute, DATEDIFF(minute, 0, DATEADD(second, 30 - DATEPART(second, GETDATE() + '00:00:30.000'), GETDATE())), 0)
SELECT  CONVERT(VARCHAR(16), @CaptureDate, 120)

将GETDATE()更改为适用的日期字段。