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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Help

Status
Not open for further replies.

waynedp

Programmer
Jan 12, 2017
13
US
I have the following SQL statement. What I am trying to do is find the longest length address that has a zip+4. I know there may be many address that have the same length. My problem is I am getting addresses that do not have a zip+4. Any help would be greatly appreciated:

SELECT TOP 1 ADDRESS FROM waynedb ;
ORDER BY SQNCNMBR WHERE LEN(ALLTRIM(ADDRESS)) = (SELECT MIN(LEN(ALLTRIM(ADDRESS))) FROM waynedb ;
WHERE LEN(ALLTRIM(ADDRESS)) > 0 AND LEN(ALLTRIM(ZIP4)) > 9)
 
You have several semicolons.
I think your query raise error on your second row, because this row is treated as different query.
BTW ORDER BY can not be before WHERE clause in SQL Server:
Also, there is NO ALLTRIM() function in SQL Server.
If your ADDRESS/ZIP4 type is n/varchar then there is no need to TRIM it, they are already trimmed.
Something like that:
Code:
SELECT TOP 1 ADDRESS 
       FROM waynedb
WHERE LEN(ADDRESS) = (SELECT MAX(LEN(ADDRESS)) -- You want longest length address, right? then why you query MIN()?
                             FROM waynedb
                      WHERE LEN(ZIP4) > 9)
ORDER BY SQNCNMBR

Borislav Borissov
VFP9 SP2, SQL Server
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top