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!

SQL output problem 1

Status
Not open for further replies.

ragepro

Technical User
Sep 9, 2003
8
US
Hello,

I'm trying to output my database by column 'CompanyName' in alphabetical order. Too simple. Now, here's the kicker, I want to start not from A, but from a predetermined letter, say L and have it loop back around to K.

Is this possible with just an SQL query? Or do I have to write a whole program for this.

I've tried

WHERE...BETWEEN 'L' AND 'K'
--and--
WHERE ... LIKE '[l-k%]'

(doesn't find anything of course) I am just not figuring this out. Thanks in advance for your help.
 
I should add that I'm using MS SQL. Thanks.
 
Interesting!

My attempt uses CASE:
Code:
SET NOCOUNT ON

CREATE TABLE tmpTbl (
companyName varchar(20) null,
)

INSERT INTO tmpTbl VALUES ('AA Removals')
INSERT INTO tmpTbl VALUES ('Dumb Company Name')
INSERT INTO tmpTbl VALUES ('K-Time')
INSERT INTO tmpTbl VALUES ('ZZ Blastpower')
INSERT INTO tmpTbl VALUES ('1234 Inc')

SELECT * FROM tmpTbl ORDER BY companyName

--order by from diff letter and back..
SELECT companyName, alphaOrder = 
  CASE 
    WHEN (LEFT(companyName,1) LIKE '[a-j]') THEN 2
    WHEN (LEFT(companyName,1) LIKE '[k-z]') THEN 1
    ELSE 0
  END
FROM tmpTbl
ORDER BY AlphaOrder, companyName

DROP TABLE tmpTbl
the second select gives:
[tt]
companyName alphaOrder
-------------------- -----------
1234 Inc 0
K-Time 1
ZZ Blastpower 1
AA Removals 2
Dumb Company Name 2
[/tt]

It's comparing the first character of company name.. modify as you see fit :)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
VERY COOL!

I now just have to go through the trouble of incorporating it into my ASP script. BAAAA

Thanks a lot man!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top