sql - Reformatting data in column -
have kinda weird here. have database that's called fldoc. in has column called sentence contains 7 numbers represent length of time.
example: 0050000 0750000 0000600 0040615 0000110
in 7 digits length of type since digits represent yyymmdd
so i'd script can convert this:
5y 00m 00d 75y 00m 00d 6m (or 000y 6m 00d fine well) 4y 6m 15d etc etc
thanks in advance...
concat
new sql server 2012. if have previous version of sql server, instead achieve desired output:
select sentence ,( case when cast(left(sentence, 3) int) > 0 cast(cast(left(sentence, 3) int) varchar(3)) + 'y ' else cast(left(sentence, 3) varchar(3)) + 'y ' end + case when cast(substring(sentence, 4, 2) int) > 0 cast(cast(substring(sentence, 4, 2) int) varchar(2)) + 'm ' else cast(substring(sentence, 4, 2) varchar(2)) + 'm ' end + case when cast(right(sentence, 2) int) > 0 cast(cast(right(sentence, 2) int) varchar(3)) + 'd' else cast(right(sentence, 2) varchar(3)) + 'd' end ) new_sentence fldoc;
update
to answer question below in comments, maybe write update statement this:
update fldoc set sentence = ( case when cast(left(sentence, 3) int) > 0 cast(cast(left(sentence, 3) int) varchar(3)) + 'y ' else cast(left(sentence, 3) varchar(3)) + 'y ' end + case when cast(substring(sentence, 4, 2) int) > 0 cast(cast(substring(sentence, 4, 2) int) varchar(2)) + 'm ' else cast(substring(sentence, 4, 2) varchar(2)) + 'm ' end + case when cast(right(sentence, 2) int) > 0 cast(cast(right(sentence, 2) int) varchar(3)) + 'd' else cast(right(sentence, 2) varchar(3)) + 'd' end )
Comments
Post a Comment