3

I have a table named TICKET. Among the columns in ticket are two date fields:

TICKET
----------------------------------------
TICKDATE             | ELSDATE     | ...
---------------------+-------------+----
yyyy-mm-dd hh:mm:ss    yyyy-mm-dd

These two dates should always be the same.

I have a range of dates (from 7/1 to current) where ELSDATE is null. I'm just starting out in sql scripting, and I don't know how to update one column to equal the other. The current script I'm running to update one day at a time is:

update TICKET ELSDATE="yyyy-mm-dd" where date(TICKDATE)="yyyy-mm-dd"

I assume I'd be able to do something like:

update TICKET where ELSDATE="???" date(TICKDATE)<date("2015-09-29")
2
  • If they should always be the same, why don't you just remove that column? It is taking up space, without giving you any benefit. Commented Sep 29, 2015 at 15:58
  • 1
    @Sumurai8 TICKDATE and ELSDATE are written to from two applications at remote sites. TICKDATE is read by a monitoring application both on-site and remotely. ELSDATE is polled by our business intelligence infrastructure. It's very stupid, but it's well-established stupid which is difficult to change. Commented Sep 29, 2015 at 16:07

3 Answers 3

6

If you want to set every value of ELSDATE to the value of TICKDATE in the same row where TICKDATE is before September 29, 2015, use this:

UPDATE TICKET SET ELSDATE = TICKDATE where TICKDATE < "2015-09-29"
Sign up to request clarification or add additional context in comments.

3 Comments

This cannot use any index on TICKDATE.. I'd suggest TICKDATE < "2015-09-29"
@Arth Can you explain? I'm not sure what that means.
If the column TICKDATE has an index, a query that directly compares it can use the index to find the rows that match the comparison. This is preferable in performance terms to accessing every row to find the rows that match.
3

You can do all updates in a single query so you don't have to do them one day at a time. The query will change each row ELSDATE to equal TICKDATE

UPDATE TICKET SET ELSDATE = TICKDATE;

To only update rows where ELSEDATE is null, you can use.

UPDATE TICKET SET ELSDATE = TICKDATE WHERE ELSDATE IS NULL;

To change rows between 2 dates, use the BETWEEN clause.

UPDATE TICKET SET ELSDATE = TICKDATE WHERE TICKDATE BETWEEN '2015-07-01' AND CURDATE();

6 Comments

Will the last statement include CURDATE()? It's important that ELSDATE is still null on data from today.
@AdamSmith It will. If you dont want to, use this Where-Clause: ...WHERE TICKDATE > '2015-07-01' AND TICKDATE < CURDATE();. This will exclude CURDATE() from the query.
@C4ud3x Thanks for the clarification! In this instance, I can update all the rows older than today so I don't have to worry about that at all. That said, it's great to know what options I have!
Your final update is wrong, the BETWEEN will convert the CURDATE() into a TIMESTAMP/DATETIME for comparison with TICKDATE (effectively appending 00:00:00). You are better of using two conditions with >= and <. I personally never use BETWEEN for date/time comparisons for this reason.
@Arth, I was mistaken. Thanks for pointing out. Editing my post.
|
2

Yep:

UPDATE TICKET 
   SET ELSDATE = TICKDATE 
 WHERE TICKDATE < CURDATE() /* AND ELSDATE IS NULL */

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.