2013-11-14 14 views
6

Qualcuno può dirmi la query per verificare se una stringa è un numero (doppia precisione). Dovrebbe restituire true se la stringa è numero. altrimenti dovrebbe restituire false.Query Postgres per verificare che una stringa sia un numero

considerare:

 s1 character varying; 
     s2 character varying; 

     s1 ='12.41212' => should return true 
     s2 = 'Service' => should return false 

risposta

11

penso che il modo più semplice sarebbe un match espressione regolare:

select '12.41212' ~ '^[0-9\.]+$' 
=> true 

select 'Service' ~ '^[0-9\.]+$' 
=> false 
+0

Come un fascino! Grazie. – Daria

+0

.123.456.789. passerà anche questa regexp –

4

ho fissato l'espressione regolare che a_horse_with_no_name ha suggerito.

SELECT '12.41212' ~ '^\d+(.\d+)?$'; #true 
SELECT 'Service' ~ '^\d+(.\d+)?$'; #false 
Problemi correlati