1

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?

6
  • Can you clarify what you mean by "stored as subscript", what is H2O, what your table looks like, etc... Commented Jun 9, 2017 at 10:34
  • 2
    insert into ... values ('H₂O') Commented Jun 9, 2017 at 10:42
  • @a_horse_with_no_name I am getting H<sub>2</sub>O as an output. Commented Jun 9, 2017 at 10:42
  • @eurotrash : H2O is chemical formula of water. Commented Jun 9, 2017 at 10:43
  • @melpomene : I have a data in XML as H2O. I have to convert it into H₂O and store it in posgresql table. Commented Jun 9, 2017 at 10:45

1 Answer 1

1

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 - and X, however
  • it assumes - always needed to be in subscript
  • X is only needed to be in subscript, where
    • it does not followed by e (so it will leave Xe as is)
    • if it is followed by e, the string must be continued with either u, r, or s (so ??ₓEu, ??ₓEr and ??ₓEs will be generated)
  • 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', '₀₁₂₃₄₅₆₇₈₉₋ₓ')

http://rextester.com/WPRDO99076

Sign up to request clarification or add additional context in comments.

2 Comments

I have XML structure like <mod>Sr<sub>1-x</sub> Ba<sub>x</sub>Nb <sub>2</sub>O <sub>6</sub> </mod> I have to store it as Sr(1-x)Ba(x)Nb(2)O(6) in table. character inside () should be store as subscript.
Thanks a lot. This is very useful solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.