SQL强制转换或转换函数

SQL强制转换或转换函数,sql,sql-server,tsql,casting,sql-server-2000,Sql,Sql Server,Tsql,Casting,Sql Server 2000,您好,我是SQL新手,需要将下面的cast语句从varchar(255)转换为decimal(6,2)我尝试了许多选项,但未能实现 case when [ A 2 cost] = ' £- ' then '0' else [ A 2 cost] end as Costing 目标中[A 2 Cost]的结果是十进制(6,2)数据类型,但在源代码中它是varchar(255)试试这个 case when [ A 2 cost] = ' £- ' then 0.0 else ca

您好,我是SQL新手,需要将下面的cast语句从
varchar(255)
转换为
decimal(6,2)
我尝试了许多选项,但未能实现

case when [ A 2 cost] = ' £-   ' then '0' else [ A 2 cost] end as Costing
目标中
[A 2 Cost]
的结果是
十进制(6,2)
数据类型,但在源代码中它是
varchar(255)

试试这个

    case when [ A 2 cost] = ' £-   ' 
then 0.0 else cast([ A 2 cost] as decimal(5,2)) end as Costing
如果其他列开头包含%,则需要清理:

    case when [ A 2 cost] = ' £-   ' 
then 0.0 else cast(REPLACE([ A 2 cost],'£','') as decimal(5,2)) end as Costing

我使用的是Microsoft SQL Server 2000-8.00.2066(英特尔X86)。您所说的
[A 2 cost]
是什么意思?它是一个列名。请给出一些可能位于
A 2 cost
中的数据示例。你现在有什么问题?您是否知道,您在列名中使用了空格(在
A
之前),但不是所有的地方?列是从一个平面文件,它的价值像英镑和105.55英镑,空白只是一个来自我的打字忽略列名内的空白,我得到错误转换数据类型VARCHAR到数字。对不起,我不能解释清楚的问题,因为我是一个新手这个地区,但U排序正确。