Friday, February 28, 2014

Get Time Difference between Two Dates and Datediff() Function to Show Date Difference in Days Hours Minutes in SQL Server

Hi friends,
In this article I will explain how to get time difference between two dates in SQL server.

DECLARE @Sdate DATETIME, @Edate DATETIME, @Timedifference VARCHAR(100)
SELECT @Sdate = '02/12/2014 08:40:18.000',@Edate='02/13/2014 09:52:48.000'
SET @Timedifference=DATEDIFF(s, @Sdate, @Edate)

SELECT CONVERT(VARCHAR(5),@Timedifference/3600)+':'+convert(varchar(5),@Timedifference%3600/60)+':'+convert(varchar(5),@Timedifference%60) AS Time_Difference


Once we run above query we will get output like as shown below
 Output

Now  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


SELECT DATEDIFF(hh,GETDATE()-1,GETDATE()) AS Date_Difference

Out Put

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

SELECT DATEDIFF(day,'2013-02-07',GETDATE()) AS Date_Difference


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

SELECT DATEDIFF(ww,'2013-02-07',GETDATE()) AS Date_Difference


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

Thursday, February 27, 2014

Get all audio & video devices on a machine


Hello Friends, Today I am going to show you how to get all audio & video devices on a machine, these will be loaded on form load and displayed in a comboboxlist.

Step 1
Download this .rar with the necessary .DLLs. Link below
Microsoft.Expression.Encoder.Utilities.dll, Microsoft.Expression.Encoder.Types.dll, Microsoft.Expression.Encoder.dll, Microsoft.Expression.Encoder.Api2.dll.
http://www.microsoft.com/en-in/download/details.aspx?id=18974
Extract the.DLLs to a your desktop or a folder.

Step 2
Now in visual studio create a new project named GetDevices.
Next right click GetDevices in your Solution Explorer and click Add Reference.
Now select a .Dll and press Add.
Next do this for all the other .DLLs in which I supplied you.


Step 3
Now double click on form1.
Look for namespace GetDevices, above it Replace all the text with this.
Quote:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Expression.Encoder.Devices;
 Step 4
Go back to to Form1.cs[Design]
Add 2 combolistboxes, name one comboBox1, and the other comboBox2.
Navigate back to form1.cs[Code]
In private void Form1_Load(object sender, EventArgs e) add this code.
Quote:


public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            devices();
        }
        public void devices()
        {
 
            foreach (EncoderDevice edv in EncoderDevices.FindDevices(EncoderDeviceType.Video))
            {

                comboBox1.Items.Add(edv.Name);
            }
            foreach (EncoderDevice eda in EncoderDevices.FindDevices(EncoderDeviceType.Audio))
            {
                comboBox2.Items.Add(eda.Name);
              
            }
        }
    }

This is the snapshot for this project is.


You can download source code from here