SQLite Forum

Efficiently checking if a TEXT column exclusively contains integers or floating point numbers (as strings)
Login
I think I've figured out the floating point equivalent:

```
select
  cast(cast(:value AS REAL) AS TEXT) in (:value, :value || '.0')
```

I tried this first:

```
select
  cast(cast(:value AS REAL) AS TEXT) = :value
```
But that failed on values like `1` because they cast turned them into `1.0`.

Adding that comparison against `:value || '.0'` seems to catch that case nicely.
Demo:

```
select
  value,
  cast(cast(value AS REAL) AS TEXT) in (value, value || '.0') as is_valid_float
from
  (
    select
      '1' as value
    union
    select
      '1.1' as value
    union
    select
      'dog' as value
    union
    select
      null as value
  )
```
<https://latest.datasette.io/fixtures?sql=select%0D%0A++value%2C%0D%0A++cast%28cast%28value+AS+REAL%29+AS+TEXT%29+in+%28value%2C+value+%7C%7C+%27.0%27%29+as+is_valid_float%0D%0Afrom%0D%0A++%28%0D%0A++++select%0D%0A++++++%271%27+as+value%0D%0A++++union%0D%0A++++select%0D%0A++++++%271.1%27+as+value%0D%0A++++union%0D%0A++++select%0D%0A++++++%27dog%27+as+value%0D%0A++++union%0D%0A++++select%0D%0A++++++null+as+value%0D%0A++%29>

Results:

```
value	is_valid_float
  	 
1 	1
1.1 	1
dog 	0
```