Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

SQL Server - Show Date and Time in a Formatted Manner in SQL Server

Article:
Viewed:  761 
Posted On:  25/07/2015 17:21:30 
How we can show Date & Time in formatted manner as per our business requirement in SQL SERVER ?  

 

In this article I am going to show how we can show Date and time in a formatted manner as per our business requirement.

Suppose you have a date in you Data Base like as: 2015-07-08 00:00:00.000 and you want to show only 2015-07-08

And suppose you have Time in your Data Base like as: 02:28:02.3467545 AM and you want to show like 02:28:02 AM

I will show this how we can write our select statement to achieve above Date Time format:

Below I have 2 Tables:


1.      
Employee
2.       EmployeeDetails

 Data in my Employee

1.png

Image 1.

Data in my Employee Details Table

2.png

Image 2.

Now I will write a join query to fetch data from both tables:

 

SELECT E.Name,E.Email,E.ManagerName, ED.JoiningDate,ED.JoiningTime
FROM Employee AS E
INNER JOIN EmployeeDetails AS ED ON E.ID=ED.Emp_ID
 

Execute this and see result:

3.png

Image 3.

In above result Date and Time format is same as we have in our Data base. So now we will convert this:

 

SELECT E.Name,E.Email,E.ManagerName,
CONVERT(VARCHAR(10), ED.JoiningDate, 101) AS JoininDate,
CONVERT(VARCHAR(8), ED.JoiningTime, 108) + ' '+ RIGHT(CONVERT(VARCHAR, ED.JoiningTime, 100), 3) AS JoiningTime
FROM Employee AS E

INNER JOIN EmployeeDetails AS ED ON E.ID=ED.Emp_ID

 

4.png

Image 4.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us