不可能在SQL Server中存储某些datetime格式 [英] Impossible to store certain datetime formats in SQL Server

查看:141
本文介绍了不可能在SQL Server中存储某些datetime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个列保存时间戳为 datetime



数据存储为 10/30/2011 10:50:34 AM



我的目标是转换该特定表格中的每个日期格式如 2011年10月30日10:50:34



问题:



当我尝试运行此SQL时,它会成功处理。

 更新日期表
SET DateTime = '30 Oct 2011 10:50:34'
WHERE DateTime = '10 / 30/2011 10:50:34 AM'

我注意到这很奇怪,所以如果我将SQL更改为

 更新日期表
SET DateTime = '31 Oct 2011 10:50:34'
WHERE DateTime = '10 / 30/2011 10:50:34 AM'

它更新更新日期时间字段具有正确的日期/时间信息,但保持相同的格式。



我已经研究了该列的其他可能的日期数据类型,但似乎并不支持日期。
我也遇到这个SQL,但不确定是否可以解决我的问题。

  SELECT CONVERT(datetime, datecolumnname,formattingparam)as tmp FROM tablename 

转换链接

解决方案

SQL Server不存储一个 DateTime 任何字符串格式 - 它存储为一个8字节的数值。



各种设置(语言,日期格式)仅影响SQL Server Management Studio中向您显示 DateTime 的方式 - 或者当您尝试将字符串转换为 DateTime 。



SQL Server支持许多格式 - 请参阅 MSDN联机丛书上的CAST和CONVERT 。这些格式中的大多数依赖于依赖于您所拥有的设置 - 因此,这些设置可能会有一段时间 - 有时不会。



解决这个问题是使用SQL Server支持的 ISO-8601日期格式 - 此格式始终始终,无论您使用SQL Server语言和日期格式设置。



ISO-8601格式由SQL Server支持有两种口味:




  • YYYYMMDD 日期(没有时间部分) - 请注意:不破折号,这非常重要! YYYY-MM-DD NOT ,与SQL Server中的dateformat设置无关,并且 NOT 可在所有情况下工作!



或:




  • YYYY-MM-DDTHH:MM:SS 日期和时间 - 请注意:此格式破折号。



    • 这对于SQL Server 2000和更新版本是有效的。



      如果您使用SQL Server 2008和 DATE 数据类型(仅 DATE - DATETIME !),那么你也可以使用 YYYY-MM-DD 格式,这样也可以在SQL Server中的任何设置下工作。



      不要问我为什么这个整个话题是如此棘手,有点令人困惑 - 就是这样。但是,使用 YYYYMMDD 格式,您应该对SQL Server的任何版本以及SQL Server中的任何语言和日期格式设置都适用。


      At the moment I have a column that holds timestamps as a datetime

      The data is being stored as 10/30/2011 10:50:34 AM

      My goal is to convert every date in that specific table to be formatted like 30 Oct 2011 10:50:34

      The problem:

      When I attempt to run this SQL it successfully processes.

      UPDATE DatesTable 
      SET DateTime = '30 Oct 2011 10:50:34' 
      WHERE DateTime = '10/30/2011 10:50:34 AM'
      

      I noticed that was weird so if I changed the SQL to say

      UPDATE DatesTable 
      SET DateTime = '31 Oct 2011 10:50:34' 
      WHERE DateTime = '10/30/2011 10:50:34 AM'
      

      It updates updates the datetime field with the correct day/time information but keeps it in the same format.

      I have looked into other possible date datatypes for that column but nothing seems to support that date. I have also come across this SQL but am unsure if it can solve my problem.

      SELECT CONVERT(datetime, datecolumnname, formattingparam) as tmp FROM tablename
      

      Converting Link

      解决方案

      SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value.

      The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime.

      There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not.

      The way to solve this is to use the ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.

      The ISO-8601 format is supported by SQL Server comes in two flavors:

      • YYYYMMDD for just dates (no time portion) - note here: no dashes!, that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!

      or:

      • YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes.

      This is valid for SQL Server 2000 and newer.

      If you use SQL Server 2008 and the DATE datatype (only DATE - not DATETIME!), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server.

      Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.

      这篇关于不可能在SQL Server中存储某些datetime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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