I want to insert H2O into a column where the "2" should be stored as a subscript in PostgreSQL table (H₂O).
What will be insert query for this?
1 Answer
If your input:
- is always a chemical formula,
- uses mostly (but incorrectly) normal numbers, and
- normal numbers can't occur anyway in them, just subscripts,
then you can use the translate() function upon insertion:
translate(formula, '0123456789', '₀₁₂₃₄₅₆₇₈₉')
http://rextester.com/XUDCXA71407
If you have other characters too, which you want to replace with its counterparts, then you'll need a slightly advanced expression:
regexp_replace(translate(regexp_replace(formula, '<.*?>', '', 'g'), '0123456789-', '₀₁₂₃₄₅₆₇₈₉₋'), 'x(e[urs]|[^e]|$)', 'ₓ\1', 'ig')
http://rextester.com/SCHLWJ33436
Notes:
- the expression above just adds
-andX, however - it assumes
-always needed to be in subscript Xis only needed to be in subscript, where- it does not followed by
e(so it will leaveXeas is) - if it is followed by
e, the string must be continued with eitheru,r, ors(so??ₓEu,??ₓErand??ₓEswill be generated)
- it does not followed by
- as you can see, this depends on the actual periodic table, so I would consider this kind of solution very fragile, but there are no other solutions in SQL for your problem
- also, the unicode subscript set is very limited. you may be able to add a few more pairs to this conversion, but you should consider storing the original formula instead.
Or, if you can guarantee that every subscript x will be in lower case (this way, it cannot collide with Xe), then:
translate(regexp_replace(formula, '<.*?>', '', 'g'), '0123456789-x', '₀₁₂₃₄₅₆₇₈₉₋ₓ')
insert into ... values ('H₂O')