Thursday, 22 February 2018

The SQL LIKE Operator



The LIKE operator is used to search for a specific content in a column of the table. The LIKE operator is used with WHERE clause. To search for a particular pattern , some wildcard characters are used with the LIKE operator.

WildCard characters

  • % - is used with LIKE operator to search with zero , one or more characters.
  • _ - is used with LIKE operator to search with single character.


  • Syntax

    SELECT TOP column_name1,column_name2
    FROM table_name
    WHERE LIKE pattern


    Both the percentage and underscore sign can also be used in combination.

    Example

    ID Name State JoinDate Experience
    1 Anoop Sharma Delhi 2017-06-12 12:04:30.233 3
    2 Ankit Pandey Bihar 2017-06-15 01:03:34.247 4
    3 Gaurav Kapoor Pune 2017-08-21 07:30:00.543 2
    4 Arjit Singh Punjab 2017-07-11 10:03:30.577 7


  • The below query will find the record where the value in the Name column will start with 'g' and end with anything.
  • SELECT * FROM [tbl_Record]
    WHERE Name like 'g%'


    Output
    ID Name State JoinDate Experience
    3 Gaurav Kapoor Pune 2017-08-21 07:30:00.543 3


  • The below query will find the record where there is a single character at 2nd and 4th position.

    SELECT * FROM [tbl_Record]
    WHERE State like 'D_l_i'


    Output
    ID Name State JoinDate Experience
    3 Anoop Sharma Delhi 2017-06-12 12:04:30.233 3


  • The below query will find record with any characters at first two positions but ending with 'har'
  • SELECT * from [tbl_Record] WHERE
    State like '__har'


    Output
    ID Name State JoinDate Experience
    2 Ankit Pandey Bihar 2017-06-15 01:03:34.247 4


  • The below query will find the records whose value in the name column ends with 'h'.
  • SELECT * FROM [tbl_Record] WHERE
    Name like '%h'


    Output
    ID Name State JoinDate Experience
    2 Arjit Singh Punjab 2017-07-11 10:03:30.577 7


  • The below query will search for the records whose value in the name column is 'a' at the second place and after that any number of characters.
  • SELECT * from [tbl_Record]
    WHERE Name like '_a%'


    Output
    ID Name State JoinDate Experience
    3 Gaurav Kapoor Pune 2017-08-21 07:30:00.543 3
    3 Tarun sharma Maharashtra 2017-08-11 10:15:25.000 5


  • The below query will search the record where name has value of 'jit' at any position.
  • SELECT * FROM tbl_Record
    WHERE name like '%jit%'


    Output
    ID Name State JoinDate Experience
    2 Arjit Singh Punjab 2017-07-11 10:03:30.577 7


  • The below query will search the record in the table where value of the column field starts with 't' and ends with 'a'.
  • SELECT * FROM tbl_Record
    WHERE Name like 't%a'


    Output
    ID Name State JoinDate Experience
    3 Tarun sharma Maharashtra 2017-08-11 10:15:25.000 5


    Saturday, 17 February 2018

    C# Program to check whether entered year is Leap year or not


    What is the Leap Year?

    Leap year is the year occurring once every four years, having 366 days including 29 February. Below is the program which will determine whether the year entered by user is Leap Year of Not.

    using System;
    namespace Leap_Year
    {
         class Program
         {
          static void Main(string[] args)
          {
             int year;
             Console.WriteLine("Please enter the Year to check for Leap Year in four digits");
             year = Convert.ToInt32(Console.ReadLine());
            if ((year % 4 == 0 && 100 != 0) || (year % 100 == 0))
           {
               Console.WriteLine("Entered Year {0} is the Leap Year", year);
           }
           else
           {
               Console.WriteLine("Entered Year {0} is not the Leap Year", year);
           }
           Console.ReadLine();
          }
         }
    }



    Output
    Please enter the Year to check for Leap Year in four digits
    2018
    Entered Year 2018 is not the Leap Year


    Please enter the Year to check for Leap Year in four digits
    2000
    Entered Year 2000 is not the Leap Year

    Explanation

  • In above program , we prompted user to enter the year the user want to check if it is leap year or not by using Console.WriteLine Statement.

  • The entered value is then accepted using Console.ReadLine() statement and converted into int Datatype because the Console.ReadLine() statement reads the value in String Datatype.

  • Then in the if statement we applied the logic for finding Leap Year and printed the result according to conditions.

  • We can write the same program in a different way. Look at the below program where we have used classes and objects and methods.

    using System;
    namespace Leap_Year
    {
        public class Leap_Calculator_class
        {
         int year;
         public void getUserInput()
         {
          Console.WriteLine("Please enter the Year to check for Leap Year in four digits");
          year = Convert.ToInt32(Console.ReadLine());
         }
         public void calculateLeapYear()
         {
          if ((year % 4 == 0 && 100 != 0) || (year % 100 == 0))
          {
           Console.WriteLine("Entered Year {0} is the Leap Year", year);
          }
          else
          {
           Console.WriteLine("Entered Year {0} is not the Leap Year", year);
          }
          Console.ReadLine();
         }
        }
    }



    using System;
    namespace Leap_Year
    {
    class Program
    {
    static void Main(string[] args)
    {
    Leap_Calculator_class ly = new Leap_Calculator_class();
    ly.getUserInput();
    ly.calculateLeapYear();
    }
    }
    }


    Output
    Please enter the Year to check for Leap Year in four digits
    2018
    Entered Year 2018 is not the Leap Year


    Please enter the Year to check for Leap Year in four digits
    2000
    Entered Year 2000 is not the Leap Year


    Explanation
  • The above program is the modified complex version of the same program to find the entered year by the user is Leap year or not.

  • In this program, we created a public class called Leap_Calculator_class in which we declared and implemented two public methods called getUseInput() and calculateLeapYear().

  • The getUseInput() method will ask user to enter the Year and accept the value and store it in the variable 'year'.

  • In calculateLeapYear() method , we applied the logic to calculate the Leap Year and printed the result on the Console.

  • Then we created another class having main method. We created object of the Leap_Calculator_class and called the two methods.