×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

regexp_matches

regexp_matches

regexp_matches

(OP)
Hi,

Is there anyway I can write a query to return results only when the value of a numeric type ends in 5?

CODE

Pseudo code:

select * from items where price ~ '5$';

Is there a way to use regexp_matches or does price need to be cast as a string or text?

Any help appreciated.

RE: regexp_matches

Hi

What you mean by "Pseudo code" ? That query works for me as is.

If the operand is not text, a cast will be automatically performed. However, if your price is double precision, you may prefer to format it yourself, or at least to round() or trunc() to the preferred precision, to avoid 0.5 vs. 0.4999999999 differences.
 

Feherke.
http://free.rootshell.be/~feherke/

RE: regexp_matches

(OP)
Hi,

Thanks for the fast reply, you are correct my pseudo code ended up just being the query that I'm running!

Whilst the query may work for char columns I'm having difficulty running the above on a numeric(7,2) field.

I receive the following:

HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Any ideas on how to implement as my cast attempts didn't work.

Thank you again.

RE: regexp_matches

(OP)
OK just figured this out ... 'thinking aloud' seems to help!

CODE

select * from items where to_char(price,'99999.99') ~ '5$';

works well.

RE: regexp_matches

Hi

Glad to see you figured it out.

The ~ operator worked for me because I tried it with integer and double precision on PostgreSQL 7.3. Now tried it on PostgreSQL 8.4 too, and there not works anymore. Strange.

Anyway, the alternative would be to just cast : price::text ~ '5$'. The formatting done by to_char() is insignificantly slower. ( Some dumb client applications/libraries may be confused by the :: notation. In such case the alternative is cast(price as text). )

Feherke.
http://free.rootshell.be/~feherke/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close