Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search From What Name To What Name 1

Status
Not open for further replies.

Thuky

MIS
Jun 3, 2003
24
US
Hi,

I need some quick help.

As you know that we can pull out data within a time period that we want like: Between @StartDate and @EndDate. Is there a way to search borrowers by the first letter of names like FROM A TO M or from any letter to a letter that we want to search for? Any help will be very appreciated.
 
Code:
SELECT
  *
FROM
  <table>
WHERE
  Left(BorrowerName, 1) LIKE '[a-m]'

You could build this dynamically, e.g.

Code:
DECLARE
  @sql VARCHAR(1000),
  @StartLetter CHAR(1),
  @FinishLetter CHAR(1)

SET @StartLetter = 'a'
SET @FinishLetter = 'm'

SET @sql = 'SELECT * FROM <table> WHERE Left(BorrowerName, 1) LIKE ' + char(39) + '[' + @StartLetter + '-' + @FinishLetter + ']' + char(39)

EXECUTE (@sql)

SQL Server is case insensitive by default so using 'a' or 'A' will give the same result.



Nathan
[yinyang]

Want to get a good response to your question? Read this FAQ! -> faq183-874
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top