Search using between no postrgres

Asked

Viewed 30 times

0

I am performing a database search in order to return only the characteristics that are between the range of 40 and 100. But when running the script below it is returning me other features that are not related to the searched. Apparently this is happening because the values 6.7 and 8.7 are being considered as 67 and 87 thus staying between the range of 40 and 100.

      SELECT carlis_nome 
        FROM caracteristica
           JOIN caracteristica_lista 
             ON caracteristica_lista.caracteristica_id = caracteristica.caracteristica_id
        WHERE caracteristica.caracteristica_id = 11
            AND CASE 
                WHEN caracteristica_lista.carlis_nome ~ '^[0-9]'
                    THEN TO_NUMBER(caracteristica_lista.carlis_nome, '999999999')
                ELSE 0
                END BETWEEN 40
                AND 100

By running this SQL you are returning these records to me:

Retorno da busca

1 answer

1


You are converting the values to an integer so that postgres is ignoring the separator. Change the function TO_NUMBER for:

TO_NUMBER(caracteristica_lista.carlis_nome, '9.99999999')

And you must get the expected result.

  • returned this: ERROR: field bursting Numeric DETAIL: A field with accuracy 1, scale 0 must round to an absolute value less than 10 1.

  • 1

    I forgot a detail. What is the maximum amount of digits you will have in the entire part of the number? This will be the amount of 9s you will get in the entire string. For example: 1800 Megapixels, 4 nines => '9999.9999999'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.