SQL Trim after final semi colon

岁酱吖の 提交于 2019-12-11 11:54:48

问题


The following query produces the examples below. I want to pull only what is after the last (always the 3rd) semi colon. Each output will have different cities so I can't just trim it a certain amount of spaces as it will vary.

SELECT  MAX(E.EVENT_DESC) 
FROM    IASDB.EVENT E 
WHERE   SL.INVOICE_NO = E.INVOICE_NO 
AND     E.EVENT_CODE IN 'EDL'

Examples:

Consignee;Jeffersonville,IN;J 6

Consignee;Nashville,TN;J 14

What do I need to do to only pull the characters that come after the last semi colon? (ex: 'J 6' or 'J 14')


回答1:


This should work in DB2:

SELECT  MAX(RIGHT(
         E.EVENT_DESC
        ,LENGTH(E.EVENT_DESC) - LOCATE_IN_STRING(E.EVENT_DESC,';',-1)))
FROM    IASDB.EVENT E 
WHERE   SL.INVOICE_NO = E.INVOICE_NO 
AND     E.EVENT_CODE IN 'EDL'


来源:https://stackoverflow.com/questions/34684520/sql-trim-after-final-semi-colon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!