Insert (Save) Time in TIME(7) data type in SQL Server

ramco1917
 
on Aug 28, 2022 10:52 PM
495 Views

Hi

Save in Sql Database with Datatype Time(7)

But it is saving in Database like this - 08:30:00.0000000

Field Datatype is Time(7)

Also how to get saved value in Dropdown.

private void BindTime()
{
    DateTime StartTime = DateTime.ParseExact("08:00", "HH:mm", null);
    DateTime EndTime = DateTime.ParseExact("20:00", "HH:mm", null);
    TimeSpan Interval = new TimeSpan(0, 15, 0);
    //TimeSpan Interval = new TimeSpan(1, 0, 0);          
    ddlTimeFrom.Items.Clear();
    ddlTimeTo.Items.Clear();
    while (StartTime <= EndTime)
    {
        ddlTimeFrom.Items.Add(StartTime.ToShortTimeString());
        ddlTimeTo.Items.Add(StartTime.ToShortTimeString());
        StartTime = StartTime.Add(Interval);
    }
    ddlTimeFrom.Items.Insert(0, new ListItem("--Select--", "0"));
    ddlTimeTo.Items.Insert(0, new ListItem("--Select--", "0"));
}
 
String frTime = ddlTimeFrom.SelectedItem.Value;
var frts = TimeSpan.Parse(frTime);
String toTime = ddlTimeTo.SelectedItem.Value;
var tots = TimeSpan.Parse(toTime);

Thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
KasimA
 
on Aug 29, 2022 02:33 AM
ramco1917 says:
Field Datatype is Time(7)

The specified length for TIME Datatype is the precision (fractional) value.

The default fractional scale is 7.

Example:

DECLARE @timeFrom TIME(7) = '12:34:54.1237'
SELECT @timeFrom
-- 12:34:54.1237000

DECLARE @timeFrom1 TIME(3) = '12:34:54.1237'
SELECT @timeFrom1
-- 12:34:54.124

So it is showing as 08:30:00.0000000 in your case.

For more details refer below link.

https://docs.microsoft.com/en-us/sql/t-sql/data-types/time-transact-sql?view=sql-server-ver16