转换日期时出错 [英] Error while converting date

查看:72
本文介绍了转换日期时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串09032017转换或转换为日期





 



消息242,级别16,状态3,行3 
将varchar数据类型转换为日期时间数据类型导致超出范围的价值。





我尝试过:



选择CAST('09032017'作为日期时间)

解决方案

< blockquote>首先决定是在3月9日,也就是9月3日。

无论如何,这都不算太糟糕。假设前者:

  DECLARE   @ dat   VARCHAR  8 
SET @ dat = ' 09032017'
SELECT CAST(SUBSTRING( @ dat 5 4 )+ SUBSTRING( @ dat 3 2 )+ SUBSTRING( @ dat 1 2 AS DATETIME

对于后者:

  DECLARE  < span class =code-sdkkeyword> @ da t   VARCHAR  8 
SET @ dat = ' 09032017'
SELECT CAST(SUBSTRING( @ dat 5 4 )+ SUBSTRING( @ dat 1 2 )+ SUBSTRING( @ dat 3 2 AS DATETIME


正如我在之前的帖子中所说的......



在传递信息时总是尝试使用明确的日期 - 尤其是数据库之间的数据。



这实际上是什么? EAN?这意味着使用一种日期格式,无论您在哪个国家/地区或时区都使用哪种语言都不会被误解。



示例:01/06 / 2016将于6月1日在英国,但1月6日在美国。



你们非常接近因为你使用了dd MMM yyyy作为格式。这在大多数语言中都很好......2017年2月27日适用于英语,荷兰语,德语等,但在芬兰语中没有意义(2月的芬兰语是helmikuu)。现在,将SQL Server服务器托管在运行应用程序的不同国家/地区并不罕见。让来自多个国家/地区的用户访问同一个SQL数据库也不常见。



有两种格式的日期可以保证适用于所有版本的SQL Server。它们也适用于大多数其他数据库。

 yy-MM-ddTHH24:mi:ss 

 yyyyMMdd HH24: mi:ss 

同样值得阅读ISO标准的日期 - ISO 8601 - Wikipedia [ ^ ]



如果这不能解决您的具体问题,那么在

 itmarr(行上放置一个断点(F9) 1  0 )=格式(TxtDate.Text,  dd MMM yyyy

并查看TxtDate的内容 - 您可能会发现它根本无法格式化为日期。您应该添加一些验证或使用DatePicker控件。


尝试将字符串日期值转换为DATETIME数据类型但日期值包含无效日期时会发生此错误。日期值(日,月和年)的各个部分都是数字,但它们一起不构成有效日期。



 转换 DATETIME  CONVERT  VARCHAR  2 ), @ Month ) + '  /' +  CONVERT (< span class =code-keyword> VARCHAR ( 2 ), @ Day 
+ ' /' + CONVERT VARCHAR 4 ), @ Year ))


How can I convert  or cast string "09032017" as date



and getting error as


Msg 242, Level 16, State 3, Line 3
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.



What I have tried:

select CAST('09032017' as datetime)

解决方案

Start by deciding if that's 9th March, or 3rd of Sept.
Either way, it's not too bad. Assuming the former:

DECLARE @dat VARCHAR(8)
SET @dat = '09032017'
SELECT CAST(SUBSTRING(@dat, 5,4) + SUBSTRING(@dat, 3, 2) + SUBSTRING(@dat, 1, 2) AS DATETIME)

For the later:

DECLARE @dat VARCHAR(8)
SET @dat = '09032017'
SELECT CAST(SUBSTRING(@dat, 5,4) + SUBSTRING(@dat, 1, 2) + SUBSTRING(@dat, 3, 2) AS DATETIME)


As I said on an earlier post ...

Always try to use "unambiguous dates" when passing information around - especially to and from databases.

What does that actually mean? It means use a date format that cannot be misinterpreted no matter which in country or time zone you're in nor which language is being used.

Example: 01/06/2016 would be 1st June in the UK but 6th January in the USA.

You are pretty close because you have used dd MMM yyyy as the format. Which is fine in most languages ... "27 Feb 2017" works in English, Dutch, German, etc etc but in Finnish would be meaningless (Finnish for February is helmikuu). It's not unusual these days to have SQL Server servers hosted in different countries to the one where the application is running. Nor is it unusual to have users from multiple countries accessing the same SQL database.

There are two formats for dates that are guaranteed to work for all versions of SQL Server. Usefully they work for most other databases as well.

yy-MM-ddTHH24:mi:ss

and

yyyyMMdd HH24:mi:ss

It's also worth having a read up on the ISO standard for dates - ISO 8601 - Wikipedia[^]

If this doesn't solve your specific problem, then put a breakpoint (F9) on the line

itmarr(1, 0) = Format(TxtDate.Text, "dd MMM yyyy")

and have a look at the contents of TxtDate - you'll probably find that it can't be formatted as a date at all. You should really add some validation OR use a DatePicker control.


This error occurs when trying to convert a string date value into a DATETIME data type but the date value contains an invalid date. The individual parts of the date value (day, month and year) are all numeric but together they don’t form a valid date.

Convert(DATETIME, CONVERT(VARCHAR(2), @Month) + '/' + CONVERT(VARCHAR(2), @Day)
+ '/' + CONVERT(VARCHAR(4), @Year))


这篇关于转换日期时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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