html - SQL Update Statement for change to correct fractions -
in database, have strings saved this:
<sup>3</sup>⁄<sub>8</sub>
this stores fraction html, want change html statement this:
<div class="fraction"> <div class="numerator">3</div> <div class="denominator">8</div> </div>
i know how change using sql update statement:
update question_table set `option` = replace(`option`, '<sup>3</sup>⁄<sub>8</sub>','<div class="fraction"> <div class="numerator">3</div> <div class="denominator">8</div> </div>') `option` '%<sup>3</sup>⁄<sub>8</sub>%'
but problem 3 , 8 might change accordingly. not 3 , 8. numbers can change. how change correct statement taking account different numbers. know need use regular expressions not sure how.
need guidance on this.
all of comment above wise , valid. however, if need keep way is, can try that:
declare @t table ([id] int, [fraction] nvarchar(max)); insert @t([id], [fraction]) values(0, '<sup>3</sup>⁄<sub>8</sub>'); ; n ( select [id], [numerator] = substring([fraction], charindex('<sup>', [fraction], 0) + len('<sup>'), charindex('</sup>', [fraction], 0) - charindex('<sup>', [fraction], 0) - len('<sup>')) , [denominator] = substring([fraction], charindex('<sub>', [fraction], 0) + len('<sub>'), charindex('</sub>', [fraction], 0) - charindex('<sub>', [fraction], 0) - len('<sub>')) @t ) select [id], [html] = '<div class="fraction"><div class="numerator">'+n.[numerator]+'</div><div class="denominator">'+n.[denominator]+'</div>div>' n;
this code works in smss sql server. replace , adapt model , needs , rdbms. julien
Comments
Post a Comment