SQL Server administration and T-SQL development, Web Programming with ASP.NET and Javascript, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Vista, etc.




Example Two Digits Minutes Format using T-SQL


While using t-sql, it is common for sql developers that they require minutes format in 2 digit format.
I'll try to show you easiest method for two digits minutes formatting with sql example.

We will use DATEPART function in our example minutes formatting sql script.
Using DATEPART with MI (Minutes) argument, we can get the minutes value.
Later we had to convert the numeric DATEPART function output into a string variable using CONVERT or CAST string conversion function.
The last step is a string manipulation trick, by adding a zero string character and fetching the RIGHT part of the concatenated string.





DECLARE @Date DATETIME

SET @Date='20090909 09:00'
SELECT RIGHT('0' + CAST(DATEPART(MI,@date) AS Varchar(2)), 2)

SET @Date='20090909 09:09'
SELECT RIGHT('0' + CAST(DATEPART(MI,@date) AS Varchar(2)), 2)

SELECT RIGHT('0' + CAST(DATEPART(MI,GETDATE()) AS Varchar(2)), 2)

And the output of the above two digits minute format t-sql statement is as follows :

two-digits-minute-format







Related SQL Resources

SQL Server Articles

SQL Server 2012

SQL Server Tools

SQL Blog

SQL Server 2008 Blog

Certification Exams Blog

Reporting Services Blog

Analysis Services Blog

MS SQL Server Forums



Free Exam Vouchers









Copyright © 2004 - 2012 Eralper Yilmaz. All rights reserved.
Community Server, by Telligent Systems