SELECT t0.FID ,t0.fbillno ,t0e.FENTRYID ,t0e.FReferPrice ,t0e.FReferAmount ,t0e.fqty ,charindex('.',t0e.FReferPrice) as 'pIndex' ,len(t0e.FReferPrice) as 'pLen' ,charindex('.',t0e.FReferAmount) as 'aIndex' ,len(t0e.FReferAmount) as 'aLen' into #temp FROM T_STK_MISDELIVERY t0 INNER JOIN T_STK_MISDELIVERYENTRY t0e on t0.fid = t0e.fid WHERE t0.FSTOCKDIRECT = 'RETURN' AND t0e.FReferPrice !='' AND t0e.FReferAmount !='' --AND t0.fbillno = 'QTCK074239' SELECT t0.FENTRYID ,t0.FBILLNO ,t0.FReferPrice ,t0.FReferAmount ,t0.fqty ,cast(cast(t0.FReferPrice as decimal(23,2)) AS varchar) as newRPrice ,cast(cast(t0.FReferAmount as decimal(23,2)) AS varchar) as newRAmount INTO #temp1 FROM #temp t0 WHERE ((t0.pindex > 0 AND (t0.plen - t0.pindex) >2) or (t0.aIndex > 0 AND (t0.alen - t0.aIndex) >2) ) --AND fentryid = 675561 --SELECT -- t0.FENTRYID -- ,t0.FReferPrice -- ,t0.FReferAmount -- ,t1.newRPrice -- ,t1.newRAmount UPDATE t0 set t0.FReferPrice = t1.newRPrice,t0.FReferAmount=t1.newRAmount FROM T_STK_MISDELIVERYENTRY t0 INNER JOIN #temp1 t1 on t0.FENTRYID = t1.FENTRYID drop table #temp drop table #temp1