Convert empty DateTime from 01.01.1900 in SQL to null in C# and vice versa2019 Community Moderator ElectionConvert a string to an enum in C#How can I convert a Unix timestamp to DateTime and vice versa?How do you convert a byte array to a hexadecimal string, and vice versa?Converting a String to DateTimeGet int value from enum in C#Converting XDocument to XmlDocument and vice versaC# DateTime to “YYYYMMDDHHMMSS” formatGetting the Average of a Calculated Item With Nullable DateTimec# (3.5) use TableAdapters to read/write null DateTime values to a databaseHow to convert a nullable datetime value to string using data reader?

Hacking a Safe Lock after 3 tries

Interplanetary conflict, some disease destroys the ability to understand or appreciate music

How to make healing in an exploration game interesting

AG Cluster db upgrade by vendor

Instead of Universal Basic Income, why not Universal Basic NEEDS?

What should tie a collection of short-stories together?

A limit with limit zero everywhere must be zero somewhere

How to deal with taxi scam when on vacation?

Define, (actually define) the "stability" and "energy" of a compound

Do I need to be arrogant to get ahead?

Stiffness of a cantilever beam

My Graph Theory Students

The difference between「N分で」and「後N分で」

Sailing the cryptic seas

Life insurance that covers only simultaneous/dual deaths

How to create the Curved texte?

Official degrees of earth’s rotation per day

What approach do we need to follow for projects without a test environment?

Do the common programs (for example: "ls", "cat") in Linux and BSD come from the same source code?

If curse and magic is two sides of the same coin, why the former is forbidden?

Employee lack of ownership

A sequence that has integer values for prime indexes only:

Charles Hockett - 'F' article?

What's the meaning of “spike” in the context of “adrenaline spike”?



Convert empty DateTime from 01.01.1900 in SQL to null in C# and vice versa



2019 Community Moderator ElectionConvert a string to an enum in C#How can I convert a Unix timestamp to DateTime and vice versa?How do you convert a byte array to a hexadecimal string, and vice versa?Converting a String to DateTimeGet int value from enum in C#Converting XDocument to XmlDocument and vice versaC# DateTime to “YYYYMMDDHHMMSS” formatGetting the Average of a Calculated Item With Nullable DateTimec# (3.5) use TableAdapters to read/write null DateTime values to a databaseHow to convert a nullable datetime value to string using data reader?










2















Our legacy database stores empty Date values as 01.01.1900



I want my application to use 'null' as empty value everywhere by using Converters.



But documentation says that:




A null value will never be passed to a value converter. This makes the implementation of conversions easier and allows them to be shared amongst nullable and non-nullable properties.




My code:



var dateConverter = new ValueConverter<DateTime?, DateTime>(
v => v == null ? new DateTime(1900, 01, 01) : v.Value,
v => v.Year == 1900 ? (DateTime?)null : v);

modelBuilder.Entity<Table>().Property(a => a.Date).HasConversion(dateConverter);

//query
return table.Where(a => a.Date == null);


Is translated to:



select * from table a where a.Date is null


I need it to automatically translate to something like:



select * from table a where a.Date = '1900-01-01T00:00:00.000'


Is there any workaround for null values in Converters?










share|improve this question



















  • 1





    That's neither empty nor null. That's a perfectly valid DateTime either in the SQL Server or C#

    – Panagiotis Kanavos
    Mar 7 at 14:01






  • 2





    why do you use this as a special value? why not make the column nullable which is a non-breaking change

    – Daniel A. White
    Mar 7 at 14:02











  • Why are you trying to change a valid value into null? Where did this value come from? Some table field default? Perhaps that field should be nullable instead of accepting meaningless defaults?

    – Panagiotis Kanavos
    Mar 7 at 14:02






  • 1





    Our legacy database stores empty Date values as 01.01.1900. I can not change this fact.

    – xiety
    Mar 7 at 14:04






  • 1





    @xiety instead of trying to change how SQL works (and having to deal with confusion and bugs when even you forget it's changed behind the scenes), define an Empty date somewhere, eg an AX.Empty constant, and use that for comparisons.

    – Panagiotis Kanavos
    Mar 8 at 8:12
















2















Our legacy database stores empty Date values as 01.01.1900



I want my application to use 'null' as empty value everywhere by using Converters.



But documentation says that:




A null value will never be passed to a value converter. This makes the implementation of conversions easier and allows them to be shared amongst nullable and non-nullable properties.




My code:



var dateConverter = new ValueConverter<DateTime?, DateTime>(
v => v == null ? new DateTime(1900, 01, 01) : v.Value,
v => v.Year == 1900 ? (DateTime?)null : v);

modelBuilder.Entity<Table>().Property(a => a.Date).HasConversion(dateConverter);

//query
return table.Where(a => a.Date == null);


Is translated to:



select * from table a where a.Date is null


I need it to automatically translate to something like:



select * from table a where a.Date = '1900-01-01T00:00:00.000'


Is there any workaround for null values in Converters?










share|improve this question



















  • 1





    That's neither empty nor null. That's a perfectly valid DateTime either in the SQL Server or C#

    – Panagiotis Kanavos
    Mar 7 at 14:01






  • 2





    why do you use this as a special value? why not make the column nullable which is a non-breaking change

    – Daniel A. White
    Mar 7 at 14:02











  • Why are you trying to change a valid value into null? Where did this value come from? Some table field default? Perhaps that field should be nullable instead of accepting meaningless defaults?

    – Panagiotis Kanavos
    Mar 7 at 14:02






  • 1





    Our legacy database stores empty Date values as 01.01.1900. I can not change this fact.

    – xiety
    Mar 7 at 14:04






  • 1





    @xiety instead of trying to change how SQL works (and having to deal with confusion and bugs when even you forget it's changed behind the scenes), define an Empty date somewhere, eg an AX.Empty constant, and use that for comparisons.

    – Panagiotis Kanavos
    Mar 8 at 8:12














2












2








2


1






Our legacy database stores empty Date values as 01.01.1900



I want my application to use 'null' as empty value everywhere by using Converters.



But documentation says that:




A null value will never be passed to a value converter. This makes the implementation of conversions easier and allows them to be shared amongst nullable and non-nullable properties.




My code:



var dateConverter = new ValueConverter<DateTime?, DateTime>(
v => v == null ? new DateTime(1900, 01, 01) : v.Value,
v => v.Year == 1900 ? (DateTime?)null : v);

modelBuilder.Entity<Table>().Property(a => a.Date).HasConversion(dateConverter);

//query
return table.Where(a => a.Date == null);


Is translated to:



select * from table a where a.Date is null


I need it to automatically translate to something like:



select * from table a where a.Date = '1900-01-01T00:00:00.000'


Is there any workaround for null values in Converters?










share|improve this question
















Our legacy database stores empty Date values as 01.01.1900



I want my application to use 'null' as empty value everywhere by using Converters.



But documentation says that:




A null value will never be passed to a value converter. This makes the implementation of conversions easier and allows them to be shared amongst nullable and non-nullable properties.




My code:



var dateConverter = new ValueConverter<DateTime?, DateTime>(
v => v == null ? new DateTime(1900, 01, 01) : v.Value,
v => v.Year == 1900 ? (DateTime?)null : v);

modelBuilder.Entity<Table>().Property(a => a.Date).HasConversion(dateConverter);

//query
return table.Where(a => a.Date == null);


Is translated to:



select * from table a where a.Date is null


I need it to automatically translate to something like:



select * from table a where a.Date = '1900-01-01T00:00:00.000'


Is there any workaround for null values in Converters?







c# linq entity-framework-core ef-core-2.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 14:03









Wai Ha Lee

6,046124065




6,046124065










asked Mar 7 at 13:59









xietyxiety

3329




3329







  • 1





    That's neither empty nor null. That's a perfectly valid DateTime either in the SQL Server or C#

    – Panagiotis Kanavos
    Mar 7 at 14:01






  • 2





    why do you use this as a special value? why not make the column nullable which is a non-breaking change

    – Daniel A. White
    Mar 7 at 14:02











  • Why are you trying to change a valid value into null? Where did this value come from? Some table field default? Perhaps that field should be nullable instead of accepting meaningless defaults?

    – Panagiotis Kanavos
    Mar 7 at 14:02






  • 1





    Our legacy database stores empty Date values as 01.01.1900. I can not change this fact.

    – xiety
    Mar 7 at 14:04






  • 1





    @xiety instead of trying to change how SQL works (and having to deal with confusion and bugs when even you forget it's changed behind the scenes), define an Empty date somewhere, eg an AX.Empty constant, and use that for comparisons.

    – Panagiotis Kanavos
    Mar 8 at 8:12













  • 1





    That's neither empty nor null. That's a perfectly valid DateTime either in the SQL Server or C#

    – Panagiotis Kanavos
    Mar 7 at 14:01






  • 2





    why do you use this as a special value? why not make the column nullable which is a non-breaking change

    – Daniel A. White
    Mar 7 at 14:02











  • Why are you trying to change a valid value into null? Where did this value come from? Some table field default? Perhaps that field should be nullable instead of accepting meaningless defaults?

    – Panagiotis Kanavos
    Mar 7 at 14:02






  • 1





    Our legacy database stores empty Date values as 01.01.1900. I can not change this fact.

    – xiety
    Mar 7 at 14:04






  • 1





    @xiety instead of trying to change how SQL works (and having to deal with confusion and bugs when even you forget it's changed behind the scenes), define an Empty date somewhere, eg an AX.Empty constant, and use that for comparisons.

    – Panagiotis Kanavos
    Mar 8 at 8:12








1




1





That's neither empty nor null. That's a perfectly valid DateTime either in the SQL Server or C#

– Panagiotis Kanavos
Mar 7 at 14:01





That's neither empty nor null. That's a perfectly valid DateTime either in the SQL Server or C#

– Panagiotis Kanavos
Mar 7 at 14:01




2




2





why do you use this as a special value? why not make the column nullable which is a non-breaking change

– Daniel A. White
Mar 7 at 14:02





why do you use this as a special value? why not make the column nullable which is a non-breaking change

– Daniel A. White
Mar 7 at 14:02













Why are you trying to change a valid value into null? Where did this value come from? Some table field default? Perhaps that field should be nullable instead of accepting meaningless defaults?

– Panagiotis Kanavos
Mar 7 at 14:02





Why are you trying to change a valid value into null? Where did this value come from? Some table field default? Perhaps that field should be nullable instead of accepting meaningless defaults?

– Panagiotis Kanavos
Mar 7 at 14:02




1




1





Our legacy database stores empty Date values as 01.01.1900. I can not change this fact.

– xiety
Mar 7 at 14:04





Our legacy database stores empty Date values as 01.01.1900. I can not change this fact.

– xiety
Mar 7 at 14:04




1




1





@xiety instead of trying to change how SQL works (and having to deal with confusion and bugs when even you forget it's changed behind the scenes), define an Empty date somewhere, eg an AX.Empty constant, and use that for comparisons.

– Panagiotis Kanavos
Mar 8 at 8:12






@xiety instead of trying to change how SQL works (and having to deal with confusion and bugs when even you forget it's changed behind the scenes), define an Empty date somewhere, eg an AX.Empty constant, and use that for comparisons.

– Panagiotis Kanavos
Mar 8 at 8:12













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55045549%2fconvert-empty-datetime-from-01-01-1900-in-sql-to-null-in-c-sharp-and-vice-versa%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55045549%2fconvert-empty-datetime-from-01-01-1900-in-sql-to-null-in-c-sharp-and-vice-versa%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices