在sql中转换函数

时间:2014-06-09 10:28:57

标签: sql tsql datetime sql-convert

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

 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

这就是我想要的,它运作得很完美,但问题是我想删除&#39; /&#39; 2014年之后,如09/Jun/2014 10:04:07:000

但在其他地方&#39; /&#39;这应该保持不变,但不应该在2014年之后出现。

怎么样?

5 个答案:

答案 0 :(得分: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

答案 1 :(得分: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小时格式,HH适用于24小时格式,您可以添加tt以获取AMPM,看看差异尝试

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)))

答案 2 :(得分: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

答案 3 :(得分: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

答案 4 :(得分: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

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