Microsoft SQL Server Function to Get Ordinal Suffix of a Number

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

Thursday, 19 December 2013
Posted by Jebastin
Tag :

PHP Code to Get Ordinal Suffix of a Number

It's a user-defined function which is a lot simpler than you think. Though there might be a .NET function already in existence for this, the following function (written in PHP) does the job. It shouldn't be too hard to port it over. This ordinal function will be useful when we needed to display the date format as 1st December 2013.
  1. function ordinal($num) {
  2.     $ones = $num % 10;
  3.     $tens = floor($num / 10) % 10;
  4.     if ($tens == 1) {
  5.         $suff = "th";
  6.     } else {
  7.         switch ($ones) {
  8.             case 1 : $suff = "st"; break;
  9.             case 2 : $suff = "nd"; break;
  10.             case 3 : $suff = "rd"; break;
  11.             default : $suff = "th";
  12.         }
  13.     }
  14.     return $num . $suff;
  15. }

 Related Function in other languages:

C# Code to Get Ordinal Suffix of a Number

Microsoft SQL Server Function to Get Ordinal Suffix of a Number

Posted by Jebastin
Tag :

C# Code to Get the Latitude and Longitude Co-Ordinates of a Place using Address or Zip-code

The following C# Code is used to get the Latitude and Longitude Co-Ordinates of a particular place anywhere in the World using the Address or Zip-code. To get the Latitude and Longitude Co-Ordinates using Address, call the function like this, getLatLong("Briarwood, NY 11435-2694",""). To get the Latitude and Longitude Co-Ordinates using the Zip-code, call the function like this, getLatLong("","11435").
  1. public string getLatLong(string Address, string Zip)
  2. {
  3.     string latlong = "", address = "";
  4.     if (Address!=string.Empty)
  5.     {
  6.         address = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + Address + "&sensor=false";
  7.     }
  8.     else
  9.     {
  10.         address = "http://maps.googleapis.com/maps/api/geocode/xml?components=postal_code:" + Zip.Trim() + "&sensor=false";
  11.     }
  12.     var result = new System.Net.WebClient().DownloadString(address);
  13.     XmlDocument doc = new XmlDocument();
  14.     doc.LoadXml(result);
  15.     XmlNodeList parentNode = doc.GetElementsByTagName("location");
  16.     var lat = "";
  17.     var lng = "";
  18.     foreach (XmlNode childrenNode in parentNode)
  19.     {
  20.         lat = childrenNode.SelectSingleNode("lat").InnerText;
  21.         lng = childrenNode.SelectSingleNode("lng").InnerText;
  22.     }
  23.     latlong = Convert.ToString(lat) + "," + Convert.ToString(lng);
  24.     return latlong;
  25. }
Posted by Jebastin
Tag : ,

C# Code to Get Ordinal Suffix of a Number

There is no inbuilt capability in the .NET Base Class Library to get the Ordinals. 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 C# code is used to get the Ordinal Suffix of any number.
  1. public static string AddOrdinal(int num)
  2. {
  3.     if (num <= 0) return num.ToString();
  4.     switch (num % 100)
  5.     {
  6.         case 11:
  7.         case 12:
  8.         case 13:
  9.             return num + "th";
  10.     }
  11.     switch (num % 10)
  12.     {
  13.         case 1:
  14.             return num + "st";
  15.         case 2:
  16.             return num + "nd";
  17.         case 3:
  18.             return num + "rd";
  19.         default:
  20.             return num + "th";
  21.     }
  22. }

Another way:

  1. public static string[] SuffixLookup = { "th","st","nd","rd","th","th","th","th","th","th" };
  2.  
  3. public static string AppendOrdinalSuffix(int number)
  4. {
  5. if (number % 100 >= 11 && number % 100 <= 13)
  6. {
  7. return number + "th";
  8. }
  9. return number + SuffixLookup[number% 10];

And the more simplest way:

  1. private static string GetOrdinalSuffix(int num)
  2. {
  3.     if (num.ToString().EndsWith("11")) return "th";
  4.     if (num.ToString().EndsWith("12")) return "th";
  5.     if (num.ToString().EndsWith("13")) return "th";
  6.     if (num.ToString().EndsWith("1")) return "st";
  7.     if (num.ToString().EndsWith("2")) return "nd";
  8.     if (num.ToString().EndsWith("3")) return "rd";
  9.     return "th";

 Related Function in other languages:

PHP Code to Get Ordinal Suffix of a Number

Microsoft SQL Server Function to Get Ordinal Suffix of a Number

Posted by Jebastin
Tag : ,

How to prevent resizing of TextArea?

We need to disable the re-sizable property of a TextArea. Normally, we can resize a TextArea by clicking on the bottom right corner of the TextArea and dragging the mouse. The following CSS is used to disable this behaviour.

Use the following CSS rule to disable this behavior for all TextArea elements:
  1. textarea {
  2.     resize: none;
  3. }
If you want to disable it for some (but not all) TextArea elements, you have a couple of options (thanks to this page).

To disable a specific TextArea with the name attribute set to foo (i.e., <TextArea name="foo"></TextArea>):
  1. textarea[name=foo] {
  2.     resize: none;
  3. }
Or, using an ID (i.e., <TextArea id="foo"></TextArea>):
  1. #foo {
  2.     resize: none;
  3. }
Sunday, 15 December 2013
Posted by Jebastin
Tag : ,

How to get current HTML Page Title/URL with jQuery/Javascript?

Get current HTML page Title and URL with jQuery and Javascript.

Current Page Title:

jQuery:

  1. var title = $(document).find("title").text();
  2. (or)
  3. var title = $('title').text(); 
  4. (or)
  5. var title = $(document).attr('title');

Javascript:

  1. var title = document.getElementsByTagName("title")[0].innerHTML;

Current Page URL:

jQuery:

  1. var url = $(location).attr('href');
  2. (or)
  3. var url = $(location).attr('pathname');

Javascript:

  1. var url = document.URL
  2. (or)
  3. var url = window.location.href
  4. (or)
  5. var url = window.location.pathname;
Posted by Jebastin

Web Service to filter the Umbraco Nodes

Web Service to filter the Umbraco Nodes

The following WebMethod is used to filter the Umbraco child nodes of a particular parent node using the given parent id by the parameters Keyword, Job Type, Position, Location, Category and Division.

Web Method:

  1. public List<JobDetails> FilterJobs(int ParentId, string Keyword, string JobType, string Position, string Location, string Category, string Division)
  2. {
  3. string strJobId = "", strJobDescription = "", strJobRequirements = "", strJobType = "";
  4. string strJobPosition = "", strJobLocation = "", strJobCategory = "", strJobDivision = "";
  5. dynamic JobsNode = new DynamicNode().NodeById(ParentId);
  6. List<DynamicNode> JobsList = JobsNode.Children.Items;
  7. List<JobDetails> FilteredJobsList = new List<JobDetails>();
  8. JobsList = JobsList.Where(x => x.GetProperty("descriptionOfDuties").Value.Contains(Keyword) ||
  9.         x.GetProperty("requirementsExperience").Value.Contains(Keyword) ||
  10.         x.GetProperty("jobType").Value.Contains(Keyword) ||
  11.         x.GetProperty("position").Value.Contains(Keyword) ||
  12.         x.GetProperty("location").Value.Contains(Keyword) ||
  13.         x.GetProperty("category").Value.Contains(Keyword) ||
  14.         x.GetProperty("division").Value.Contains(Keyword) ||
  15.         Keyword == "").ToList();
  16. JobsList = JobsList.Where(x => (x.GetProperty("jobType").Value.Equals(JobType) || JobType == "") &&
  17.         (x.GetProperty("position").Value.Equals(Position) || Position == "") &&
  18.         (Location.Split(',').Contains(x.GetProperty("location").Value.Replace(",", "")) || Location == "") &&
  19.         (Category.Split(',').Contains(x.GetProperty("category").Value) || Category == "") &&
  20.         (Division.Split(',').Contains(x.GetProperty("division").Value) || Division == "")).ToList();
  21. foreach (var job in JobsList)
  22. {
  23.     strJobId = Convert.ToString(job.Id);
  24.     strJobDescription = job.GetProperty("descriptionOfDuties").Value;
  25.     strJobRequirements = job.GetProperty("requirementsExperience").Value;
  26.     strJobType = job.GetProperty("jobType").Value;
  27.     strJobPosition = job.GetProperty("position").Value;
  28.     strJobLocation = job.GetProperty("location").Value;
  29.     strJobCategory = job.GetProperty("category").Value;
  30.     strJobDivision = job.GetProperty("division").Value;
  31.     FilteredJobsList.Add(new JobDetails
  32.     {
  33.         JobId = strJobId.Trim(),
  34.         JobDescription = strJobDescription.Trim(),
  35.         JobRequirements = strJobRequirements.Trim(),
  36.         JobType = strJobType.Trim(),
  37.         JobPosition = strJobPosition.Trim(),
  38.         JobLocation = strJobLocation.Trim(),
  39.         JobCategory = strJobCategory.Trim(),
  40.         JobDivision = strJobDivision.Trim()
  41.     });
  42. }
  43. return FilteredJobsList;
  44. }

Properties:

  1. public class JobDetails
  2. {
  3.     public string JobId { get; set; }
  4.     public string JobDescription { get; set; }
  5.     public string JobRequirements { get; set; }
  6.     public string JobType { get; set; }
  7.     public string JobPosition { get; set; }
  8.     public string JobLocation { get; set; }
  9.     public string JobCategory { get; set; }
  10.     public string JobDivision { get; set; }
  11. }
Saturday, 14 December 2013
Posted by Jebastin

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 -