June 17, 2010 at 1:17 am
Hi All,
I want to capture the shift timings in a DB it'll be MM:SS.
what will be the correct way to capture it.
can i use DATETIME and update only the HH:MM columns or I have to go for a varchar with the format HH:MM? and if so how can i restrict the input and force the format hh:mm
Regards,
Ami
June 17, 2010 at 10:08 am
In SQL 2008 you could use the time datatype (12:35:29. 1234567)
Prior to that I would use Datetime.
I would use a datatype that understands date / time and not varchar.
There are many ways to then display / report the values HH:MM to the end user.
June 23, 2010 at 6:42 am
Hi,
in sqlserver 2008 total 4 New date and time data types are added.
The previous versions of SQL Server had two date/time data types DATETIME and SMALLDATETIME. Both of them stored a DATE value and a TIME value together. SQL Server 2008 introduces four new date and time data types.
DATE
TIME
DATETIME2
DATETIMEOFFSET
Gothrough the below url you will have good idea on the data futures in SSMS 2008.
Veeren.
Thanks & Regards,
Veeren.
Ignore this if you feel i am Wrong. 😉
June 23, 2010 at 8:42 am
Here's a quick example of the differences of the different datatypes. Note that to insert the time into the datatypes that support time, you have to prefix your MM:SS with "00:" to make it in HH:MM:SS format. If you are NOT running on SQL 2008, you'll have to remove the "[Time]" field from the table and inserts.
declare @test-2 table (ID int identity, [Time] time NULL, [DateTime] datetime NULL, [char] char(5) NULL, [smalldatetime] smalldatetime NULL)
declare @time char(5)
set @time = '12:55'
insert into @test-2 ([Time]) values ('00:' + @time)
insert into @test-2 ([DateTime]) values ('00:' + @time)
insert into @test-2 ([smalldatetime]) values ('00:' + @time)
insert into @test-2 ([char]) values (@time)
set @time = '12:63' -- invalid time!
begin try
insert into @test-2 ([Time]) values ('00:' + @time)
end try
begin catch
end catch
begin try
insert into @test-2 ([DateTime]) values ('00:' + @time)
end try
begin catch
end catch
begin try
insert into @test-2 ([smalldatetime]) values ('00:' + @time)
end try
begin catch
end catch
begin try
insert into @test-2 ([char]) values (@time)
end try
begin catch
end catch
-- show the results
select *,
RIGHT(convert(char(8), [Time], 8), 5),
RIGHT(convert(char(8), [DateTime], 8), 5),
RIGHT(convert(char(8), [smalldatetime], 8), 5)
from @test-2
Note that when the time is valid, all fields will enter the time. However, if for some reason the time is NOT valid, only the char field will accept it. Using this will allow invalid data, so don't use it.
Also note that since the precision of a smalldatetime is a minute, that the minutes were rounded up. So, don't use this either
The final select shows how to extract the data in MM:SS format.
So, in summary:
If you're on 2008, use the Time datatype, otherwise use the datetime datatype. Of course, the datetime datatype will also work on 2008.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 23, 2010 at 8:57 am
Thats a very good inforamtion.Thanks a lot for the post Wayn.
Veeren.
Thanks & Regards,
Veeren.
Ignore this if you feel i am Wrong. 😉
June 23, 2010 at 10:46 am
Glad it helps
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 23, 2010 at 10:48 pm
Thanks for the information.
I'm using sql server 2005. So I'm taking DATETIME data type. It further helps me to do some calculations on that column.
Regards
Ami
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply