| Author |
Topic |
|
rpc86
Posting Yak Master
200 Posts |
Posted - 2006-08-29 : 04:15:16
|
| Hi guys,How to produce ordinal numbers in sql query?Say I have a table for a Building. My floor numbers are intergers, 1,2,3,4 and so on. When I run the query, the result I need is like this:1st2nd3rd4th5th...23rd....How will I do this?Thank you. |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2006-08-29 : 05:05:30
|
How is this:DECLARE @table table (FloorNumber int)DECLARE @counter intSET @Counter = 1WHILE @Counter < 35 BEGIN INSERT INTO @Table SELECT @Counter SET @Counter = @Counter + 1 ENDSELECT FloorNumber, Ordinal = CASE WHEN FloorNumber BETWEEN 11 AND 20 THEN 'th' WHEN RIGHT(FloorNumber, 1) = 0 THEN 'th' WHEN RIGHT(FloorNumber, 1) = 1 THEN 'st' WHEN RIGHT(FloorNumber, 1) = 2 THEN 'nd' WHEN RIGHT(FloorNumber, 1) = 3 THEN 'rd' WHEN RIGHT(FloorNumber, 1) > 3 THEN 'th' ENDFROM @table --Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2006-08-29 : 05:07:58
|
| How many floors do you have? Seems like it's working up until 110th floor..gets messed up on the 111th.--Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2006-08-29 : 05:10:27
|
Should work now for unlimited floors I belive...DECLARE @table table (FloorNumber int)DECLARE @counter intSET @Counter = 1WHILE @Counter < 200 BEGIN INSERT INTO @Table SELECT @Counter SET @Counter = @Counter + 1 ENDSELECT FloorNumber, Ordinal = CASE WHEN RIGHT(FloorNumber, 2) BETWEEN 11 AND 20 THEN 'th' WHEN RIGHT(FloorNumber, 1) = 0 THEN 'th' WHEN RIGHT(FloorNumber, 1) = 1 THEN 'st' WHEN RIGHT(FloorNumber, 1) = 2 THEN 'nd' WHEN RIGHT(FloorNumber, 1) = 3 THEN 'rd' WHEN RIGHT(FloorNumber, 1) > 3 THEN 'th' ENDFROM @table --Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
rpc86
Posting Yak Master
200 Posts |
Posted - 2006-08-29 : 05:14:48
|
| Thank you for your help. It seems that the last advice is much good.Thank you and more power. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-29 : 05:26:11
|
How about this?SELECT DISTINCT Number, CASE WHEN Number % 100 IN (11, 12, 13) THEN 'th' WHEN Number % 10 = 1 THEN 'st' WHEN Number % 10 = 2 THEN 'nd' WHEN Number % 10 = 3 THEN 'rd' ELSE 'th' END OrdinalFROM master..spt_valuesWHERE Number BETWEEN 1 AND 255ORDER BY Number Peter LarssonHelsingborg, Sweden |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2006-08-29 : 05:37:18
|
| You have the same problem as I initially did...check out floor # 111. It says st when it should be th--Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-29 : 05:47:26
|
The world tallest building has 101 floors and Sears Tower rank no 4 with 110 stories. Your query will still hold  KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-29 : 05:49:19
|
| What about the one building in Shang Hai?600 floors?Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-29 : 05:50:34
|
quote: Originally posted by Lumbago You have the same problem as I initially did...check out floor # 111. It says st when it should be th
Not anymore, check out my revised query...Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-29 : 05:58:19
|
| What about Burj Dubai? 162 floors.Taipei 101? 101 floors.Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-29 : 06:17:04
|
quote: Originally posted by Peso What about Burj Dubai? 162 floors.Taipei 101? 101 floors.Peter LarssonHelsingborg, Sweden
Wow ! Burj Dubai is at least 700 m. http://en.wikipedia.org/wiki/Al_BurjYes Taipei 101 is 101 floors The highest i ever been up to is Menara Telekom Headquarters, Kuala Lumpur, Malaysia. 310 m. KH |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2006-08-29 : 06:21:14
|
Better to be safe than sorry...that's why I always use bigint for my identity-columns (KIDDING! )--Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-29 : 06:25:23
|
The highest building I've been to is Las Vegas Stratosphere, 1149 ft (350 m).And I also did the roller coaster on top and the Space Shot. Talk about "I can see my house from heeeeeeere..." Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-29 : 08:17:43
|
quote: Originally posted by Peso The highest building I've been to is Las Vegas Stratosphere, 1149 ft (350 m).And I also did the roller coaster on top and the Space Shot. Talk about "I can see my house from heeeeeeere..." Peter LarssonHelsingborg, Sweden
Argh ! you beat me to it. I will be in Kuala Lumpur next week. Will try to go to the Petronas Twin Tower. KH |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-08-29 : 08:43:04
|
| Aren't Al Burj and Burj Dubai two different projects?Ob height: 447m (CN Tower SkyPod). |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2006-08-29 : 08:52:59
|
Hm...does Oslo Plaza count?? 34 stories and 117 meters Well, I have been in the empire state building and the tallest building in bankok...no where near any of your suggestions I'm afraid...--Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand" |
 |
|
|
|