How to get minutes and seconds from datetime in SQL?

0
173
mssql-server

There are a couple of ways to get minutes and seconds in SQL from datetime field. I will talk and write sql query in context with MS-SQL Server. Let me quickly give you SQL query, that will return minutes and seconds from datetime. I will walk you through whole process using snap shot images.

Get Hour Minute and Second from datetime column

SELECT FORMAT(GETDATE(),'mm:ss') );

The above Query shows “How to get minutes and seconds from datetime in SQL”. I have used FORMAT in-build function provided by SQL Server in above query.

FORMAT (Transact-SQL)

Returns a formatted value with the format and optional culture listed. Use the FORMAT feature to format the date / time and number values locale-aware as strings. Using CAST or CONVERT for general transformations of data sort. You can find more detailed references at https://docs.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-ver15

Get Hour Minute and Second from datetime column

Convert is built-in conversion feature has a very rich amount of code to translate data into various formats.

To get Hour and Minute from the datetime column in 12 hour date format

SELECT SUBSTRING(CONVERT(CHAR(20),GETDATE(),109), 14, 7) AS HoursMinutesSeconds;

Must Read RPA Developer

how-to-get-minutes-and-seconds-from-datetime-in-sql
Hour Minute and Second from datetime

To get Hour and Minute from the date column in 24 hour time format

SELECT CONVERT(CHAR(8),GETDATE(),108) AS HourMinuteSecond;

how-to-get-minutes-and-seconds-from-datetime-in-sql
To get Hour and Minute from the date column in 24 hour time format

You can find more details about “Convert” function at https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15

We can get minutes and seconds from datetime in SQL using Convert function. Please find below SQL query –

SELECT Substring(CONVERT(CHAR(8),GETDATE(),108),4,12);

Although this query gives desired output, but I don’ reccomend this query. I hope, I covered all aspects to get minutes and seconds from datetime. Please comment, like and share this post.

WordPress PHP Memory Limit – Increase Memory Limit

If you have any questions, please drop a comment or mail me. I will answer at earliest. Thank you and keep reading, aquire knowledge as much as you can.

Please follow and like us:

LEAVE A REPLY

Please enter your comment!
Please enter your name here