如何使用 sql server 2008 r2 将日期显示为 mm/dd/yyyy hh:mm Am/PM?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15020865/
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
How to display the date as mm/dd/yyyy hh:mm Am/PM using sql server 2008 r2?
提问by Adalarasan_Serangulam
My sample query is
我的示例查询是
SELECT D30.SPGD30_LAST_TOUCH_Y
from CSPGD30_TRACKING D30
My given date format is like "2013-01-01 00:00:00.000"
. I need to convert this date format to "mm/dd/yyyy hh:mm AM/PM"
. Do you have any idea about that?
我给定的日期格式就像"2013-01-01 00:00:00.000"
. 我需要将此日期格式转换为"mm/dd/yyyy hh:mm AM/PM"
. 你有什么想法吗?
回答by Kaf
I think there is no single format to give them both. Try this using Convert
; Sql-Demo
我认为没有单一的格式可以同时提供它们。试试这个使用Convert
; Sql-Demo
declare @mydate datetime = getdate()
select convert(varchar(10),@mydate, 101) + right(convert(varchar(32),@mydate,100),8)
| COLUMN_0 |
----------------------
| 02/22/2013 9:36AM |
回答by KateA
The FORMAT() function is available from version 2012 onwards. Once upgraded, you can use
FORMAT() 函数从 2012 版开始可用。升级后即可使用
select FORMAT(@date,'MM/dd/yyyy hh:mm:s tt')
回答by DevelopmentIsMyPassion
Use this
用这个
select CONVERT(VARCHAR(10), mydate, 101) + ' ' + RIGHT(CONVERT(VARCHAR, mydate, 100), 7) from tablename
回答by Meherzad
回答by Charan Ghate
Use the following scenario for getting date,time,day,month,year,hours,minutes,seconds,AM/PM
使用以下场景获取日期、时间、日、月、年、小时、分钟、秒、AM/PM
:)
:)
SELECT UpdatedOn ,
CONVERT(varchar,UpdatedOn,100) DateTime,
CONVERT(varchar,UpdatedOn,10) Date ,
CONVERT(varchar,UpdatedOn,108) Time ,
substring(CONVERT(varchar,UpdatedOn,106),1,2) Day,
substring(CONVERT(varchar,UpdatedOn,106),4,3) CMonth,
substring(CONVERT(varchar,UpdatedOn,105),4,2) NMonth,
substring(CONVERT(varchar,UpdatedOn,106),8,4) Year,
left(right(CONVERT(varchar,UpdatedOn,100),7),2) Hours_12,
substring(CONVERT(varchar,UpdatedOn,108),1,2) Hours_24,
substring(CONVERT(varchar,UpdatedOn,108),4,2) Minutes,
substring(CONVERT(varchar,UpdatedOn,108),7,2) Second,
right(CONVERT(varchar,UpdatedOn,100),2) AM_PM
FROM dbo.DeviceAssignSim
WHERE AssignSimId=55;
回答by user1848942
You can do it like this:
你可以这样做:
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]
For more information look this:
有关更多信息,请查看:
回答by Guffa
Use the convert
method to format a datetime
value. Example:
使用convert
方法格式化datetime
值。例子:
select convert(varchar(20), D30.SPGD30_LAST_TOUCH_Y, 101)
The third parameter determines the format. You can find the available formats in the cast and convert documentation.
第三个参数决定格式。您可以在cast and convert 文档中找到可用的格式。