[英]How to insert datetime value as dd-mmm-yyyy hh:mm:ss AM/PM format in SQL Server
I'm new to SQL Server, I want to insert a datetime value as in the following format dd-mmm-yyyy hh:mm:ss AM/PM
( 31-May-2019 11:06:37 PM
). 我是SQL Server的新手,我想插入一个日期时间值,格式如下dd-mmm-yyyy hh:mm:ss AM/PM
( 31-May-2019 11:06:37 PM
)。
To do so, I have used the following code: 为此,我使用了以下代码:
REPLACE(CONVERT(NVARCHAR, (GETDATE()), 106), ' ', '-') + ' ' +
LTRIM(RIGHT(CONVERT(VARCHAR(30), GETDATE(), 22), 11))
Output : 31-May-2019 11:06:37 PM 输出:201-May-2019 11:06:37 PM
Column datatype : datetime
列数据类型: datetime
When I execute the following query, I get the result as I expect, but while using this in an insert statement, it stores the value as follows 当我执行以下查询时,我得到了我期望的结果,但是在insert语句中使用它时,它将值存储如下
2019-05-31 23:02:47.000
The format has changed. 格式已更改。
Can anyone help me to fix this? 任何人都可以帮我解决这个问题吗?
It's good that you store it as datetime. 将它存储为日期时间是件好事。 Now you can display it in selects/reports as you want using convert()
. 现在,您可以根据需要使用convert()
在选择/报告中显示它。 Check this cheatsheet . 检查这个备忘单 。
You can also use format()
: 您还可以使用format()
:
SELECT FORMAT(cast('2019-05-31 23:02:47.000' as datetime), 'dd-MMM-yyyy hh:mm:ss tt') as datetime
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.