Is there an easy way to insert a zero in the 5 place of each date field where men Len is < 8.
The is suppose to be a day field but it’s varchar so some dates only have 7 chars . A data is 8 chars so it’s missing a number
Example 1999230
Should be 19990230
if OBJECT_ID('tempdb..#dstyles') is not null
drop table #dstyles
create table #dstyles(bootleg varchar(50))
insert into #dstyles
select distinct top 200
case
when DATEPART(mm, dateadd(mm,column_id, getdate())) < 10
then cast( year(dateadd(mm,column_id, getdate())) as varchar(4)) +
cast(month(dateadd(mm,column_id, getdate())) as varchar(2)) +
cast(day(dateadd(mm,column_id, getdate())) as varchar(2))
else convert(varchar(8), getdate(), 112) end as yy
from sys.all_columns
select case
when len(bootleg) < 8 then Stuff(bootleg, 5, 0, '0')
else bootleg end ,
bootleg
from #dstyles
it's not the same issue. From what it looks like, the month and day are being treated as integers when combining, so the leading zero is getting cut off of the month or day.
Not really - the OP stated that there are missing zeroes for the month portion of the string, not a missing 0 or 1 for the month. It is also likely that the construction of the string date would be dropping the leading zero for both month and day - which is going to be even harder to reconcile.