views:

42

answers:

3

I want to put some same value in some fields where data is not present, for that do I need to query each field and see if there is data present or not and accordingly put the data, or there is some other work arround.

Like

Name    Age    City
N        22      J
K                K
          23     L

Here I want to put data on those fields which don't have data otherwise I don't want to touch tghose fields. After inserting it should look like

Name    Age    City
N        22      J
K        Gar     K
Gar      23      L

There is no validation for the datatype, all are of same datatype. How should I write the query in that case..

+2  A: 
select NVL(name, 'Gar'), NVL(age, 'Gar'), city from your_table;
Juris
I am looking for update not select
Nits
You beat me to it, so I'll just add a comment: If you are interested in *persisting* the default value, go ahead and use `UPDATE SET Name='Gar' WHERE Name IS NULL` ...
kbrimington
@kbrimington how to go for more than one field, I can't use OR not even And, how to do that then...
Nits
The problem with using `or` or `and` is that it will change the rows targeted by the `where` clause. You're `null`'s are not in the same row. An update statement applies its updates to all rows identified in the where clause.
kbrimington
@Nits - No offense, but the language in your question makes it barely understandable. Anyway, if you need to update these columns, I think your best bet is to use separate UPDATE statement for each column, as in kbirmington's comment.
Juris
@Juris soory for the langauge, ya it was not clear due to some typo but now I have refined the question. Also I can't go for querying each field and then inserting, there must be some workaround.... Also your given query doesn't help in upadting the records, it will only fetch the results from table while I want the changes in table.
Nits
A: 

If you just want to do this for all rows in the table you can do something like

UPDATE your_table
  SET name = NVL(name, 'Gar'),
      age = NVL(age, 'Gar');

This assumes that 'age' is a character field.

Share and enjoy.

Bob Jarvis
Just be aware that if you are doing this on a large table, it is updating both fields, and generating redo, for every row in the table. I would add `WHERE name IS NULL OR age IS NULL`.
Dave Costa
@Bob this query is working for me for now...
Nits
A: 

Do you thing this?

create test table...

create table test_table(Name VARCHAR2(10), Age NUMBER(10), City VARCHAR2(10));

insert sample data...

insert into test_table(Name, Age, City) values('N', 22, 'J');
insert into test_table(Name, Age, City) values('K', NULL, 'K');
insert into test_table(Name, Age, City) values(NULL, 23, 'L');
commit;

data look like this...

SQL> select * from test_table;

NAME              AGE CITY
---------- ---------- ----------
N                  22 J
K                     K
                   23 L

and finally programmatically update sample data using CASE statement...

update test_table
  set Name = (CASE WHEN Name IS NULL AND Age >= 23 THEN 'John' ELSE Name END)
     ,Age = (CASE WHEN Age IS NULL AND Name = 'K' THEN 21 ELSE Age END)
;
commit;

3 rows updated

and now data look like this...

SQL> select * from test_table;

NAME              AGE CITY
---------- ---------- ----------
N                  22 J
K                  21 K
John               23 L
Martin Mares