2

In VB .net I'm wanting to run an sqlite select statement to fetch only numeric values from a text field (a4) which contains both numbers and letters.

My sqlite text a4 field data contains data like this:

  28 days
  1966
  the 100 years war

In would like to be able to fetch

  28
  1966
  100

The sqlite a4 field has a text data type

This is what I have tried

SELECT a4 from data where CAST(a4 AS INTEGER) GLOB '[0-9]'
SELECT a4 FROM data WHERE a4 REGEXP '[0-9]'

I have search for an answer but only found MS SQL answer that uses ISNUMERIC which doesn't exist in SQLITE.

Thanks

1
  • 1
    Maybe in the close but no cigar category: trim(lower(info),"abcdefghijklmnopqrstuvwxyz "). I guess you could include special characters too. But it wouldn't work on 2 plus 2 is 4 for instance. Commented Apr 1, 2019 at 22:03

2 Answers 2

3

You can do it in SQL for SQLite3:

SELECT ROUND(a4) from data WHERE NOT ROUND(a4) = 0;

This is because non-numeric values applied to the ROUND() function result in a value of 0. For example:

sqlite> select round('hello');
round('hello')
--------------
0.0

..whereas numeric values stored as text become numbers:

sqlite> select round('1234');
round('1234')
-------------
1234.0

N.B. There is a limitation to this solution is if a numeric value is also '0' or rounds to 0.0, such as text value '0.1'. It also will give a false positive if the text starts with a number but includes other characters, e.g.

sqlite> select round('1234Hello');
round('1234Hello')
------------------
1234.0
Sign up to request clarification or add additional context in comments.

Comments

1

Not done in the database but in the client code.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim strings() As String = {"28 days", "1966", "the 100 years war"}
    Dim numbers As New List(Of String)
    For Each s In strings
        Dim num As String = ""
        For Each c As Char In s
            If Char.IsNumber(c) Then
                num &= c
            End If
        Next
        numbers.Add(num)
    Next
    For Each num In numbers
        Debug.Print(num)
    Next
    '28
    '1966
    '100
End Sub

1 Comment

Thanks Mary, I might have to revert to this if I cannot achieve something similar using sql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.