O
O
one__for__one2018-09-22 06:31:36
Entity Framework
one__for__one, 2018-09-22 06:31:36

Which data type to choose in datetime or string class?

Created a class. Parsing XML file. Using EF, I insert objects into the database.
What is the best data type to choose for a property that may or may not store a date?
datetime or string? In XML, the information contains information about the time, and sometimes it does not. If not, then null should be inserted, but Datetime does not support the nullable type.
What's the best way to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
basrach, 2018-09-22
@basrach

Use DateTime?, with a question mark. It's like a regular DateTime, only it can be NULL. It is specially designed for such cases.

A
Andrey Skorzhinsky, 2018-09-22
@AndyKorg

It's not clear what's the catch - where do you define the data type? If in the database, then go to the database documentation:
CAST('2007-05-08 12:35:29. 1234567 +12:15' AS date...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question