Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

SQL substring query help?

I have a bit of a problem trying to query a substring. I'm needing to split a ContactName(which I've done) and query the last name part of it. I would like to be able to query it if a person only enters part of the last name (say the user enters an S - it would receive all last names starting with S, OR maybe enter it as Smi - and the query would return Smith and Smithers) is that possible? I currently have a non working query for my last name and this is what I have so far. This is in C#, but I'm using a sqldataadapter.

SqlDataAdapter("select o.OrderID, c.CompanyName, left(c.ContactName, CHARINDEX(' ', c.ContactName)) as Firstname,substring(c.ContactName, CHARINDEX(' ', c.ContactName)+1, len(c.ContactName)-(CHARINDEX(' ', c.ContactName)-1)) as Lastname, o.OrderDate, o.ShippedDate FROM Customers c JOIN Orders o ON c.CustomerID =o.CustomerID WHERE SUBSTRING(c.ContactName,2,10) = @lastNameFilter ORDER BY c.CompanyName ASC", connection);

Update:

yeah, I'm not worried so much about the pattern right now as I am trying to figure out how to be able to query the substring. Any help on that part?

Update 2:

you led me in the right direction, thank you. I didn't realize I couldn't use the alias name from the select in my where clause, i suppose that's kind of a noob mistake on sql. thank you very much.

1 Answer

Relevance
Still have questions? Get your answers by asking now.