What is the default value for datetime2?
Edited: Sorry maybe I explain me wrong, I've tried to insert getDate() and it save me this value.
I need to know what is the value in this field (red one) of my ssms
What is the default value for datetime2?
Edited: Sorry maybe I explain me wrong, I've tried to insert getDate() and it save me this value.
I need to know what is the value in this field (red one) of my ssms
4条答案
按热度按时间e1xvtsh31#
Everything in SQL Server has a default value of
NULL
unless you specify otherwise. For exampleDECLARE @MyDate datetime2(0);
:@MyDate
will have a value ofNULL
.As for tables:
The column
MyDate
will have a default value ofNULL
,MyDate2
will beGETDATE()
(which is the current date and time when the row was created). FinallyMyDate3
will have a default value of 01 January 2000.Stored Procedures/Functions work slight differently:
Here,
@MyDate
has no default value, thus it must have a value supplied to be able to use the Procedure.@MyDate2
, however, has a default value ofNULL
, thus doesn't need to be supplied and the valueNULL
will be used. If a value for@MyDate2
is supplied, that supplied value will be used.@MyDate3
has a default value of 01 January 2000, thus if the parameter isn't specified then the default value will be used.Edit: it's worth noting that if you pass
NULL
in anINSERT
statement to a table, or SP/Function, with a default value (that isn'tNULL
) thenNULL
will be used. It is only if the column/parameter is omitted from theINSERT
/EXEC
/etc statement that the default value will be used.3zwjbxry2#
If you need to specify the default value you should use that format : '9999-12-31 23:59:59.9999999'
If you need UTC Now, you should use getutcdate()
https://i.stack.imgur.com/a3OiS.png
unguejic3#
In the Microsoft Documentation for datetime2 , it shows that the default value for datetime2 is
'1900-01-01 00:00:00'
It's four years late, but I hope it helps others down the line.
8fq7wneg4#
The default value is '1900-01-01 00:00:00.0000000' in SQL Server but when you try from EntityFramework it comes '0001-01-01 00:00:00.0000000' and to achieve this you put your code like this :
the datetime variable or value should be assigned ''
So the output be like this
Also when you look up for your record the query is select * from TblTickets where UpdatedDate = ''