Reputation: 65
Invoice No
12345/1
789_2
The output i require is as follows
Invoice No
12345
789
Kindly help me to achieve the above result using mysql and laravel 5.2.
Thanks in advance
Upvotes: 1
Views: 257
Reputation: 521914
In MySQL, we can use SUBSTRING_INDEX
to conditionally take the substring of the invoice depending on the presence/absence of separator characters:
SELECT
CASE WHEN invoice_no LIKE '%/%' THEN SUBSTRING_INDEX(invoice_no, '/', 1)
WHEN invoice_no LIKE '%\\\\%' THEN SUBSTRING_INDEX(invoice_no, '\\', 1)
WHEN invoice_no LIKE '%\_%' THEN SUBSTRING_INDEX(invoice_no, '_', 1)
ELSE invoice_no END AS invoice_no_new
FROM yourTable;
Upvotes: 1