4

Table A:

Id  Name  DateCreated  DateModified
-----------------------------------
1    A     2013-1-12    2013-1-15
2    B     NULL         2013-2-1
3    C     NULL          NULL

I have to migrate this table's data to another table in which DateCreated is a not nullable column, and the conditions are if DateCreated is null use DateModified and if both are null use current date.

I can't use

ISNull(DateCreated,DateModified)

because both can be null.

How do I do it? The table has around 10000 rows.

3 Answers 3

8

You can use COALESCE(DateCreated, DateModified, GETDATE())

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

Comments

3

Nested Isnull

Isnull(DateCreated,isnull(DateModified, getdate()))

Or use a CASE

case when DateCreated is null and DateModified is null then getdate()
     when DateCreated is null then DateModified 
     else DateCreated end

2 Comments

colesce has some precedence problem, thats what I knew, but in this case since the data types are same both will work. Marking this as correct answer, because it was answered first. Both are actually correct, and so an upvote for both.
-2

is is simething like this you are looking for?

$result = mysql_query($query, $link);

if ($result) {
  while($row = mysql_fetch_array($result)) {

    if ($row['DateCreated'] == 'NULL'){

        if ($row['DateModified'] == 'NULL'){

                $today = date("Y-m-d");
                $id = $row['id'];
                $query = sprintf("SET DateCreated = $today WHERE id = $id");

            } else {
                $newDate = $row['DateModified'];
                $id = $row['id'];
                $query = sprintf("SET DateCreated = $newDate WHERE id = $id");}
        }
  }

}
else {
  echo mysql_error();
}

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.