Pages

Wednesday, April 2, 2014

How to capitalize the first letter in sql ?

Recently I got the requirement of capitalizing the first letter of a string in a drop down list which holds some product related information.

The drop down list looks like the below screen:



From the above screen, I also got the requirement of replacing the letter ';' with the letter ':'. This string is coming from a SQL table and I got some business reasons for why I can't change this value during the insertion into the Database. As I couldn't control it during the insertion neither by writing a query for one time update, I had to follow a different approach. Then I only left with a choice of modifying the value during the selection. Then I wrote the below SQL function and used it in the SELECT statement.

Writing a SQL function:

CREATE FUNCTION [dbo].[CapitalizeFirstLetter] 
(
        -- Add the parameters for the function here
 @InputStr VARCHAR(200)
)
RETURNS VARCHAR(200)
AS
BEGIN
 --Declare a variable
 DECLARE @Index INT,
  @ResultStr VARCHAR(200)
 --Intialize the variables
 SET @Index = 1
 SET @ResultStr = ''
 
 --Loop through the string for each letter
 WHILE (@Index < LEN(@InputStr) + 1)
 BEGIN
  IF (@Index = 1) --First letter of the string
  BEGIN
   --Make the first letter capital
   SET @ResultStr = @ResultStr + upper(substring(@InputStr, @Index, 1))
   SET @Index = @Index + 1--increase the index
  END
  -- If the previous character is space or ';'
  ELSE IF ((SUBSTRING(@InputStr, @Index-1, 1) = ' ' OR SUBSTRING(@InputStr, @Index-1, 1) = ';') AND @Index+1 <> LEN(@InputStr))
  BEGIN
   --make the letter capital
   SET @ResultStr = @ResultStr + UPPER(SUBSTRING(@InputStr,@Index, 1))
   SET @Index = @Index + 1--increase the index
  END
  -- If the current character is ';'
  ELSE IF ((SUBSTRING(@InputStr, @Index, 1) = ';') AND @Index+1 <> LEN(@InputStr))
  BEGIN
   --replace the character ';' with character ':'
   SET @ResultStr = @ResultStr + replace(SUBSTRING(@InputStr,@Index, 1),';', ':')
   SET @Index = @Index + 1--increase the index
  END
  ELSE --all others
  BEGIN
   --make the letter capital
   SET @ResultStr = @ResultStr + LOWER(SUBSTRING(@InputStr,@Index, 1))
   SET @Index = @Index + 1--increase the index
  END
  
  IF (@@ERROR <> 0)-- If any error occurs, assign input string to the result string
  BEGIN
   SET @ResultStr = @InputStr
  END
 END
 RETURN @ResultStr
END
GO


Using the above function in a SELECT statement:
SELECT dbo.CapitalizeFirstLetter(Name) FROM tblProduct

Result:


Thursday, March 20, 2014

Wednesday, March 19, 2014

How to detect IE (Internet Explorer) by using JavaScript ?

In this blog, I will demonstrate how to detect the browser IE including latest version IE 11 by using JavaScript.

Why do we need to detect a specific browser?

As every browser is backed up by its own rendering engine to draw the HTML/CSS web page, you might find some things that do not work perfectly across all the browsers.

Ex: I have recently developed a MVC web application and my requirement was to show loading animation when the form submits.



After I deployed my website to the web server and upgraded my IE to IE 11, I realized that the loading animation is not working in IE 11 browser. It was perfectly working in the other browsers like Chrome and Firefox.

Solution

I had to do some other stuff to make it work on IE by detecting the browser. I thought I could detect the browser IE 11 just by checking a ‘navigator.appName == “Microsoft Internet Explorer”’ property in the JavaScript. But, later I found out IE11 preview has changed the value of navigator.appName. Before IE11, this value is: “Microsoft Internet Explorer”.

Now with IE11 preview, Microsoft changed this value to “Netscape”.
Not only the IE11, for other browsers like Chrome and Firefox also ‘navigator.appName’ property returning ‘Netscape’.

Try the below link in different browsers to find ‘navigator.appName’ value:


To fix the above problem, I had to write a piece of code to detect the browser IE11 by using JavaScript. After a little bit of research on the internet I found this code.

$(document).ready(function () {
if (IsRequestFromIE())
    alert(‘This is IE’);
else
    alert(‘Some other browser’);
});

function IsRequestFromIE(){
   var rv = -1;
   var result = false;
   if (navigator.appName == 'Microsoft Internet Explorer')
   {
     var ua = navigator.userAgent;
     var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
     if (re.exec(ua) != null)
     rv = parseFloat( RegExp.$1 );
   }
   else if (navigator.appName == 'Netscape')
   {
     var ua = navigator.userAgent;
     var re  = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
     if (re.exec(ua) != null)
         rv = parseFloat( RegExp.$1 );
   }
   if(rv != -1)
      result = true;
      return result;
  }

As I found the solution, I thought to blog it in my blogspot. So that it might help to someone who is struggling to resolve this issue.