且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将日期时间转换为SQL Server 2008中的通用格式?

更新时间:2023-11-29 13:11:46

首先为什么将字符串转换为字符串?您可能是说:
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.