Sql varchar列与十进制列的数字比较
我有下列表格 产品信息:Sql varchar列与十进制列的数字比较,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有下列表格 产品信息: ID|productname|productarea|productcost|productid ID|salesid|productid| ID|productid|productname|salestotal 销售: ID|productname|productarea|productcost|productid ID|salesid|productid| ID|productid|productname|salestotal 销售数据: ID|prod
ID|productname|productarea|productcost|productid
ID|salesid|productid|
ID|productid|productname|salestotal
销售:
ID|productname|productarea|productcost|productid
ID|salesid|productid|
ID|productid|productname|salestotal
销售数据:
ID|productname|productarea|productcost|productid
ID|salesid|productid|
ID|productid|productname|salestotal
我遇到的问题是:salesdata.salestotal
是一个varchar
列,可能有空值
比较saledata.saletotal
与productinfo.productcost
列
我能做一件事
cast(salesdata.saletotal as float(7)) > X
它是有效的。我想做的是
cast(salesdata.saletotal as float(7)) > productinfo.productcost
where
sales.productid = salesdata.productid and
productinfo.productid = salesdata.productid
但是,当我这样做时,我会得到一个错误:
将类型varchar转换为float时出错
我找到了一篇类似的文章,但我无法用它获得任何其他专栏。我无法更改当前的数据库结构 如果您使用的是sql server 2012及更高版本,则可以使用或 或 在2012年之前,我会使用
patindex('%[^0-9.-]',salesdata.saletotal)=0
来确定某个东西是否是数字的,因为isnumeric()
有点像数字
e、 g.雷克斯测试仪:
如果您使用的是sql server 2012及更高版本,则可以使用或 或 在2012年之前,我会使用
patindex('%[^0-9.-]',salesdata.saletotal)=0
来确定某个东西是否是数字的,因为isnumeric()
有点像数字
e、 g.雷克斯测试仪:
您可以在where子句中添加
IsNumeric()
,以检查salesdata.saletotal
是否可以解析为float
SELECT
cast(salesdata.saletotal as float(7)) > productinfo.productcost
FROM Sales,Salesdata
WHERE
sales.productid = salesdata.productid and
productinfo.productid = salesdata.productid and
IsNumeric(salesdata.saletotal) =1
或者您可以使用不喜欢
(比IsNumeric更好)
您可以在where子句中添加
IsNumeric()
,以检查salesdata.saletotal
是否可以解析为float
SELECT
cast(salesdata.saletotal as float(7)) > productinfo.productcost
FROM Sales,Salesdata
WHERE
sales.productid = salesdata.productid and
productinfo.productid = salesdata.productid and
IsNumeric(salesdata.saletotal) =1
或者您可以使用不喜欢
(比IsNumeric更好)
您使用的是哪个版本的sql server<代码>选择@@version作为“sql server version”由于您出现错误,我猜您在那里有数字以外的内容,或者例如数字的小数点分隔符错误等。sql server 2008您使用的是哪一版本的sql server<代码>选择@@version作为“sql server version”既然您得到了错误,我猜您的答案中除了数字之外还有其他内容,例如数字的小数点分隔符错误等等。sql server 2008我想补充一点,说明这如何在您的答案中反映“误报”,以便OP了解该函数的工作原理。这绝对是一个值得阅读的函数,而不是仅仅接受它的名称作为预期输出。
select isnumeric(“$”)select isnumeric('12e4')select isnumeric('12,55')
。这绝对是一个值得阅读的函数,而不是仅仅接受它的名称作为预期输出。select isnumeric('$')select isnumeric('12e4')select isnumeric('12,55')
isnull(cast(salesdata.saletotal作为float(7)),0.0)>isnull(cast(productinfo.productcost作为float(7)),0)我仍然得到错误。@liquidacid为什么您认为ISNULLing可以工作?将表中的任何Null都设置为0。我正在查看msdn并尝试使用isnull(cast(salesdata.saletotal为float(7)),0.0)>isnull(cast(productinfo.productcost为float(7)),0),但仍然得到错误。@liquidacid为什么您认为ISNULLing会起作用?将表中的任何Null都设置为0。我正在看msdn并尝试一些东西