Quantcast
Viewing all articles
Browse latest Browse all 4871

Concatenate and Update field based on itself?

We have a goofy piece of software that store dates as a char(10) field in the database.

There was an issue with the software not validating the dates correctly before saving them and now the issue is rearing it's ugly head.

The developers have fixed the software but have left the erroneous data in the database for me to find and repair.

There could be anywhere from 50 to 5000 records that will need fixed.

What the software did was create an 'end date' that was incremented up from a 'start date'

it worked fine for some of the dates (which are really numbers stored in a char(10) field) but other ended up like: 20130732 or 20140132 or 20140431

now, obviously there are not 32 days in any month and April never has 31 days.

What i would like is a query that will look at the data and if the date is greater than 31 increment the month and make the day 01. so 20130732 would become 20130801 and 20140132 would become 20140201 and 20140431 would become 20140501

However, it being the friday after a holiday my brain is just not cooperating, so here i am. :D

In my efforts, i have this, which gives me a set of records in a table i need to change:

select * from TABLE1 where RIGHT(DATETO,2)>31

and this which gives me the wrong value as a date....i knew it was wrong writing it, but as i said above, my brain is highly uncooperative today. ;-)

Update TABLE1
Set DATETO = (LEFT(DATETO,4)+(SUBSTRING(DATETO,5,2)+1)+('01')) where RIGHT(DATETO,2)>31

i know am going to get a solution, followed closely by a facepalm!


Viewing all articles
Browse latest Browse all 4871


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>