How can I check if a provided value is a number/ integer or letters?
For instance, this is my working SQL,
SELECT
p.*
FROM page AS p
WHERE p.title = 'home'
AND IF(CONVERT('7', SIGNED INTEGER) IS NOT NULL, p.parent_id != p.page_id, p.parent_id = p.page_id)
‘7‘ is variable, sometime it is a number, sometimes it is ‘self‘.
So when it is a number, then p.parent_id != p.page_id
Else p.parent_id = p.page_id
But I can get it right as my SQL query returns always p.parent_id != p.page_id.
You can use a
REGEXP.The reason your
CONVERT()fails to work as expected is that a non-numeric string will always be cast as 0, rather than NULL.Your method may work if you compare against 0 instead of NULL, but I don’t have a good way to test. However, this fails if the value actually is 0, wherein you’ll get the wrong comparison back.