1

我想从 varchar 类型的数据库中检索数据,例如 size: 100mm,100 ,20mm, 500, 450mm 并从字符串中修剪“mm”,然后将其转换为整数,然后从同一列进行比较:例如@Size1 <= size_column 和 @size2 >= size_column:

这是我要修剪和转换的查询,但它给了我错误。

SELECT DEVICE_NO,
CASE [Rms_Size]
WHEN
(SELECT RMS_SIZE FROM DW_DATA.DBO.DIM_DEVICE WHERE
Ltrim(Rtrim(Rms_Size)) LIKE '%mm')
THEN
(SELECT SUBSTRING(Rms_Size,1,LEN(rms_size)-2)
FROM DW_DATA.DBO.DIM_DEVICE
where Ltrim(Rtrim(Rms_Size)) LIKE '%mm')
ELSE
RMS_SIZE
END
AS RMS_SIZE
FROM
DW_DATA.DBO.DIM_DEVICE
4

2 回答 2

2

试试这个 -:

SELECT 
   DEVICE_NO,
   CASE 
      WHEN
         LTRIM(RTRIM(Rms_Size)) LIKE '%mm'
      THEN
         SUBSTRING(Rms_Size,1,LEN(rms_size)-2)
      ELSE
         RMS_SIZE
      END
      AS RMS_SIZE
FROM DW_DATA.DBO.DIM_DEVICE
于 2012-04-17T11:38:37.240 回答
0
create table mytable(
mystring varchar(100))

insert into mytable values('100mm')
insert into mytable values('100')

create Function removeChars(@mainString VarChar(100))
Returns VarChar(1000)
AS
BEGIN
    WHILE PatIndex('%[^0-9]%', @mainString) > 0 BEGIN
        Set @mainString = Stuff(@mainString, PatIndex('%[^0-9]%', @mainString), 1, '')
    END

    Return @mainString
END

select * 
from mytable
where dbo.removeChars(mystring) >50
于 2012-04-17T11:54:36.820 回答