Oracle复杂查询
1、查询两个日期之间的数据。
假设有表Table1,其创建表的sql语句为:
create table Table1(
StationID NUMBER(10) Primary key,
Year NUMBER(4) not null,
Month NUMBER(2) not null,
Day NUMBER(2) not null);
现在我需要查在'2005-01-01'至'2006-01-01'之间的所有数据,遇到的主要问题是:表Table1中的日期信息分别存放在三个字段(Year,Month,Day)中,怎样实现这一查询呢?
答案如下:
SQL>select * from Table1 where to_date(concat(concat(concat(concat(year,'-'),lpad(month,2,0)),'-'),lpad(DAY,2,0)),'yyyy-mm-dd')
between to_date('2005-01-01','yyyy-mm-dd') and to_date('2006-01-01','yyyy-mm-dd');
答案解释:
(1)使用concat函数连接Year,Month,Day三个字段,从而构造一个新的字符串(包含年、月、日信息),使其格式为:'年-月-日';
(2)使用lpad函数补足月份及天数,使之都是二位,这是因为to_date函数的第一个参数是必须为'yyyy-mm-dd'类型;
(3)使用between and来确定范围。
相关文档:
1,查看当前的保护模式
select DATABASE_ROLE,PROTECTION_MODE,PROTECTION_LEVEL from v$database;
2,查看日志的传送方式;
select dest_name,archiver from v$archive_dest;
3,停止standby的自动恢复状态
alter database recover manager standby database finish;
4,添加standby logfile
&nb ......
首先查看数据库现有模式可使用以下语句
select name,log_mode from v$database;
也可以用下面的语句
archive log list;(该方法需要as sysdba)
对于非归档模式的数据库该为归档模式(主要以Oracle 10g为参考)使用以下步骤:
1. SQL> alter system set log_archive_dest_1='location=/oracle/oracle10g/log/archive ......
输入参数:str ——要截取的字符串, ch——要查找的字符串
截取ch之前(不包括ch)的字符串: substr(str, 0, instr(str, ch) - 1)
截取ch之后(不包括ch)的字符串: substr(str, , instr(str, ch) + 1, length(str)) ......
修改oracle 10g的字符集
修改数据库字符集为:ZHS16GBK
首先用scott&tiger&orcl登录到sql/plus
查看服务器端字符集
SQL > select * from
V$NLS_PARAMETERS;
修改:
$sqlplus /nolog
SQL>conn / as sysdba
若
此时数据库服务器已启动,则先执行 SHUTDOWN IMMEDIATE 命
令关闭数据库服务器,然后执 ......