简体   繁体   English

oracle9i查询

[英]oracle9i queries

I am having a oracle table consisting of 6 columns,out of which one is date column. 我有一个由6列组成的oracle表,其中之一是date列。 The date field has null entries in some rows...no other field is null.. Can I delete the rows having date field as null? 日期字段在某些行中具有空条目...其他字段均不为空。我可以删除具有日期字段为空的行吗? or should I update some value in the date field for those rows...? 还是应该在这些行的日期字段中更新某些值?

Please provide me the correct way to do both these operations? 请提供正确的方法来执行这两项操作吗?

thanks.... 谢谢....

If you need to retain the other values in the row you can provide a default value for the date in a query with; 如果您需要在行中保留其他值,则可以使用以下命令为查询中的日期提供默认值:

SELECT NVL(dateCol, to_date('31/01/2009','dd/MM/yyyy')) FROM dataTable

To update the null values in the table use; 要更新表中的空值,请使用;

UPDATE dataTable
SET dateCol = to_date('31/01/2009','dd/MM/yyyy') 
WHERE dateCol IS NULL

To remove the null rows; 删除空行;

DELETE FROM dataTable
WHERE dateCol IS NULL

从table_name中删除,其中date_field为空;

Correct answers have already been given. 已经给出正确答案。 I'm only adding a bit of explanation here. 我在这里只添加一些解释。 Comparing using the equality operator (=) and NULL always gives NULL, never TRUE. 使用相等运算符(=)和NULL进行比较总是得出NULL,从不得出TRUE。 That's why you have to use "IS NULL" instead of "= NULL". 这就是为什么您必须使用“ IS NULL”而不是“ = NULL”的原因。 Here is the part of the documentation that discusses NULLS: http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/sql_elements005.htm#i59110 这是讨论NULL的文档部分: http : //download.oracle.com/docs/cd/B28359_01/server.111/b28286/sql_elements005.htm#i59110

An example: 一个例子:

SQL> create table mytable (col1,col2,col3,col4,col5,datecol)
  2  as
  3  select 1,1,1,1,1,sysdate from dual union all
  4  select 2,2,2,2,2,null from dual union all
  5  select 3,3,3,3,3,null from dual
  6  /

Tabel is aangemaakt.

SQL> delete mytable
  2   where datecol = null
  3  /

0 rijen zijn verwijderd.

SQL> select * from mytable
  2  /

 COL1  COL2  COL3  COL4  COL5 DATECOL
----- ----- ----- ----- ----- -------------------
    1     1     1     1     1 03-05-2009 11:36:08
    2     2     2     2     2
    3     3     3     3     3

3 rijen zijn geselecteerd.

SQL> delete mytable
  2   where datecol is null
  3  /

2 rijen zijn verwijderd.

SQL> select * from mytable
  2  /

 COL1  COL2  COL3  COL4  COL5 DATECOL
----- ----- ----- ----- ----- -------------------
    1     1     1     1     1 03-05-2009 11:36:08

1 rij is geselecteerd.

Regards, Rob. 问候,罗布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM