查询的是序列号  比较长 不能用长整型表示用文本表示,最长20个数字
要求查询2个字符串中间的值如
SN
2009080500000001
2009080500000003
2009080500000005
2009080500000014
2009080500000025
2009080500000036
2009080500000047查询>=2009080500000004  <=2009080500000025之间的值结果为
2009080500000005
2009080500000014
2009080500000025

解决方案 »

  1.   

    直接用字条串就可以比较了。只要保证都是数字,和数字的比较是一样的。 select * from tb where sn>='2009080500000004'  sn<='2009080500000025'
      

  2.   

    select * from tb where SN>='2009080500000004'  AND SN<='2009080500000025'
      

  3.   

    select * from tb 
    where SN>='2009080500000004'  AND SN<='2009080500000025'
      

  4.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([SN] text)
    insert [tb]
    select '2009080500000001' union all
    select '2009080500000003' union all
    select '2009080500000005' union all
    select '2009080500000014' union all
    select '2009080500000025' union all
    select '2009080500000036' union all
    select '2009080500000047'
     
    ---查询---
    select 
      * 
    from 
      [tb] 
    where 
      cast(SN AS VARCHAR(20)) between '2009080500000004'  and '2009080500000025'---结果---
    SN                                  
    ----------------------------------
    2009080500000005
    2009080500000014
    2009080500000025(所影响的行数为 3 行)
      

  5.   

    -- =========================================
    -- -----------t_mac 小编-------------------
       --------------------希望有天成为大虾---- 
    -- =========================================IF OBJECT_ID('tb') IS NOT NULL
    DROP TABLE tb
    GO
    CREATE TABLE tb( a varchar(20))
    go
    insert tb SELECT 
    '2009080500000001' UNION ALL SELECT 
    '2009080500000003' UNION ALL SELECT 
    '2009080500000005' UNION ALL SELECT 
    '2009080500000014' UNION ALL SELECT 
    '2009080500000025' UNION ALL SELECT 
    '2009080500000036' UNION ALL SELECT 
    '2009080500000047' 
    go
    select * 
    from tb
    where a between 2009080500000004  and 2009080500000025 go
    /*
    a
    --------------------
    2009080500000005
    2009080500000014
    2009080500000025*/
      

  6.   

    楼主所说的大数值,就示例数据来讲,好象是日期+序列号的形式,其实算不上大数值的。也可以比较后几位的数值位了。
    如,
    right(sn,8) 得到的部分就可以用int来表示了。
    即使int不行,还有bigint,呵呵。
      

  7.   

    DECLARE @A TABLE(sn ntext)
    insert @a select
    '2009080500000001' union all select 
    '2009080500000003' union all select 
    '2009080500000005' union all select 
    '2009080500000014' union all select 
    '2009080500000025' union all select 
    '2009080500000036' union all select 
    '2009080500000047'select * from @a where cast(sn as varchar(20)) between '2009080500000004' and '2009080500000025' sn
    ----------------
    2009080500000005
    2009080500000014
    2009080500000025
      

  8.   

    select * from tablename where sn>='2009080500000004' and sn<='2009080500000025'
      

  9.   

    select * from tb where SN>='2009080500000004' AND SN<='2009080500000025'sql在比较的时候会当成int来看大小的
      

  10.   

    如果数据长度不是一样长,建议格式化一下字符串,不足长度则补齐到最大20字节:
    select 
        * 
    from 
        tb 
    where 
        right('00000000000000000000'+SN,20)>=right('0000'+'2009080500000004',20)  
        AND 
        right('00000000000000000000'+SN,20)<=right('0000'+'2009080500000025',20) 
      

  11.   

    我是在VC中使用的   值1,值2 通过控件传进来的
    用于水晶报表的查询between 值1  and 值2
      

  12.   

    between and 就应该可以了
      

  13.   

    如果sn不是定长,建议处理成定长,然后再用Sql
      

  14.   

    SN between '2009080500000004'  and '2009080500000025'
      

  15.   

    都可以的if object_id('[tb]') is not null drop table [tb] --字符符
    go
    create table [tb]([SN] text)
    insert [tb]
    select '2009080500000001' union all
    select '2009080500000003' union all
    select '2009080500000005' union all
    select '2009080500000014' union all
    select '2009080500000025' union all
    select '2009080500000036' union all
    select '2009080500000047'
    select * from tb where CAST(sn as nvarchar(30)) between '2009080500000005' and '2009080500000025'if object_id('[tb]') is not null drop table [tb]  --长整型
    go
    create table [tb]([SN] bigint)
    insert [tb]
    select '2009080500000001' union all
    select '2009080500000003' union all
    select '2009080500000005' union all
    select '2009080500000014' union all
    select '2009080500000025' union all
    select '2009080500000036' union all
    select '2009080500000047'
    select * from tb where sn between 2009080500000005 and 2009080500000025