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
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
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
Related
I calculate data directly in a userform and I transfer my data into a table afterwards. In my userform the numbers are good, but not in the table. I would like that my data (Double) appear with a comma in my table. For now, my data are in the table without comma. For example, my data is 441 666 666 666 667 instead of 4,41666666666667 ... I think that I have to change the format? Can anyone enlighten me on this subject please? Thank you.
Code example:
Private Sub tbxHours_Change()
Dim rate As String
Dim hours As String
rate = Replace(tbxRate .Value, ".", ",")
hours = Replace(tbxHours .Value, ".", ",")
If StrComp(rate , "") = 0 Then rate = "0"
If StrComp(hours , "") = 0 Then hours = "0"
tbxCostRate.Value = CStr(CDbl(rate) * CDbl(hours))
End sub
I'm not following your code completely, but this is the format you want to apply to your values.
.NumberFormat = "#,##0"
You are using the comma as the decimal delimiter, correct? If so, make sure that the settings are correct in excel. See Microsoft Change Character for Delimiter
This can be done this with the VBA Format() function:
'Comma as the Decimal Delimiter
tbxCostRate.Value = Format(CDbl(rate) * CDbl(hours), "#.##0,00")
'Comma as the Thousands delimiter
tbxCostRate.Value = Format(CDbl(rate) * CDbl(hours), "#,##0.00")
*Note: the rate probably shouldn't be converted to a string if the data is numeric
Also:
tbxCostRate.Value = Format CStr(CDbl(rate) * CDbl(hours)), "Standard")
I would like my function to follow some convention to check if my ticket number needs formatting or not.
If the convention is not met, then I would like to make some changes to the ticket number.
Ticket number 19K3072216 needs to be formatted to this 19-K3-07-002216 because it does not meet the following conditions.
My function should do the following.
Check if the 1st 2 digits has a value 0 - 9 (numeric)
Check if the 3rd digit has a value of A to Z
Check if the 4th digit has a value 0 - 9 (numeric)
Check if the 5th and 6th digits has a date value (e.g.2 digit year - 17, 90, 15 etc)
Check if the next 6 digits i.e. 7th - 12th digits are numeric.
Because ticket number 19K3072216 does not meet the above conditions, I would like my function to format it to look like this 19-K3-07-002216
The string strTicketNumber should return formatted ticket number 19-K3-07-002216
My vb.net function
Public Class Ticket_Code
Public Shared Sub main()
Dim strTicketNumber As String = FixTicketNumber("19K3072216")
End Sub
Public Shared Function FixCaseNumber(ByVal astrCaseNumber As String) As String
Dim strCaseNumber As String = Replace(astrCaseNumber, "-", "")
'Determine if ticket number is formatted
How do I do this?
'If ticket number is formatted add 2 zeros
'How do I do this?
'Else return unchanged
'If ticket number is already formatted, just returned the number (original number)
Return strCaseNumber
End Function
End Class
It will really depend on your input and how different from the example it can be.
For instance will invalid input always be in the same format 19K3072216or is there the chance it will be all digits, all letters, less/more than 10 characters long etc. All of these rules need to be considered and handled as necessary.
If the input will be from a user, never trust it and always assume it is the furthest from valid as possible. If the app can handle that case it can handle everything else
Something like this should get you started:
Public Sub Main()
Dim strTicketNumber As String = FixTicketNumber("19-K3-07-002216") ' or 19K3072216
Console.WriteLine(strTicketNumber)
Console.ReadKey()
End Sub
Private Function FixTicketNumber(p1 As String) As String
Dim fixed As String = ''
Dim valid As Boolean = checkTicketNumber(p1)
If valid Then
Return p1 ' Ticket number is valid, no transformation needed
Else
'Assume invalid input will always be 10 characters (e.g. 19K3072216)
'Split the input and Step through each rule one at a time
'returning the necessary result/format string as you go
'#1 Check if the 1st 2 digits has a value 0 - 9 (numeric)
Dim ruleOne As String = p1.Substring(0, 2)
'perform isNumeric, concatenate to fixed if everything is ok
'fixed += ruleOne+"-"
'#2 Check if the 3rd digit has a value of A to Z
Dim ruleTwo As String = p1.Substring(3, 1)
'check if its a letter, concatenate to fixed if everything is ok
'... same for all the rules
End If
End Function
Private Function checkTicketNumber(p1 As String) As Boolean
'See if the input matches the rules
'Check if the 1st 2 digits has a value 0 - 9 (numeric)
'Check if the 3rd digit has a value of A to Z
'Check if the 4th digit has a value 0 - 9 (numeric)
'Check if the 5th and 6th digits has a date value (e.g.2 digit year - 17, 90, 15 etc)
'Check if the next 6 digits i.e. 7th - 12th digits are numeric.
Dim pattern As String = "\d{2}-[A-Z]\d-\d{2}-\d{6}"
Dim match As Match = Regex.Match(p1, pattern)
Return match.Success
End Function
It is hard to produce a fully working solution as there are too many unknowns about the input as an outsider.
I have an If statement in which I need to check if a decimal number is less than or equal to 4. I planned to do so using this code, which I will paste for some clarity:
Dim handicap As New List(Of Decimal)
For i = 0 To handicap.Count - 1
If handicap.Item(i) <= 4 Then
....
End If
Next i
However, I cannot compare the decimal to the integer type. Is there a way I can do this?
Try If handicap.Item(i) <= Convert.ToDecimal(4) Then
or If handicap.Item(i) <= 4D Then
Read more on https://msdn.microsoft.com/en-us/library/xtba3z33.aspx
I have a .text file of strings delimited by spaces. That would be the obvious parsing solution when I bring it into Excel or Access, but since the strings are not the same size, the fields will be incorrect. I'm hoping to find some help here as I'm not really experienced with this.
This is an example section of the string:
259998887575 15 00:14:38 C33 0:14:42 T33 00:14:52 00:14:58
202224446898 33 00:16:24 B23 00:17:00 C31 00:17:15 T31 00:19:30 C04 00:17:15 T28 00:19:30 00:19:32
The numbers with colons are time stamps and the letter codes (T/C/B) all represent a different field type. My problem is that there can be any number of C and T time stamps listed in the string and there may or may not be a B time stamp.
I'd like the result to show 4 fields... the first, the first c time stamp, the last t time stamp and the last time stamp (the bolded time stamps). All other information can be disregarded. I'd like to use VB to cycle through due to the number of records.
Any help is appreciated!
Edit: It seems like you want to split the dates into types, determined by the bit before each letter.
You can use the Strings.Split method to divide the lines at the spaces. Then use the Date.TryParse method to check which parts are Dates (or times, it's the same). Then you select the first letter of the previous part and sort the dates into their respective lists.
Dim s As String = "202224446898 33 00:16:24 B23 00:17:00 C31 00:17:15 T31 00:19:30 C04 00:17:15 T28 00:19:30 00:19:32"
Dim parts() As String = Strings.Split(s, " ") 'Split the string at all spaces
'Create lists of Date, one list for all dates, one list for each Letter
Dim Tdates As New List(Of Date) 'This stores converted dates
Dim Cdates As New List(Of Date) 'This stores converted dates
Dim Bdates As New List(Of Date) 'This stores converted dates
Dim Alldates As New List(Of Date) 'This stores converted dates
For i = 0 To parts.Count - 1 'Iterate through all parts
Dim ThisDate As Date
If Date.TryParse(parts(i), ThisDate) = True Then 'This tries to parse the current part as a date, returns True if the part is a date
Alldates.Add(ThisDate) 'You want to store all dates in this list
If i > 0 AndAlso parts(i - 1).Length > 0 Then
Dim PrevPartsFirstLetter As String = parts(i - 1)(0) 'Crop the first letter of the previous part
Select Case PrevPartsFirstLetter
Case "T" : Tdates.Add(ThisDate)
Case "C" : Cdates.Add(ThisDate)
Case "B" : Bdates.Add(ThisDate)
End Select
End If
End If
Next
'You can now select the parts you want from the lists
The Tdates-List contains all dates which have a T thing before them and so on. The AllDates-List contains all dates in the line.
I have following values in a column :
123
456
789
65
1
I want to append correct number of zeros in all the values in that column such that the total length of character is 5.
00123
00456
00789
00065
00001
How do I do that?
If there is one number per cell, you can do this easily by changing the format to "Custom."
Right-click on the cells you would like to format.
From the context menu, choose "Format cells"
Choose the Custom category.
Over the word "General," in the Type textbox, enter 00000. (This tells Excel to print with
leading 0s).
Click OK.
If the number is bigger than five digits, it will print all of the digits.
===EDIT===
You explained that these were all in one cell. #paulmorriss has an excellent Excel-function-only solution, but let me proffer a VBA solution as an alternative:
Sub Macro1()
Dim txt As String
Dim asWords() As String
Dim zeros As String
txt = vbNullString
asWords = Split(Range("A1").Value) 'asWords(0)="123" etc.
For i = 0 To UBound(asWords) ' emulate StrDup (missing in VBA)
zeros = vbNullString
For j = Len(asWords(i)) + 1 To 5: zeros = zeros + "0": Next j
txt = txt + zeros + asWords(i) + " "
Next i
Range("B1").Value = txt 'Places answer in B1
End Sub
If the value you specify is in cell A1 then put the following formulae in B1 to K1. The value in K1 is what you need. You could specify one massive formula, but it's easier for people maintaining the spreadsheet to see what's going on if it's split up like this.
in B1 =TEXT(VALUE(LEFT(A1,SEARCH(" ",A1))),"000000")
in C1 =RIGHT(A1,LEN(A1)-SEARCH(" ",A1))
etc. =TEXT(VALUE(LEFT(C1,SEARCH(" ",C1))),"000000")
=RIGHT(C1,LEN(C1)-SEARCH(" ",C1))
=TEXT(VALUE(LEFT(E1,SEARCH(" ",E1))),"000000")
=RIGHT(E1,LEN(E1)-SEARCH(" ",E1))
=TEXT(VALUE(LEFT(G1,SEARCH(" ",G1))),"000000")
=RIGHT(G1,LEN(G1)-SEARCH(" ",G1))
=TEXT(I1,"000000")
=B1&" "&D1&" "&F1&" "&H1&" "&J1