Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Help with query - Selecting Uniques

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2005-08-25 : 21:13:29
I have a table "USERS" with a column "USERNAME"

I want to create a stored procedure that SELECTs all unique records with a unique first 2 characters when passed the first character.

For example, I would like to pass the letter "A" to the SPROC

I would like 1 unique record returned for each username starting with "A" & x

x = any other character

Example results would look something like

aa,
ab,
ac,
ae,
ag,
ai,......

Any suggestions ? This seems like a bit of a tricky one.

The purpose of this is for a directory displayed on a website, and I do not want links to users displayed if there are not any corresponding users so I want to filter out this information. Technically I don't need the username brought back, just the first 2 letters of it.


Thanks alot for any help,

Mike123

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-08-25 : 21:56:53
how about:

select distinct left(username,2) as First2Letters from dbo.users where username like @firstletter + '%'

Be One with the Optimizer
TG
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2005-08-25 : 22:33:30
perfect! thank you very much :)

mike123
Go to Top of Page
   

- Advertisement -