Conversion error from string from change to table column and one line in query.

huangapple go评论67阅读模式
英文:

Conversion error from string from change to table column and one line in query

问题

我有一个表格,包含多列。相关的列有:

  1. 一个叫做 HoursNUMERIC
  2. 一个叫做 ActiveBOOLEAN
  3. 一个叫做 TypeVARCHAR

Type 原本也是 BOOLEAN 类型,直到我需要超过两个可能值时,我将该列改为了 VARCHAR 类型。

我有一个原本有效的大型查询。以下是我修改的查询的一部分:

(
	SELECT SUM("Hours")
    FROM "Table"
	WHERE
		(
    		"Type" = 'B'
	    	AND "Active" = TRUE
	    )
) AS "Label",

上述查询部分的唯一更改是从 "Type" = TRUE 改为了 "Type" = "B"。没有其他行发生改变。现在当我运行查询时,我收到以下错误:

> 字符串 "A" 的转换错误

如果我将同一行改为 "Type" = "A",或者 "Type" = "C",错误会变为:

*列未知
*A

或者:

*列未知
*C

我的目标是在 Table 中对 ActiveTRUE,且为指定 Type 的行的 Hours 进行求和。我不确定为什么一个看似简单的查询会失败。

英文:

I have a table with multiple columns. The relevant columns are:

  1. A NUMERIC column called Hours
  2. A BOOLEAN column called Active
  3. A VARCHAR column column called Type

Type used to also be BOOLEAN until I needed more than two possible values, so I changed the column to VARCHAR

I have a large query that used to work. Below is the part of the query I modified:

(
	SELECT SUM("Hours")
    FROM "Table"
	WHERE
		(
    		"Type" = "B"
	    	AND "Active" = TRUE
	    )
) AS "Label",

The only change in the above query section being from "Type" = TRUE to "Type" = "B". No other line was changed. Now when I run the query, I receive the following error:

> Conversion error from string "A"

If I change that same line to "Type" = "A", or "Type" = "C", the error changes to:

*column unknown
*A

Or:

*column unknown
*C

My goal is to sum up Hours in Table, where the rows are TRUE for Active and of the specified Type. I'm not sure why an otherwise (seemingly) simple query fails.

答案1

得分: 1

第一个问题是,在条件 "Type" = TRUE 中,Firebird 会将列 Type 的值强制转换为布尔值,而不是将 TRUE 转换为字符串。这会导致对于不是 'TRUE''FALSE'(不区分大小写)的值失败。你需要使用 = 'TRUE' 而不是 = TRUE

至于第二个问题,在标准 SQL 和 Firebird(至少在方言 3 中,Firebird 的默认方言)中,双引号用于界定标识符,而不是字符串。你需要使用 'A''C',而不是 "A""C" 作为字符串。当你使用 "A" 时,Firebird 将其视为对列 A 的引用,而在这种情况下,该列不存在。

英文:

The first problem is that in the condition "Type" = TRUE, Firebird will coerce the values of column Type to a Boolean instead of converting TRUE to a string. This fails for values that are not 'TRUE' or 'FALSE' (case-insensitive). You need to use = 'TRUE' instead of = TRUE.

As to the second problem, double quotes in standard SQL and Firebird (at least in dialect 3, Firebird's default dialect) delimit identifiers, not strings. You need to use 'A' and 'C', not "A" or "C" for strings. When you use "A", Firebird sees it as a column reference to a column A, which in this case doesn't exist.

huangapple
  • 本文由 发表于 2023年5月22日 12:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定