Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regexp_matches

Status
Not open for further replies.

spperl

Programmer
Mar 29, 2005
34
0
0
GB
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.
 
Hi

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

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


Feherke.
 
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.
 
OK just figured this out ... 'thinking aloud' seems to help!

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

works well.
 
Hi

Glad to see you figured it out.

The [tt]~[/tt] operator worked for me because I tried it with [tt]integer[/tt] and [tt]double precision[/tt] 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 : [tt]price::text ~ '5$'[/tt]. The formatting done by [tt]to_char()[/tt] is insignificantly slower. ( Some dumb client applications/libraries may be confused by the [tt]::[/tt] notation. In such case the alternative is [tt]cast(price as text)[/tt]. )

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top