0

I have an sql field issue_code data type string with the following sample data format "2020-12-0012" with hundreds of records i want to update all from "2020" to "2021" without modifying the rest of the record. how can i query this?

4
  • Are you sure it's "2020-12-0012" with two zeros in last group? Commented Feb 24, 2021 at 14:24
  • Which dbms are you using? When it comes to date/time, many products are far from ANSI SQL compliant. Commented Feb 24, 2021 at 14:50
  • sorry the field is not a date but a unique string. i just need the right query on how to do this on mysql. Commented Feb 24, 2021 at 15:00
  • Found it.. $query = UPDATE table_name SET issue_code = REPLACE(issue_code, '2020', '2021') WHERE issue_code LIKE '%2020%'; Commented Feb 24, 2021 at 15:08

1 Answer 1

1

If your field is DATE, this could be helpful

If your field is text, you could use this Please, check date format

#example for MySQL

UPDATE Test.test_table
SET
    issue_code=DATE_FORMAT(DATE_ADD(STR_TO_DATE(issue_code, '%Y-%m-00%d'), INTERVAL 1 YEAR), '%Y-%m-00%d')
# you may add this condition
# WHERE
#    YEAR(STR_TO_DATE(issue_code, '%Y-%m-00%d')) = '2020'
;

UPD: Be careful with leap years

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

2 Comments

Product specific answer to a question with no dbms specified. At least tell us which dbms this is for.
Found it.. $query = UPDATE table_name SET issue_code = REPLACE(issue_code, '2020', '2021') WHERE issue_code LIKE '%2020%'

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.