Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching text string on first letter in SQL query

Tags:

sql

vb.net

SAMPLE CODE:

Dim sql As String = "SELECT * FROM " + tblName + " WHERE needsTranslation = 'True' AND dataText LIKE " & "'" & alpha & "%" & "'" & " ORDER BY dataText;"
da = New SqlDataAdapter(sql, strConnection)

OP: I would like to create a SQL query that returns all records when the first letter of a string matches my variable. I am coding this in an ASP.net code behind page in vb.net.

SELECT * FROM " + tblName + " WHERE textData = ' & alpha & "

In this exmample textData is a string of text and alpha is a single letter a through z or A through Z.

I don't need the criteria to be case sensitive, but I do need only the first letter of textData to match alpha.

I have tested the LIKE comparator and it does not return all records that begin with alpha.

What is the best way to do this? Any and all help will be appreciated.

thanks again,

like image 632
htm11h Avatar asked Aug 26 '26 16:08

htm11h


2 Answers

The LIKE operator is what you'd want to use, but you have to use the % wildcard character like so:

SELECT * FROM MyTable WHERE textData LIKE 'a%'
like image 152
Justin Swartsel Avatar answered Aug 28 '26 06:08

Justin Swartsel


SQL has sub-string operator SUBSTR() or SUBSTRING()

select * from tableName where substr( textData ) in ( 'A', 'B', 'C', ... );

like image 42
Tim Child Avatar answered Aug 28 '26 07:08

Tim Child



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!