INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Need help with an SQL Statement

Need help with an SQL Statement

(OP)
Hi,


Current I am using the following statement to get the longest full name:

SELECT TOP 1 CITY FROM MYDB ORDER BY SQNCNMBR,
WHERE LEN(CITY) = (SELECT MAX(LEN(CITY)) FROM MYDB,
WHERE LEN(CITY)) > 0)


What I am trying also to add is a check for zipcode > 5


Please advice

RE: Need help with an SQL Statement

If I understand your question, you just need to add the check for zipcode to the WHERE clause of the subquery.

CODE

SELECT TOP 1 CITY FROM MYDB ORDER BY SQNCNMBR,
WHERE LEN(CITY) = (SELECT MAX(LEN(CITY)) FROM MYDB,
WHERE LEN(CITY)) > 0 AND LEN(Zip)>5) 

Tamar

RE: Need help with an SQL Statement

What if multiple cities have the same length?

RE: Need help with an SQL Statement

(OP)
Yes, I did that before submitting my question, I am attach the length, but did not get any records, than I tried on another database and it worked. I submitted question because I thought my zip code length wasn't working. And Dave I am only selecting Top 1 for maximum length. Thanks guys

RE: Need help with an SQL Statement

>And Dave I am only selecting Top 1 for maximum length.

You haven't understood the implication Dave makes. While TOP 1 only returns one row (unless you'd use the WITH TIES option), if you have ties, you may still get two or more results with a query fetching the city with "the" longest name and getting, if this isn't a unique length:

CODE

declare @test as table (city varchar(10));
declare @longest as int;

insert into @test values ('Dublin'),('Leeds'),('London');

select top 1 @longest=len(city) from @test order by len(city) DESC
select city from @test where len(city) = @longest 

So while the top length is only one value, there could still be many cities with the same name length.

Bye, Olaf.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close