Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to store time without seconds in SQL Server

I want to save time in my SQL Server database in this format:

hh:mm

and not this:

hh:mm:ss

Is that possible? Is there a unique data type for that (I tried to go over the list of data types but failed to find; tried also to search on MSDN and didn't find). Is there another way? I really do not want the second to be stored.

like image 640
Michael Haddad Avatar asked Jun 28 '16 11:06

Michael Haddad


People also ask

How do you store time intervals in SQL?

You will need to use two fields to store an interval e.g. [time_span_start] and [time_span_end]. The difference between the two will give you the interval. Save this answer.

What is the alternative of timestamp in SQL Server?

In SQL Server, ROWVERSION and TIMESTAMP data types represent automatically generated binary numbers, unique within the database. ROWVERSION and TIMESTAMP are synonyms, ROWVERSION is available since SQL Server 2005, while TIMESTAMP is deprecated and will be removed in a future version of SQL Server .


2 Answers

Use Format function available from SQL Server 2012

select format(getdate(),'hh:mm')

Insert into sometable
(timepart)
values
(
format(getdate(),'hh:mm'))

Output:

07:08

like image 169
TheGameiswar Avatar answered Nov 15 '22 09:11

TheGameiswar


No there isn't a data type for this. The smallest is time(0), which is accurate to one second.

SELECT
    -- Returns time in the format hh:mm:ss
    CAST(GETDATE() AS TIME(0))
;

You could add a check constraint to ensure the seconds are always equal to 00, but I'm not sure what value this would add.

like image 38
David Rushton Avatar answered Nov 15 '22 11:11

David Rushton