SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for ASP.Net, SQL Server, Reporting Services, T-SQL, Windows, AWS, SAP HANA and ABAP


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)
Code

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

two-digits-minute-format



SQL Server

SQL Server 2019 Installation
download SQL Server 2019
download SQL Server 2017
download SQL Server 2016
download SQL Server 2014
download SQL Server 2012
MacOS ve SQL Server 2019


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.