Home All Groups Group Topic Archive Search About

Simple SELECT statement???

Author
28 Jul 2005 3:36 PM
Tim::..
Can someone please tell me how I do a select for values that starts with a
letter...

For example if I wanted to find all the employees from a database that
lastnames started with "A"???

EG...
SQL = "SELECT LastName, FirstName, Initial,Dept,TeleNo, Email FROM
tblNonADUsers WHERE lastname =" & alpha & "* ORDER BY LastName ASC"

Thanks for any help!

Author
28 Jul 2005 3:44 PM
Roji. P. Thomas
SQL = "SELECT LastName, FirstName, Initial,Dept,TeleNo, Email FROM
tblNonADUsers WHERE lastname LIKE" & alpha & "%  ORDER BY LastName ASC"


--
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com


Show quote
"Tim::.." <myatix_at_hotmail.com> wrote in message
news:F4FB7A55-1C70-4D31-8843-D1BE9E41A1EA@microsoft.com...
> Can someone please tell me how I do a select for values that starts with a
> letter...
>
> For example if I wanted to find all the employees from a database that
> lastnames started with "A"???
>
> EG...
> SQL = "SELECT LastName, FirstName, Initial,Dept,TeleNo, Email FROM
> tblNonADUsers WHERE lastname =" & alpha & "* ORDER BY LastName ASC"
>
> Thanks for any help!
Author
28 Jul 2005 3:51 PM
Steve
In news:O$EhDs4kFHA.320@TK2MSFTNGP09.phx.gbl,
Roji. P. Thomas <thomasr***@gmail.com> said:
> SQL = "SELECT LastName, FirstName, Initial,Dept,TeleNo, Email FROM
> tblNonADUsers WHERE lastname LIKE" & alpha & "%  ORDER BY LastName
> ASC"

Close, but no banana.

SQL = "SELECT LastName, FirstName, Initial,Dept,TeleNo, Email FROM
tblNonADUsers WHERE lastname LIKE '" & alpha & "%'  ORDER BY LastName
ASC"

--
Steve
Author
28 Jul 2005 3:50 PM
Jens Süßmeyer
Use a string function like RIGHT,lEFT or SUBSTRING

Select * from Customers where LEFT(lastname,1) = 'A'

HTH, Jens Suessmeyer.

Show quote
"Tim::.." wrote:

> Can someone please tell me how I do a select for values that starts with a
> letter...
>
> For example if I wanted to find all the employees from a database that
> lastnames started with "A"???
>
> EG...
> SQL = "SELECT LastName, FirstName, Initial,Dept,TeleNo, Email FROM
> tblNonADUsers WHERE lastname =" & alpha & "* ORDER BY LastName ASC"
>
> Thanks for any help!
Author
28 Jul 2005 3:53 PM
Steve
In news:6B9D26A1-42F9-44CA-ABC6-1D54CD45ED41@microsoft.com,
Jens Süßmeyer <JensSme***@discussions.microsoft.com> said:
> Use a string function like RIGHT,lEFT or SUBSTRING
>
> Select * from Customers where LEFT(lastname,1) = 'A'
>
> HTH, Jens Suessmeyer.

It would be a bad idea to do this, as SQL Server would have to do a scan to
find matching records.  Use "like" to find strings beginning with certain
characters, as it can then use a seek.

--
Steve
Author
28 Jul 2005 3:50 PM
Aaron Bertrand [SQL Server MVP]
(a) SQL Server uses % not * for wildcards
(b) wildcard searches require LIKE, not =, since the value will NOT be equal
to the string you present
(c) strings need to be enclosed in single quotes ' '

SQL = "SELECT ... WHERE LastMame LIKE '" & alpha & "%' ORDER BY ..."






Show quote
"Tim::.." <myatix_at_hotmail.com> wrote in message
news:F4FB7A55-1C70-4D31-8843-D1BE9E41A1EA@microsoft.com...
> Can someone please tell me how I do a select for values that starts with a
> letter...
>
> For example if I wanted to find all the employees from a database that
> lastnames started with "A"???
>
> EG...
> SQL = "SELECT LastName, FirstName, Initial,Dept,TeleNo, Email FROM
> tblNonADUsers WHERE lastname =" & alpha & "* ORDER BY LastName ASC"
>
> Thanks for any help!
Author
28 Jul 2005 3:51 PM
Mike Epprecht (SQL MVP)
Hi

LIKE is what you want.

SQL = "SELECT LastName, FirstName, Initial,Dept,TeleNo, Email FROM
tblNonADUsers WHERE lastname LIKE '" & alpha & "%' ORDER BY LastName ASC"

Regards
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/



Show quote
"Tim::.." wrote:

> Can someone please tell me how I do a select for values that starts with a
> letter...
>
> For example if I wanted to find all the employees from a database that
> lastnames started with "A"???
>
> EG...
> SQL = "SELECT LastName, FirstName, Initial,Dept,TeleNo, Email FROM
> tblNonADUsers WHERE lastname =" & alpha & "* ORDER BY LastName ASC"
>
> Thanks for any help!
Author
28 Jul 2005 3:52 PM
Tim::..
Don't worry...

Show quote
"Tim::.." wrote:

> Can someone please tell me how I do a select for values that starts with a
> letter...
>
> For example if I wanted to find all the employees from a database that
> lastnames started with "A"???
>
> EG...
> SQL = "SELECT LastName, FirstName, Initial,Dept,TeleNo, Email FROM
> tblNonADUsers WHERE lastname =" & alpha & "* ORDER BY LastName ASC"
>
> Thanks for any help!
Author
28 Jul 2005 3:54 PM
Steve
In news:B05FA590-2806-4098-B97C-6082A39818D1@microsoft.com,
Tim::.. <myatix_at_hotmail.com> said:
> Don't worry...

....be happy?

--
Steve

AddThis Social Bookmark Button