|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
RANDOM order by - SQL Server 2000
Hi,
I am wanting to create a SQL query which allows me to order a list by a random letter of the alphabet every time the query is run. So for instance the first time the list will be ordered starting from letter F, second time list is ordered by letter W, and so on. Several attempts at writing this SQL statement proved fruitless, so does anyone have any ideas? Much appreciated! Leila |
|
#3
|
|||
|
|||
|
Let me give you an example.
Say I had a list of names in my database Ann Albert Alfred Bernard Catherine Derrick Emma Fred Greg Ingrid James Kate Leila Mark Paul Richard Sarah Willam Now I want to list them alphabetically from a randomly chosen letter of the alphabet, so say the random letter happens to be 'f' the list should look like this: Fred Greg Ingrid James Kate Leila Mark Paul Richard Sarah Willam Ann Albert Alfred Bernard Catherine Derrick Emma If the randmoly chosen letter doesn't happen to exist in the list then the list should still be ordered by it eg: random letter is 't'. No names starting with the letter 't' in list, but list must still be ordered as in: Willam Ann Albert Alfred Bernard Catherine Derrick Emma Fred Greg Ingrid James Kate Leila Mark Paul Richard Sarah My sql query needs to select a random letter and order list by the selected letter. |
|
#4
|
||||
|
||||
|
select ...
from ... where ... order by case when left(name,1) >= '$letter' then 0 else 1 end, name |
|
#5
|
|||
|
|||
|
Many thanks for your help!
My final code ended up looking like this select ClientName from Client order by case when left(ClientName, 1) >= CHAR(65 + 26 * rand()) then 0 else 1 end, ClientName It works like a dream - thanks again! |
|
#6
|
||||
|
||||
|
well done! that's a cute single-letter-randomizer
|
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > RANDOM order by - SQL Server 2000 |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|