Home > Software engineering >  how can i store datetime in the table without time fraction part?
how can i store datetime in the table without time fraction part?

Time:03-01

I'm inserting sample call data to the table ContactCallDetail. when I query the data inserted I'm getting datetime in the format yy-mm-dd hh-mm-ss-ff

declare @StartDateTime datetime = convert(varchar, getdate(), 20);
declare @EndDateTime datetime = dateadd(ss,20,@StartDateTime);
INSERT INTO ContactCallDetail values(0,NULL,' xxxxx545xx77',@StartDateTime,@EndDateTime,0);
GO

select * from ContactCallDetail 
SessionID   SessionSeqNum   originatorID    CallingDN   StartDateTime   EndDateTime Transfer
100000  0   NULL     xxxxx5453xx7   2022-02-28 20:11:34.000 2022-02-28 20:11:54.000 0

I need to store the datetime without fractions part(ff). how can I achieve that? i want to see 2022-02-28 20:11:34 instead of 2022-02-28 20:11:34.000

CodePudding user response:

The datetime data type always stores fractional seconds with a 1/300 second precision. Use datetime2(0) if you don't need fractional seconds. This will also save storage space (2 bytes).

CodePudding user response:

Just to be sure and to add to @DanGuzman 's answer, you need to change the datatype of the StartDateTime and EndDateTime columns in the ContactCallDetail table to datetime2(0).

That will also make it so you don't have to do CONVERT either on the way in or the way out.

  • Related