Posted by : Jebastin Thursday 19 December 2013

This AddOrdinal function will be useful when we needed to display the date format as 1st July -3rd October. There is nothing in there about ordinals, but it can't be done using String.Format. However it's not really that hard to write a function to do it. The following Microsoft SQL Server Function is used to get the Ordinal Suffix of any number.
  1. CREATE FUNCTION [Internal].[GetNumberAsOrdinalString]
  2. (
  3.     @num int
  4. )
  5. RETURNS nvarchar(max)
  6. AS
  7. BEGIN
  8.     DECLARE @Suffix nvarchar(2);
  9.     DECLARE @Ones int; 
  10.     DECLARE @Tens int;
  11.     SET @Ones = @num % 10;
  12.     SET @Tens = FLOOR(@num / 10) % 10;
  13.     IF @Tens = 1
  14.     BEGIN
  15.         SET @Suffix = 'th';
  16.     END
  17.     ELSE
  18.     BEGIN
  19.     SET @Suffix =
  20.         CASE @Ones
  21.             WHEN 1 THEN 'st'
  22.             WHEN 2 THEN 'nd'
  23.             WHEN 3 THEN 'rd'
  24.             ELSE 'th'
  25.         END
  26.     END
  27.     RETURN CONVERT(nvarchar(max), @num) + @Suffix;
  28. END

 Related Function in other languages:

C# Code to Get Ordinal Suffix of a Number

PHP Code to Get Ordinal Suffix of a Number

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -