在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语名;
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语名;
解决方案 »
- SQL语句查询速度优化
- materialized view log 中的cache|nocache是啥意思
- 在存储过程中如何创建一个临时表来返回经过计算处理后的记录集?
- 【求助】在plsql中如何界定这样的日期格式(2008-12-23 15:20:50)来统计当天数据???
- 请教一个dblink的问题!在线等待!!!! 高分
- trailing null missing from STR bind value
- 如何对一张表部分字段的值拷贝
- 求存储过程?
- oracle9i如何导入数据表
- LINUX下写一脚本,想定期执行:ORACLE中的删除或者修改!谢谢
- oracle中如何将一个表的数据插入到另一个表中
- 一条语句。。。
SQL> select * from temp where extract(month from sdate) between 2 and 8; ID SDATE
---------- --------------
1 11-5月 -09
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年
这样能insert进oracle吗?需要用to_date转化一下吧,yinto temp values(1,to_date('',''));查询between and就可以
这样是可以插入的
不建议做这样的查询,很有可能全表扫描!
可以的,但是你写的还是不对,应该这样'11-5月 -09',这样是完全按照oracle的date格式写的,可以直接使用
字段定义是DATE型,怎么可能用insert into temp values(3, '11-9月-2009 ') 这种方式插入?
这个是可以的,主要是客户端的NLS_DATE_FORMAT和这个格式一样的,所以可以插进去。
where to_char(sdate,'mm') between 2 and 8
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