在Oracle中假如有一个临时表temp ,结构如下:
Create table temp (
 id number,
 sdate date
);插入几条数据为:
insert into temp values(1,'11-5月-2009');   
insert into temp values(2,'2-1月-2009'); 
insert into temp values(3,'11-9月-2009') ;  现要求查询月份是2 --- 8 月份的数据,该怎样写SQL语名;

解决方案 »

  1.   

    需要看年份么,如果只看月份的话可以select * from temp where extract(month from sdate) between 2 and 8;
      

  2.   


    SQL> select * from temp where extract(month from sdate) between 2 and 8;        ID SDATE
    ---------- --------------
             1 11-5月 -09
      

  3.   

    我不知道Oracle数据库和SQL有什么区别,不过看你上面的代码应该区别不大,我用sql写了你可以参考下
    Oracle 中的数据库中的日期类型可以写成'11-9月-2009 '这样的吗?
      下面的代码中的日期我用varchar代替了没有用date类型--sql中创建表
    Create   table   temp   ( 
      id int identity(1,1) primary key not null,
       number varchar(20), 
      sdate  varchar(10),
      date varchar(20) 
    ); 插入几条数据为: 
    insert   into   temp   values(1, '','5月-11-2009 ');       
    insert   into   temp   values(2,'', '1月-2-2009 ');   
    insert   into   temp   values(3, '','9月-11-2009 ')   ; 
    insert   into   temp   values(3, '','8月-11-2009 ')   ;     
        现要求查询月份是2   ---   8   月份的数据,该怎样写SQL语名; 1.建个视图,去掉日期中的"月"字,
    create view view_aaa
    as
    select id,number,sdate,convert(varchar(20),replace(date,'月',''))as date from temp
    select * from view_aaa2.查询视图,把日期中的"_"去掉
    select * from view_aaa where convert(int,replace(date,'-',''))>=2012009 and convert(int,replace(date,'-',''))<=8312009上面语句中的2012009 代表 2月01日2009年  8012009 代表8月31日2009年
      

  4.   

    要是数据库的日期是date型的就简单多了...
      

  5.   

    insert   into   temp   values(1, '11-5月-2009 ');       
    这样能insert进oracle吗?需要用to_date转化一下吧,yinto temp values(1,to_date('',''));查询between and就可以
      

  6.   

    insert  into  temp  values(1, '11-5月-2009 ');   
    这样是可以插入的
      

  7.   

    select * from temp where to_char(temp.sdate,'mm') between 2 and 8;
    不建议做这样的查询,很有可能全表扫描!
      

  8.   

    select * from temp where to_char(sdate,'mm') between '02' and '08'
      

  9.   


    可以的,但是你写的还是不对,应该这样'11-5月 -09',这样是完全按照oracle的date格式写的,可以直接使用
      

  10.   

    trunc(date_field, 'MM')楼主可以 google 一下 trunc(date, fmt) 的用法
      

  11.   

    楼主写的插入语句不对吧
    字段定义是DATE型,怎么可能用insert   into   temp   values(3, '11-9月-2009 ') 这种方式插入?
      

  12.   


    这个是可以的,主要是客户端的NLS_DATE_FORMAT和这个格式一样的,所以可以插进去。
      

  13.   

    select * from temp
    where to_char(sdate,'mm') between 2 and 8
      

  14.   

    SQL> Create   table   temp   ( 
      2    id   number, 
      3    sdate   date 
      4  ); 表已创建。SQL> insert   into   temp   values(1, '11-5月-2009 ');       已创建 1 行。SQL> insert   into   temp   values(2, '2-1月-2009 ');   已创建 1 行。SQL> insert   into   temp   values(3, '11-9月-2009 ')   ;     已创建 1 行。SQL> commit;提交完成。SQL> select * from temp;        ID SDATE
    ---------- --------------
             1 11-5月 -09
             2 02-1月 -09
             3 11-9月 -09SQL> select * from temp
      2  where sdate between '01-2月-09' and '31-8月-09';        ID SDATE
    ---------- --------------
             1 11-5月 -09
      

  15.   

    试了一下,的确是可以insert的
      

  16.   

    select * from temp where to_char(temp.sdate,'mm') between 2 and 8;