SQL 日期格式返回为 mm/dd/yyyy hh:mm:ss AM/PM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26063902/
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
Date format returned as mm/dd/yyyy hh:mm:ss AM/PM
提问by Chad Portman
I am brand new to sql my company just kinda threw me head long into this and said do it. So any help is greatly appreciated. I am trying to get a date to come out in the format of mm/dd/yyyy hh:mm:ss AM/PM so for example a date of 09/26/2014 11:04:54 AM. I have tried using the code:
我是 sql 的新手,我的公司只是把我的头扔进了这个并说去做。因此,非常感谢任何帮助。我正在尝试以 mm/dd/yyyy hh:mm:ss AM/PM 的格式获取日期,例如 09/26/2014 11:04:54 AM 的日期。我试过使用代码:
Select Convert(nvarchar,EntryDate,101)
From DB1
However that returns just 09/26/2014. I also tried
但是,这仅返回 2014 年 9 月 26 日。我也试过
Select Convert(nvarchar,EntryDate,100)
From DB1
but this returns Sep 26 2014 11:04AM
但这会返回 2014 年 9 月 26 日上午 11:04
Not sure where to go from here. Again thanks for the help. BTW I am using SQL Server 2012.
不知道从这里去哪里。再一次感谢你的帮助。顺便说一句,我正在使用 SQL Server 2012。
回答by M.Ali
DECLARE @Date_Value DATETIME = GETDATE();
SELECT CONVERT(VARCHAR(10), @Date_Value, 101) + ' '
+ LTRIM(RIGHT(CONVERT(CHAR(20), @Date_Value, 22), 11))
RESULT: 09/26/2014 5:25:53 PM
Your Query
您的查询
SELECT CONVERT(VARCHAR(10), EntryDate, 101) + ' '
+ LTRIM(RIGHT(CONVERT(CHAR(20), EntryDate, 22), 11))
From DB1
回答by jpw
Since you're on SQL 2012 the format function should work:
由于您使用的是 SQL 2012,因此格式函数应该可以工作:
declare @date datetime = '2014-09-26 11:04:54'
select FORMAT(@date,'MM/dd/yyyy hh:mm:s tt')
result: 09/26/2014 11:04:54 AM
结果: 09/26/2014 11:04:54 AM
In your case it would be:
在您的情况下,它将是:
Select FORMAT(EntryDate,'MM/dd/yyyy hh:mm:s tt')
From DB1
回答by Saikh Rakif
> select convert(varchar(20),GETDATE(),1)+' '+convert(varchar(20),convert(time,getdate()),100)
> select convert(varchar(20),GETDATE(),103)+' '+convert(varchar(20),convert(time,getdate()),100)
> select convert(varchar(20),GETDATE(),103)+' '+convert(varchar(20),convert(time,getdate()),22)
> Result (1) :- 10/15/2018 11:22AM (mm/dd/yyyy hh:mm AM/PM)
> Result (2):- 15/10/2018 11:22AM (dd/mm/yyyy hh:mm AM/PM)
> Result (3):- 15/10/2018 11:22:35 AM (dd/mm/yyyy hh:mm:ss AM/PM)
回答by Raj K
You can use FORMAT function in sql
您可以在 sql 中使用 FORMAT 函数
SELECT FORMAT(@date,'MM/dd/yyyy hh:mm:s tt')
回答by Chad Portman
As yet another way to solve the same problem this works.
作为解决相同问题的另一种方法,这是有效的。
Select Convert(char(10), getdate(),101) + Right(Convert(VarChar(20),getdate(),100),8)
回答by Pieter Geerkens
Combining two formats:
结合两种格式:
select convert(char(11),getdate(),103)
+ stuff(right(convert(char(31),getdate(),130),14),9,4,' ')
yields:
产量:
26/09/2014 12:29:09 PM
回答by raz_c
You should do this to convert a date in the format you ask for DATE_FORMAT(date,'%m %d %Y %h:%i %p'). Where date is the date you want converted.
您应该这样做以按照您要求的 DATE_FORMAT(date,'%m %d %Y %h:%i %p') 格式转换日期。其中 date 是您要转换的日期。
I hope this helps.
我希望这有帮助。
回答by radar
USING two formats and concatenating them with REPLACE and SUBSTRING functions.
使用两种格式并将它们与 REPLACE 和 SUBSTRING 函数连接起来。
select CONVERT(nvarchar(128), dbo.GetDateTime(@input), 101) +
REPLACE(SUBSTRING(CONVERT(nvarchar(128),
dbo.GetDateTime(@input), 109), 12 , 128),':000', ' ')