简体   繁体   English

SQL Server 2008 DATETIME2 格式问题

[英]SQL Server 2008 DATETIME2 Format Question

I am dealing with a date/time from Australia (I am located in the USA).我正在处理来自澳大利亚的日期/时间(我位于美国)。 I am unable to get the following string to insert into a DATETIME2 column:我无法将以下字符串插入到 DATETIME2 列中:

2010/19/10 04:38:12.892

As you can see, it's formatted in yyyy/dd/mm HH:MM:ss.MMM format.如您所见,它的格式为yyyy/dd/mm HH:MM:ss.MMM格式。 I understand that the normal format is yyyy-mm-dd HH:MM:ss.MMM .我知道正常格式是yyyy-mm-dd HH:MM:ss.MMM What I am wondering is if there is a locality setting on SQL Server that I can change to get it to accept this format, or if I need to parse it and rearrange it myself.我想知道的是,我是否可以更改 SQL Server 上的位置设置以使其接受这种格式,或者我是否需要解析它并自己重新排列它。

EDIT: Just for your information, I have been importing a mm/dd/YYYY HH:MM:ss.MMM format string into the field just fine.编辑:仅供参考,我一直在将mm/dd/YYYY HH:MM:ss.MMM格式字符串导入该字段就好了。

It depends on the "locale" date format, I guess.我猜这取决于“语言环境”日期格式。

Some samples on how to convert here : http://www.java2s.com/Code/SQLServer/Date-Timezone/Formatdatemmddyyyy.htm关于如何在此处转换的一些示例: http : //www.java2s.com/Code/SQLServer/Date-Timezone/Formatdatemmddyyyy.htm

Hope this was helpful.希望这是有帮助的。

Try issuing SET DATEFORMAT ydm;尝试发出SET DATEFORMAT ydm; before INSERT .INSERT之前。 Then CONVERT(DATETIME,('2010/19/10 04:38:12.892'));然后CONVERT(DATETIME,('2010/19/10 04:38:12.892')); works fine.工作正常。

More info更多信息

You can try this:你可以试试这个:

SET DATEFORMAT YDM;
select cast('2010/19/10 04:38:12.892' as datetime)

And it will parse it correctly它会正确解析它

UPDATE: I found help here.更新: 我在这里找到了帮助。

But I tried casting to datetime2 directly and it didn't work.但是我尝试直接转换为 datetime2 并且没有用。 I don't understand why you can cast to datetime and not datetime2.我不明白为什么你可以投射到 datetime 而不是 datetime2。 Perhaps a good question for SO.对于SO来说,这也许是个好问题。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM