1
INSERT INTO books(book_id, title, author_last_name, author_first_name, classify)
VALUES (1000, 'The Picture of Dorian Gray', 'Wilde', 'Oscar', 'fiction');
INSERT INTO books(book_id, title, author_last_name, author_first_name, classify)
VALUES (1001, 'Think Big', 'Carson', 'Dr. Ben', 'non - fiction');
INSERT INTO books(book_id, title, author_last_name, author_first_name, classify)
VALUES (1003, 'Mathematical Scandals', 'Pappas', ' ', 'non - fiction');
INSERT INTO books(book_id, title, author_last_name, author_first_name, classify)
VALUES (1004, 'SQL', 'Harris', 'Andy', 'non - fiction');
INSERT INTO books(book_id, title, author_last_name, author_first_name, classify)
VALUES (1010, 'Excel 2016', 'Chan', ' ', 'non - fiction');
commit;

here is the value of my table. in some records i have inserted empty string ''. how to replace that empty string with NULL values using nvl function

7
  • Do you mean "replace null with empty strings?" Commented May 6, 2016 at 18:18
  • Replace empty strings (which are the same as null in Oracle), or values which are just whitespace? Commented May 6, 2016 at 18:19
  • @AlexPoole empty strings are not the same as null. Commented May 6, 2016 at 18:19
  • @JosephStyons - they are the same in Oracle (varchar and varchar2 etc.). Currently, anyway *8-) Commented May 6, 2016 at 18:21
  • @JosephStyons In Oracle, yes they are. Commented May 6, 2016 at 18:21

2 Answers 2

1

This will replace the single whitespace strings with a NULL value:

UPDATE books
SET    author_first_name = NULL
WHERE  author_first_name = ' ';

Or, this will trim all the whitespace from the start and end of the names (and if an empty string is left then that is the same as a NULL value)

UPDATE books
SET    author_first_name = TRIM( author_first_name );

The NVL function is used to conditionally replace NULL values with another value - not the other way round so it is inappropriate to use it for this purpose.

Sign up to request clarification or add additional context in comments.

Comments

1

The NVL function is used to replace NULL with some other value, not the reverse.

If you had actually entered an empty string, your question would be irrelevant. In Oracle, there is no distinction between NULL and the empty string ''.

But it appears you've entered strings containing a single space, which is not the same as an empty string.

A simple update to replace these strings with NULL would be:

UPDATE books SET
  author_first_name = NULL
WHERE
  author_first_name = ' '

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.