0

我正在使用此查询来显示日期和时间。我使用了 113 in 转换函数,它给了我带空格的日期格式,我用 / 替换了它,现在我只想取出分隔日期和时间的 /。

 SELECT [ID]
      ,REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 113), ' ','/') as RecievingDate
      FROM [CmsSMSDb].[dbo].[ReceivedMessages] 
      where Convert(date,ReceivedDateTime)>= @DateFrom AND Convert(date,ReceivedDateTime)<= @DateTo

它给出了这个结果:

09/Jun/2014/10:04:07:000

这就是我想要的,它工作得很好,但问题是我想在 2014 年之后删除“/”,比如09/Jun/2014 10:04:07:000

但在其他地方 '/' 这应该保持不变,但它不应该出现在 2014 年之后。

如何 ?

4

5 回答 5

1

肮脏但有效:

SELECT 
      LEFT(REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 113), ' ','/'), 11) + ' ' +
      RIGHT(REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 113), ' ','/'), 12)
      as RecievingDate
FROM [CmsSMSDb].[dbo].[ReceivedMessages] 
      where Convert(date,ReceivedDateTime)>= @DateFrom AND Convert(date,ReceivedDateTime)<= @DateTo
于 2014-06-09T10:34:10.010 回答
1

如果您使用的是 SQLServer 2012 或更高版本,则可以使用新功能FORMAT

SELECT [ID]
     , FORMAT(ReceivedDateTime, 'dd/MMM/yyyy hh:mm:ss:ms') as RecievingDate
FROM   [CmsSMSDb].[dbo].[ReceivedMessages] 
WHERE  Convert(date,ReceivedDateTime)>= @DateFrom 
  AND  Convert(date,ReceivedDateTime)<= @DateTo

FORMAT函数中hh是12小时制,HH24小时制可以加ttgetAM或者PM,看看区别试试

SELECT FORMAT(SYSDATETIME()(), 'dd/MMM/yyyy hh:mm:ss:ms tt') _12
     , FORMAT(SYSDATETIME()(), 'dd/MMM/yyyy HH:mm:ss:ms') _24

此外,您不应该更改存储的数据(可以在索引中)以匹配参数,如果应该是相反的方式

SELECT [ID]
     , FORMAT(ReceivedDateTime, 'dd/MMM/yyyy hh:mm:ss:ms') as RecievingDate
FROM   [CmsSMSDb].[dbo].[ReceivedMessages] 
WHERE  ReceivedDateTime >= Cast(@DateFrom AS DateTime2)
  AND  ReceivedDateTime <= DateAdd(dd, 1, DateAdd(ms, -1, Cast(@DateTo as DateTime2)))
于 2014-06-09T12:23:02.730 回答
0

只需删除该replace功能

SELECT [ID], 
       convert(varchar, ReceivedMessages.ReceivedDateTime, 113) as RecievingDate
  FROM [CmsSMSDb].[dbo].[ReceivedMessages] 
 where Convert(date,ReceivedDateTime) >= @DateFrom 
   AND Convert(date,ReceivedDateTime)<= @DateTo
于 2014-06-09T10:32:41.300 回答
0

使用以下查询:

SELECT REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '/') + ' '
       +  CONVERT(VARCHAR(8), GETDATE(), 108) as [DD/Mon/YYYY HH:MM:SS]

在您的查询中:

SELECT [ID]
,REPLACE(CONVERT(VARCHAR(11),ReceivedMessages.ReceivedDateTime , 106), ' ', '/') + ' '
+  CONVERT(VARCHAR(8), ReceivedMessages.ReceivedDateTime , 108) as RecievingDate
FROM [CmsSMSDb].[dbo].[ReceivedMessages] 
where Convert(date,ReceivedDateTime)>= @DateFrom 
AND Convert(date,ReceivedDateTime)<= @DateTo

[编辑] 对于 12 小时格式,写为:

SELECT REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '/') + ' '
+ substring(CONVERT(VARCHAR, GETDATE(), 109),13,
Len (CONVERT(VARCHAR, GETDATE(), 109))) as [DD/Mon/YYYY HH:MI:SS:MMMAM (or PM)]

并将您的查询修改为:

SELECT [ID]
,REPLACE(CONVERT(VARCHAR(11),ReceivedMessages.ReceivedDateTime , 106), ' ', '/') 
+ ' '
+  substring(CONVERT(VARCHAR, ReceivedMessages.ReceivedDateTime , 109),13,
Len (CONVERT(VARCHAR, ReceivedMessages.ReceivedDateTime , 109))) as RecievingDate
FROM [CmsSMSDb].[dbo].[ReceivedMessages] 
where Convert(date,ReceivedDateTime)>= @DateFrom 
AND Convert(date,ReceivedDateTime)<= @DateTo
于 2014-06-09T10:48:19.417 回答
0
select [ID]
     , replace(left(convert(varchar,getdate(),113),11),' ','/')+
       right(convert(varchar,getdate(),113),13) 
       as RecievingDate
FROM [CmsSMSDb].[dbo].[ReceivedMessages] 
       where Convert(date,ReceivedDateTime)>= @DateFrom 
       AND Convert(date,ReceivedDateTime)<= @DateTo

您只需要在第一部分使用替换功能,而不是像罗伯特的回答那样在第二部分使用。

于 2014-06-09T11:52:25.743 回答