针对数据库a的字符串类型的字段tj,如何这样查询?
该字段现在存储如下数据
id,tj
1   5
2   5c
3   阿
4   100
5   哈
======================================
查询tj所存数据中大于3小于100的数据,非数字内容都作为999看待。

解决方案 »

  1.   

    --tryselect * from tablename where case when isnumeric(tj)=0 then 999 else cast(tj as int) end between 3 and 100
      

  2.   

    Select * From A Where IsNumeric(tj)=1 And tj>3 And tj<100
      

  3.   

    既然非數字的當做999,而999肯定不在“大于3小于100”這個區間內,所以不用考慮。另外是“大于3小于100”,所以用Between的時候,要調整一下區間范圍了。Create Table A(ID Int,tj Varchar(10))
    Insert A Select 1,'5'
    Union All Select 2,'5c'
    Union All Select 3,'阿'
    Union All Select 4,'100'
    Union All Select 5,'哈'
    GO
    Select * From A Where IsNumeric(tj)=1 And tj>3 And tj<100
    GO
    Drop Table A
    /*
    ID tj
    1 5
    */
      

  4.   

    select * from 表 where id not in(select id from 表 where tj>3 and tj<100)
      

  5.   

    yooono(yes or no) ( ) 信誉:100  2006-07-17 08:55:00  得分: 0  
     
     
       大于3小于100并不是固定的,有时候会改变,还有这个值也是变化的,如果不变你怎么不直接输出5那条呢?
      
     
    -----------------------
    你看看我那條語句,就可以看到我不是沒有考慮非數字的語句的。
    只要你的區間不會到999這裡時,你就可以直接用IsNumeric(tj)=1將所有的非數字的過濾掉。
      

  6.   

    當然,如果區間有可能會到999這裡,用個case when判斷就更好,否則就沒有必要那麼麻煩加case when了,這就看你怎麼取捨了。PS:不是學師范的,:)。
      

  7.   

    借鱼的表Create Table A(ID Int,tj Varchar(10))
    Insert A Select 1,'5'
    Union All Select 2,'5c'
    Union All Select 3,'阿'
    Union All Select 4,'100'
    Union All Select 5,'哈'
    GO
    Select * From A Where IsNumeric(tj)=1 And tj>3 And tj<100
    SELECT * FROM A WHERE ISNULL(STUFF(RTRIM(NULLIF(ISNUMERIC(tj),0)) + tj,1,1,''),999) BETWEEN 4 AND 99
    GO
    Drop Table A--这段可以将你非数字的值用任意指定的数值来代替ISNULL(STUFF(RTRIM(NULLIF(ISNUMERIC(tj),0)) + tj,1,1,''),999)
      

  8.   

    fcuandy(边打魔兽边回贴),用這麼多函數來處理還不如直接用case when來判斷。
      

  9.   

    这样写的语句样子好看啊,case when太难看,怎么看也不像是一句语完成的.haha
      

  10.   

    你可以建个100w的表试下它与CASE WHEN的效率区别.