Tsql 试图清除查询中不需要的记录

Tsql 试图清除查询中不需要的记录,tsql,Tsql,我有以下疑问 Select * from Common.dbo.Zip4Lookup where zipcode='76033' and StreetName='PO BOX' and '704' between AddressLow and AddressHigh and (OddEven='B' or OddEven = 'E') AddressLow和AddressHigh列是varchar(10)字段 返回的记录是 AddressLow AddressHigh ------

我有以下疑问

Select * from Common.dbo.Zip4Lookup where 
zipcode='76033' and 
StreetName='PO BOX' and 
'704' between AddressLow and AddressHigh and 
(OddEven='B' or OddEven = 'E')
AddressLow和AddressHigh列是varchar(10)字段

返回的记录是

AddressLow   AddressHigh
------------ ------------
1            79
701          711

第二个是所需的记录如何删除第一个记录。

问题在于SQL使用的是字符串比较而不是数字比较。这是因为AddressLow/High是
varchar
而不是
int

只要AddressLow/High包含数字,这应该可以工作:

Select * from Common.dbo.Zip4Lookup where 
   zipcode='76033' and 
   StreetName='PO BOX' and 
   704 between 
       CAST(AddressLow as INT) and 
       CAST(AddressHigh as INT) and 
   (OddEven='B' or OddEven = 'E')

问题是SQL使用的是字符串比较,而不是数字比较。这是因为AddressLow/High是
varchar
而不是
int

只要AddressLow/High包含数字,这应该可以工作:

Select * from Common.dbo.Zip4Lookup where 
   zipcode='76033' and 
   StreetName='PO BOX' and 
   704 between 
       CAST(AddressLow as INT) and 
       CAST(AddressHigh as INT) and 
   (OddEven='B' or OddEven = 'E')

问题是您的条件与
79
开头的
7
中的第一条记录相符,因为它是字符串值。最简单的方法是将数据类型更改为数字类型。

问题是您的条件适合
79
开头的
7
中的第一条记录,因为它是字符串值。最简单的方法是将数据类型更改为数字类型。

什么是“摆脱”?什么是“摆脱”?+1;这是一种方法,但如果您想使用这种条件,我还是宁愿将列重新键入为numeric;这是一种方法,但如果您想使用这种条件,我还是宁愿将列重新键入为numeric。