9

I have a table that has a string value of 'null' that I wish to replace with an actual NULL value.

However if I try to do the following in my select

Select Replace(Mark,'null',NULL) from tblname

It replaces all rows and not just the rows with the string. If I change it to

Select Replace(Mark,'null',0) from tblname

It does what I would expect and only change the ones with string 'null'

3
  • Always specify (tag) the actual database you are using when you ask SQL questions. Commented Jun 15, 2016 at 18:00
  • @GordonLinoff What?, no, that's not right, op wants the opposite of COALESCE Commented Jun 15, 2016 at 18:37
  • 1
    +1 for showing me something I had overlooked in the doco: "REPLACE returns NULL if any one of the arguments is NULL." It doesn't seem very logical when some values of Mark may not contain substring 'null'. Commented Jun 15, 2016 at 18:39

1 Answer 1

13

You can use NULLIF:

SELECT NULLIF(Mark,'null') 
FROM tblname;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.