简体   繁体   English

SQL Server算术溢出错误-将nvarchar转换为datetime

[英]SQL Server Arithmetic Overflow Error - converting nvarchar to datetime

I'm retrieving data from a view, and one of the columns I'm using is nvarchar(50) , but is only ever N'True' or N'False' , depending on the operation of a related date column in this parent view. 我正在从视图中检索数据,而我正在使用的列之一是nvarchar(50) ,但仅是N'True'N'False' ,具体取决于此父级中相关日期列的操作视图。

The following code retrieves the record ID and the column I'm looking for, YTD : 以下代码检索记录ID和我要查找的列YTD

SELECT Enquiry_Number, YTD 
FROM dbo.vw_SalesPO_YTD

Output: 输出:

ENQ-001 True
ENQ-002 False
ENQ-003 True

However, I'm unable to filter my results using this YTD column for some reason. 但是,由于某些原因,我无法使用此YTD列过滤结果。 If I attempt to do this: 如果我尝试这样做:

SELECT Enquiry_Number, YTD
FROM dbo.vw_SalesPO_YTD
WHERE YTD = N'True'

Then it fails with the following error: 然后它失败并显示以下错误:

Arithmetic overflow error converting expression to data type datetime. 将表达式转换为数据类型datetime的算术溢出错误。

Which I don't understand because there are no datetime expressions in play in this query. 我不明白,因为此查询中没有datetime表达式在起作用。 Yes the True or False was determined by comparing datetimes in the parent view, but I don't understand how that might have trickled down to this subquery. 是的TrueFalse是由父视图比较日期时间确定,但我不明白怎么可能惠及到这个子查询。 Attempting the same thing in the parent view yields the same error - I'm demonstrating it this way for simplicity's sake. 在父视图中尝试相同的操作会产生相同的错误-为了简单起见,我将以此方式进行演示。

However, performing a similar operation in the SELECT portion of the query works without issues: 但是,在查询的SELECT部分执行类似的操作不会出现问题:

SELECT 
    Enquiry_Number,
    YTD,
    CASE 
       WHEN YTD = N'True' THEN 1 ELSE 0 
    END As C
FROM
    dbo.vw_SalesPO_YTD 

Output: 输出:

ENQ-001 True 1
ENQ-002 False 0
ENQ-003 True 1

However, these 1's and 0's inherit the same flaw, where I can't use them in a WHERE clause without getting this datetime error. 但是,这些1和0继承了相同的缺陷,在这里我无法在WHERE子句中使用它们,而不会出现此datetime错误。

I've been searching hard and am not sure how to identify the core issue. 我一直在努力搜索,不确定如何确定核心问题。 I've been reading things about Collations and type precedence, but can't understand why this behaviour is happening. 我一直在阅读有关排序规则和类型优先级的内容,但无法理解为什么会发生这种行为。

When I've checked YTD in INFORMATION_SCHEMA.COLUMNS , it confirms that this column is no different from other columns in my table: YTD is nvarchar(50) , using the Latin1_General_CI_AS collation. 当我在INFORMATION_SCHEMA.COLUMNS检查了YTD时,它确认此列与表中的其他列没有不同: YTDnvarchar(50) ,使用Latin1_General_CI_AS归类。

Related question: SQL Server Arithmetic overflow error converting expression to data type datetime 相关问题: SQL Server算术溢出错误将表达式转换为数据类型datetime


The Source of the Problem 问题的根源

This issue is still unsolved, but if you wish to reproduce it, this code from the parent view must be generating this issue: 此问题仍未解决,但是如果您想重现此问题,则必须从父视图查看以下代码来产生此问题:

CASE WHEN 
Award_Date <= DATEFROMPARTS(FinancialYear - 1, 11, 1) + GETDATE() - DATEADD(year, DATEDIFF(month, '20161101', GETDATE()) / 12, '20161101')
THEN N'True'
ELSE N'False'
END

Yes this looks overly complicated. 是的,这看起来过于复杂。 We're checking the Award_Date against its associated FinancialYear , which runs from November 1st to October 31st. 我们正在对照其关联的FinancialYear (从11月1日到10月31日)检查Award_Date Each record already knows which FinancialYear it's in. The ultimate aim is to compare TODAY's position (2016-11-30) against TODAY last year (2015-11-30), and TODAY the year before (2014-11-30), etc. 每个记录都已经知道它在哪个FinancialYear中。最终目的是将TODAY的位置(2016-11-30)与去年的TODAY(2015-11-30)和前一年的TODAY(2014-11-30)进行比较,等等。 。

So the code takes today's date and combines it with the FinancialYear for the associated record, and spits out whether the record had occurred between the start of its financial year and the today of the same year. 因此,该代码采用今天的日期,并将其与FinancialYear结合使用以获取相关记录,并吐出该记录是否在其财政年度开始至同一年的今天之间发生。 And it's doing this successfully, but then I can't do anything with the N'True' or N'False' it's producing. 它成功完成了此操作,但随后我无法对其产生的N'True'N'False'做任何事情。

I do not know what the type of the source YTD is. 我不知道源YTD的类型。

Try using the following: 尝试使用以下内容:

SELECT Enquiry_Number, [YTD] FROM (
    SELECT Enquiry_Number, CONVERT(nvarchar(10),YTD) AS [YTD] FROM dbo.vw_SalesPO_YTD
) AS A
WHERE A.YTD = N'True'

10 is just a thump suck value. 10只是重击吸值。 It will cut of any part of the field longer that 10. It depends on your actual field size. 它将剪切字段的任何部分的时间超过10。这取决于您的实际字段大小。

SELECT * FROM (SELECT Enquiry_Number, YTD FROM dbo.vw_SalesPO_YTD) AS A WHERE cast(A.YTD as varchar) = 'True'

I used the following as an example: 我以以下为例:

DECLARE @Data TABLE
(
    Enquiry_Number nvarchar(10),
    YTD nvarchar(50)
)

INSERT INTO @Data(Enquiry_Number, YTD)
SELECT N'ENQ-001', N'True' UNION
SELECT N'ENQ-002', N'False' UNION
SELECT N'ENQ-003', N'True' 


SELECT Enquiry_Number, [YTD] FROM (
    SELECT Enquiry_Number, CONVERT(nvarchar(10),YTD) AS [YTD] FROM @Data
) AS A
WHERE A.YTD = N'True'

Result: 结果:

ENQ-001 True
ENQ-003 True

There must be results in the YTD field that causes to return it as a datetime type. YTD字段中必须有结果,该结果导致将其作为日期时间类型返回。

Try a query like: 尝试如下查询:

SELECT * FROM dbo.vw_SalesPO_YTD WHERE ISDATE(YTD)= 1

Updated Question: Try: 更新的问题:尝试:

ISNULL(CASE WHEN  Award_Date <= DATEFROMPARTS(FinancialYear - 1, 11, 1) + GETDATE() - DATEADD(year, DATEDIFF(month, '20161101', GETDATE()) / 12, '20161101') THEN N'True' ELSE N'False' END, 'false')

试试这个:-不要在字符串前添加n

SELECT * FROM (SELECT Enquiry_Number, YTD FROM dbo.vw_SalesPO_YTD) AS A WHERE A.YTD = 'True'

This is a successful workaround , not a solution per se or explanation of the core issue. 这是一个成功的解决方法本身不是解决方案 ,也不是核心问题的解释。

There's obviously an issue with the data coming out of the view, which will not allow YTD column's results to be operated on in a WHERE clause, and yet they can be operated on by the time the query reaches its SELECT phase. 数据从视图中出来显然存在问题,这将不允许在WHERE子句中操作YTD列的结果,但是可以在查询到达其SELECT阶段时对其进行操作。

I've created a new table which explicitly defines the YTD column as nvarchar(50) , and then inserted all the records from my view into this table, which has resolved the issue. 我创建了一个新table ,该tableYTD列显式定义为nvarchar(50) ,然后将视图中的所有记录插入该表中,从而解决了该问题。 The records can then be sorted and filtered by YTD as they are supposed to. 然后,可以按照预期的方式通过YTD对记录进行排序和过滤。

暂无
暂无

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

相关问题 SQL Server算术溢出错误将表达式转换为数据类型datetime - SQL Server Arithmetic overflow error converting expression to data type datetime “将表达式转换为数据类型日期时间的算术溢出错误” SQL SERVER - “Arithmetic overflow error converting expression to data type datetime” SQL SERVER 将float转换为datetime时的算术溢出错误(SQL Server 2014) - Arithmetic overflow error when converting float to datetime (SQL Server 2014) 在SQL Server 2008算术溢出中将nvarchar转换为datetime - Convert nvarchar to datetime in SQL Server 2008 arithmetic overflow 将表达式转换为数据类型nvarchar的SQL算术溢出错误 - SQL Arithmetic overflow error converting expression to data type nvarchar 在SQL Server中将表达式转换为数据类型datetime错误的算术溢出错误 - Arithmetic overflow error converting expression to data type datetime error in sql server 在SQL Server 2008中将表达式转换为数据类型datetime的算术溢出错误 - Arithmetic overflow error converting expression to data type datetime in SQL Server 2008 SQL / Python 尝试检查数据是否已存在:将 nvarchar 转换为数据类型数字的算术溢出错误 - SQL / Python Trying to check if data already exists: Arithmetic overflow error converting nvarchar to data type numeric SQL Server:将数据类型nvarchar转换为datetime时出错 - SQL Server : error converting data type nvarchar to datetime 将数据类型 nvarchar 转换为日期时间 SQL Server 时出错 - Error converting data type nvarchar to datetime SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM