----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-11-25 16:32:16
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
-- Dec 28 2012 20:23:12 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go 
create table [huang]([filed] varchar(7))
insert [huang]
select '年龄>20' union all
select '30' union all
select '40' union all
select '50' union all
select 'gg' union all
select 'aa'
--------------开始查询--------------------------select * from [huang] WHERE ISNUMERIC([filed] )>0 AND [filed] >30 AND [filed]<50
----------------结果----------------------------
/* 
[filed]
*/

解决方案 »

  1.   

    你试试这个,看行吗:
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([filed] varchar(7))
    insert [tb]
    select '年龄>20' union all
    select '30' union all
    select '40' union all
    select '50' union all
    select 'gg' union all
    select 'aa'
    select *
    from tb
    where ISNUMERIC(filed) = 1 and filed > 30 and filed < 50
    /*
    filed
    40
    */
      

  2.   

    这个例子,这样也行:if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([filed] varchar(7))
    insert [tb]
    select '年龄>20' union all
    select '30' union all
    select '40' union all
    select '50' union all
    select 'gg' union all
    select 'aa'
    select *
    from tb
    where filed > '30' and filed < '50'
    /*
    filed
    40
    */
      

  3.   

    filed
      年龄>20
       30
       40
       50
       gg
       aa
       4
    用上面的语句,就会把4也检索出来!
      

  4.   


    这个还是可以的,可以过滤4,只显示40:if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([filed] varchar(7))
    insert [tb]
    select '年龄>20' union all
    select '30' union all
    select '40' union all
    select '50' union all
    select 'gg' union all
    select 'aa' union all
    select '4'
    select *
    from tb
    where ISNUMERIC(filed) = 1 and filed > 30 and filed < 50
    /*
    filed
    40
    */
      

  5.   

    我看不出有什么问题----------------------------------------------------------------
    -- Author  :DBA_Huangzj(發糞塗牆)
    -- Date    :2013-11-25 16:32:16
    -- Version:
    --      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
    -- Dec 28 2012 20:23:12 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
    --
    ----------------------------------------------------------------
    --> 测试数据:[huang]
    if object_id('[huang]') is not null drop table [huang]
    go 
    create table [huang]([filed] varchar(7))
    insert [huang]
    select '年龄>20' union all
    select '30' union all
    select '40' union all
    select '50' union all
    select 'gg' union all
    select 'aa'union all
    select '4'
    --------------开始查询--------------------------select * from [huang] WHERE ISNUMERIC([filed] )>0 AND [filed] >30 AND [filed]<50
    ----------------结果----------------------------
    /* 
    filed
    -------
    40
    */
      

  6.   

    select * from [huang] WHERE ISNUMERIC([filed] )>0 AND CAST([filed] AS INT ) >30 AND CAST([filed] AS INT )<50如果不行的话就这样
      

  7.   

    各位亲  我弄错类型了  我说怎么试验都不行呢!
    数据库里的数据类型不是varchar,而是nvarchar,不好意思啊!
      

  8.   


    这样就行哈:if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([filed] nvarchar(7))
    insert [tb]
    select '年龄>20' union all
    select '30' union all
    select '40' union all
    select '50' union all
    select 'gg' union all
    select 'aa' union all
    select '4'
    select [filed]
    from
    (
    select *,
           case when ISNUMERIC(filed) = 1
                     then [filed]
                else null
           end field_t
    from tb
    )t
    where ISNUMERIC(field_t) = 1 and field_t > 30 and field_t < 50
    /*
    filed
    40
    */
      

  9.   

    用:select * from [huang] WHERE ISNUMERIC([filed] )=1
    把数值型的数据抽出来,独立成一个数值型的列,其他的手工处理
      

  10.   

    现在数据库是无法改变了,涉及面太多了,能不能单独在where条件里写个转换处理,因为我是查询的一个视图,用
    select [filed]
    from
    (
    select *,
           case when ISNUMERIC(filed) = 1
                     then [filed]
                else null
           end field_t
    from tb
    )t
    语句,目前我改变不了视图,所以只能想在where条件里做过滤!
      

  11.   


    对了 ,这个语句不行吗:
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([filed] nvarchar(7))
    insert [tb]
    select '年龄>20' union all
    select '30' union all
    select '40' union all
    select '50' union all
    select 'gg' union all
    select 'aa' union all
    select '4'
    select [filed]
    from
    (
    select *,
           case when ISNUMERIC(filed) = 1
                     then [filed]
                else null
           end field_t
    from tb
    )t
    where ISNUMERIC(field_t) = 1 and field_t > 30 and field_t < 50
    /*
    filed
    40
    */
      

  12.   

    我是视图,不能用你的from另外一个表的问题
      

  13.   

    对,楼上的哥们,视图改不了,只能考虑在where条件里过滤!帮忙想想!看怎么弄!
      

  14.   

    不行啊,版主,因为这个视图很多歌地方使用,我不能改,where条件做不了吗?
      

  15.   

    能解决,至于效率就另说了。首先你的字段是字符串类型,它的排序是根据第一个字符,这也就是说为什么有4也能被查询出来。基本思路是这样的了,写个存储过程,先把该字段是数字类型的,也就是楼上都说到了的isnumeric(col)=1的数据放入临时表。
    可以这么写:
    select col1,cast(col2 as int) as col2  into #tb from tb where isnumeric(col2)=1然后再从这个临时表取数据:select * from #tb   where col2.....如果数据量大的话可以考虑给临时表加索引
      

  16.   

    直接在where条件后面过滤  没有办法呗? 因为我们这个项目框架挺麻烦的,临时表这种方式在框架里不好弄,而且现在条件都是拼出来的,很麻烦
      

  17.   


    临时表跟你框架有什么关系?你把数据取出来丢给前台就是了,你直接where过滤不也是要取出数据给前台吗?
      

  18.   

    在where条件里直接过滤没有什么好办法呗?
      

  19.   

    我把我程序里的转换后的SQL语句贴出来:SELECT * FROM 
    (SELECT ROW_NUMBER() 
    OVER (order by CreateTime DESC) AS rows ,* 
    FROM VEdsPartPrefabAndPartHis where isnumeric(filed)=1 ) AS main_temp 
    WHERE rows BETWEEN 1 AND 100其中红色部分是我在后台可以修改的,可以传值到这个分页存储过程里,其他的我无法更改,看看怎么修改,请指教!
      

  20.   

    SELECT * FROM 
    (SELECT ROW_NUMBER() 
    OVER (order by CreateTime DESC) AS rows ,* 
    FROM VEdsPartPrefabAndPartHis where isnumeric(filed)=1 ) AS main_temp 
    WHERE rows BETWEEN 1 AND 100