0

Is there mysql function to check if value is number only ?

I wan't to CONCAT 2 columns, if tbl3.column2 is number then concat with other table column, if string then same table other column will do.

SELECT tbl1.column1 as Column1, tbl2.column2 as Column2, 
IF tbl3.column2 REGEXP '^[0-9]+$' THEN CONCAT(tbl3.column1, ' ', tbl4.column1) ELSE CONCAT(tbl3.column1, ' ', tbl3.column2) END IF as Combined
FROM table1 tbl1
LEFT JOIN table2 tbl2 ON tbl1.id = tbl2.id
LEFT JOIN table3 tbl3 ON tbl3.id = tbl1.id
LEFT JOIN table4 tbl4 ON tbl4.id = tbl1.id
LEFT JOIN table5 tbl5 ON tbl5.id = tbl1.id 
WHERE
tbl5.column3 = ?
ORDER BY Column1 ASC

Current code gives error:

You have an error in your SQL syntax; check the manual that corresponds
 to your MySQL server version for the right syntax to use near 'tbl3.column2 REGEXP '^[0-9]+$'
    THEN CONCAT(tbl3.column1, ' ', tbl4.column1' at line
2
  • This problem seems symptomatic of poor design Commented Feb 17, 2017 at 8:20
  • yes Just the table names are enough to make that judgment @Strawberry Commented Feb 17, 2017 at 8:21

1 Answer 1

2

try this please:

SELECT tbl1.column1 AS Column1, tbl2.column2 AS Column2, 
IF(tbl3.column2 REGEXP '^[0-9]+$'
    , CONCAT(tbl3.column1, ' ', tbl4.column1)
    , CONCAT(tbl3.column1, ' ', tbl3.column2)
    ) AS Combined
FROM table1 tbl1
LEFT JOIN table2 tbl2 ON tbl1.id = tbl2.id
LEFT JOIN table3 tbl3 ON tbl3.id = tbl1.id
LEFT JOIN table4 tbl4 ON tbl4.id = tbl1.id
LEFT JOIN table5 tbl5 ON tbl5.id = tbl1.id 
WHERE
tbl5.column3 = ?
ORDER BY Column1 ASC
Sign up to request clarification or add additional context in comments.

Comments

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.