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.
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
Post a Comment