如何将日期时间转换为SQL Server 2008中的通用格式? [英] how to get datetime to a common format in sql server 2008?

查看:117
本文介绍了如何将日期时间转换为SQL Server 2008中的通用格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想以通用格式获取日期时间.如果日期时间为"2015年5月8日下午5:37"

或"2015年5月5日下午5:37".我正在使用

选择convert(varchar,''2015/05/08 5:37 PM'',126)

但没有得到所需的结果. ;(


谢谢...



i want to get datetime in a common format. if datetime in ''05/08/2015 5:37 PM''

or ''08/05/2015 5:37 PM''. i am using

select convert(varchar,''05/08/2015 5:37 PM'',126 )

but not getting the required result. ;(


thanks...

推荐答案

首先为什么将字符串转换为字符串?您可能是说:
First of all why convert string to string?! You probably meant:
convert(datetime, '05/08/2015 5:37 PM', 126)


但是代码126需要ISO8601格式...
对于您的格式,您可以使用101或103,具体取决于它是dd/mm还是mm/dd ...


But code 126 expects ISO8601 format...
For your format you may use 101 or 103, depending on wether it is dd/mm or mm/dd...


进一步解决方案1,您可能会在有关设置DateFormat的文档 [设置语言的链接 [链接 [
Further to Solution 1 you might find the comments on the documentation on Set DateFormat[^] useful to explain what is going on.

Also note the link to set Language[^]

KEP also mentioned ISO 8601 format - here is a link[^] to explain - it''s main benefit is that is unambiguous regardless of the culture settings on either PC or Server (which may be different)

Finally - your use of varchar on the convert makes me suspicious that you may be attempting to store dates as varchar on your database. Don''t. Always use the most appropriate data type for the data you are trying to store.


这篇关于如何将日期时间转换为SQL Server 2008中的通用格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆