0

Im trying to pull bunch of records from MySQL with condition whether the column value contains 0. If I convert to string and check with contains (%0%), it takes more time to execute.. Is there any short way to check on Integer column? Thank you..

8
  • Why do you need such check? Performing a string operation on a integer column is always slower than straight artithmetics. Please explain the problem you are trying to solve. Commented Oct 24, 2020 at 17:08
  • We are writing a query to display the customer_id,customer full name ,city,pincode,and -- order details (order id,order date, product class desc, product desc, subtotal(product_quantity * product_price)) -- for orders shipped to cities whose pin codes do not have any 0s in them. And sort the output on customer name, order date and subtotal. Commented Oct 24, 2020 at 17:11
  • 1
    How many digits is your pin? Commented Oct 24, 2020 at 17:16
  • 1
    Simply add according CHECK constraint over this column into the table structure - and the presence of the value with zero digit in it will be impossible. Recommended check is REGEXP '^[1-9]{6}$'. Commented Oct 24, 2020 at 19:26
  • 2
    In described situation you must take into account that pin must be > 111110. Because, for example, entered 012345 which will be stored as 12354 will match NOT LIKE '%0%' check. Commented Oct 24, 2020 at 19:28

2 Answers 2

0

You can probably run a regex on the integer column. Something like below.

SELECT fieldname FROM tablename WHERE fieldname REGEX '[0]';

About the performance, I believe that LIKE is faster than REGEX. But since your LIKE involves string conversion, this would have some difference in execution time

Sign up to request clarification or add additional context in comments.

2 Comments

REGEXP converts the argument to string too.
It's supposed to be REGEXP and not REGEX. Also it should be REGEXP '^[0]$' if you want the value to be 0 and not just to contain 0
0

@SriniK You can use either POSITION() or LOCATE() in conjunction with CONVERT() to convert the column value into a string and then find if the string "0" exists within the string. Both of these functions return a positive number greater than 0 indicating the index of the substring within your string and they return a 0 if the string isn't found.

Here's how to create a simple test table to illustrate:

CREATE TABLE `testTable` (`id` INTEGER NOT NULL AUTO_INCREMENT, `someNum` INTEGER, PRIMARY KEY(`id`));
                          
INSERT INTO `testTable` (`someNum`)
VALUES
(1),
(10),
(11),
(100),
(111),
(222),
(210);  

And here are two queries illustrating both POSITION() and LOCATE():

SELECT `id`,
    `someNum`,
    POSITION('0' IN CONVERT(`someNum`, CHAR))
FROM `testTable`
WHERE POSITION('0' IN CONVERT(`someNum`, CHAR)) > 0
;
                              
SELECT `id`,
    `someNum`,
    LOCATE('0', CONVERT(`someNum`, CHAR))
FROM `testTable`
WHERE LOCATE('0', CONVERT(`someNum`, CHAR)) > 0
; 
   

I have mocked this up on dbfiddle here so you can get a better look at it.

If this doesn't get you what you need or if you don't understand something please leave a comment with details and I'll do what I can to help further. Good luck.

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.