Thursday, 13 March 2014

SQL Sever Datediff() Function to Show Date Difference in Days Hours Minutes


Introduction:

Here I will explain datediff() function in
 SQL Server with example to get date difference in days hours minutes, years, month between two dates or calculate datediff in SQL Server in days hours minutes format.

Datediff() function

This function is used to get time difference between two dates like days, hours, minutes, seconds, years, months. Generally Datediff function will take 3 arguments.

Declaration of SubString function

DATEDIFF(DATEPART, startdate, enddate)

In this function startdate and enddate will be valid dates and Datepart will be one of the following like as shown below

Datepart
Datepart (Another way declaration)
year
yy, yyyy
quarter
qq, q
month
mm, m
week
wk, ww
day
dd, d
hour
hh
minute
mi, n
second
ss, s

Examples of Datediff Function:

EX1:


SELECT DATEDIFF(hh,GETDATE()-1,GETDATE()) AS DateDifference
Output:

24
In above query we declared datepart as hh that’s why that return hours difference between two dates

EX2:


SELECT DATEDIFF(day,'2014-02-12',GETDATE()) AS DateDifference
Output:

365
In above query we declared datepart as day that’s why that return days difference between two dates

EX3:


SELECT DATEDIFF(ww,'2014-03-12',GETDATE()) AS DateDifference
Output:

52
In above query we declared datepart as week that’s why that return weeks difference between two dates

Query to Alter,Add,modify,delete columns in table - sql server

Introduction:

In this article I will explain how to write SQL Query to add new column to existing table in sql server and query to delete column from table in sql and query to modify column in existing table.



Query to add new column to table

If we want to add new column to existing table that syntax will be like this


ALTER TABLE Table_Name ADD COLUMN_NAME DATATYPE
Ex:


ALTER TABLE emp_table ADD Manager_Name VARCHAR(50)
From the above query declaration new column “Manager_Name” will add to emp_table with data typeVARCHAR(50)

Query to Drop or delete column from existing table

If we want to drop column from existing table that syntax will be like this


ALTER TABLE Table_Name DROP COLUMN COLUMN_NAME
Ex:


ALTER TABLE emp_table DROP COLUMN Manager_Name
From the above query declaration column “Manager_Name” will drop from emp_table

Query to modify existing column datatype in table

If we want to modify existing column datatype from data table that syntax will be like this


ALTER TABLE Table_Name ALTER COLUMN COLUMN_NAME DATATYPE
Ex:


ALTER TABLE emp_table ALTER COLUMN Manager_Name nvarchar(max)

From the above query declaration I changed “Manager_Name” column datatype from VARCHAR(50) tonvarchar(max)

Wednesday, 12 March 2014

Find Client IP Address using JavaScript & jQuery

How to find client IP Address using javascript & jquery

Designer Page Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
    window.onload = function () {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://jsonip.appspot.com/?callback=DisplayIP";
        document.getElementsByTagName("head")[0].appendChild(script);
    };
    function DisplayIP(response) {
        document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
    }
</script>
</head>
<body>
    <form>
        <span id = "ipaddress"></span>
    </form>
</body>
</html>