mysql - substring of a column from another SQL -


i have following table.

+--------------------+-----+ |fardet_cd_fare_basis|part2|     +--------------------+-----+ |            meo00rig|  00r| |            meo00rig|  rig| |            meo00rig|    i| +--------------------+-----+ 

i need extract "fartdet_cd_fare_basis" first part of chain until appearance of "part2", example be:

+--------------------+-----+--------+ |fardet_cd_fare_basis|part2|     num| +--------------------+-----+--------+ |            meo00rig|  00r|     meo| |            meo00rig|  rig|   meo00| |            meo00rig|    i| meo00ri| +--------------------+-----+--------+ 

i'm working spark sql, used sql solution. ideas?

i think understand want. try this.

livedemo

select fardet_cd_fare_basis,part2,   substr(            fardet_cd_fare_basis,             1,             instr(fardet_cd_fare_basis,part2)-1          ) der_sub your_table 

output

+----------------------+--------+---------+ | fardet_cd_fare_basis | part2  | der_sub | +----------------------+--------+---------+ | meo00rig             | (null) | (null)  | | meo00rig             | o      | me      | | meo00rig             | 00r    | meo     | | meo00rig             | rig    | meo00   | | meo00rig             | 00     | meo     | | meo00rig             |      | meo00r  | | meo00rig             | r      | meo00   | | meo00rig             | 00     | meo     | +----------------------+--------+---------+ 

note: if part2 doesn't exists in first column, not in output. if part2 null null. can handle them separately if want.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -