这是一个可扩展的解决方案,适用于任何数量。我以为别人用了%100来表示11,12,13,但我弄错了。
WITH CTE_NumbersAS( SELECt 1 num UNIOn ALL SELECT num + 1 FROM CTE_Numbers WHERe num < 1000)SELECt CAST(num AS VARCHAr(10)) + CASE WHEN num % 100 IN (11,12,13) THEN 'th' --first checks for exception WHEN num % 10 = 1 THEN 'st' WHEN num % 10 = 2 THEN 'nd' WHEN num % 10 = 3 THEN 'rd' ELSE 'th' --works for num % 10 IN (4,5,6,7,8,9,0) ENDFROM CTE_NumbersOPTION (MAXRECURSION 0)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)