简体   繁体   English

如何在SQL Server中将日期时间值插入dd-mmm-yyyy hh:mm:ss AM / PM格式

[英]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/PM31-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.

相关问题 如何在mssql中将NVARCHAR值转换为dd-mm-yyyy hh:mm:ss AM / PM格式 - How to convert NVARCHAR value to dd-mm-yyyy hh:mm:ss AM/PM format in mssql 将日期时间转换为MM / dd / yyyy HH:MM:SS AM / PM - Convert datetime to MM/dd/yyyy HH:MM:SS AM/PM 在SQL Server中将'EEE MMM dd HH:mm:ss zzz yyyy'格式varchar转换为Date - Converting 'EEE MMM dd HH:mm:ss zzz yyyy' format varchar to Date in SQL Server 如何将mm / dd / yyyy格式的日期转换为yyyy-mm-dd hh:mi:ss.mmm - How to convert date in mm/dd/yyyy format to yyyy-mm-dd hh:mi:ss.mmm 将 dd/mm/yyyy hh:mm:ss am/pm 转换为 yyyy/mm/dd hh:mm:ss - Convert dd/mm/yyyy hh:mm:ss am/pm to yyyy/mm/dd hh:mm:ss 如何格式化SQLServer DateTime(mm / dd / yyy HH:MM:SS AM / PM) - How to format SQLServer DateTime (mm/dd/yyy HH:MM:SS AM/PM) 在SQL Server中的datetime中插入dd / mm / yyyy hh:mm:ss.000 - Insert dd/mm/yyyy hh:mm:ss.000 in datetime in SQL Server 选择查询以在SQL Server 2008中将日期格式显示为mmm dd yyyy hh:mm am - Select query to display Date format as mmm dd yyyy hh:mm am in SQL server 2008 如何在Java中以所需格式(MM / DD / YYYY HH:MM:SS AM)创建SQL日期对象? - How to create SQL date object in a desired format ( MM/DD/YYYY HH:MM:SS AM ) in Java? 如何在SQL Server中获取日期格式(dd-MMM-yy HH:mm:ss)? - How to get date format (dd-MMM-yy HH:mm:ss) in SQL Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM