0

The DB has a field "end date" and 1753/1/1 is supposed to be the value representing nothing, as we cannot assign null to datetime in SQL Server..

In reporting application the select query used, gets the data including those 1753 date values. good

Problem

1753 date causes hell with report calculations. There are a lot of calculations in the report and changing the code to ignore 1753 will be a headache, so I was thinking

Is there a way to tell SQL Server:

SELECT * 
FROM TABLE BUT IN END_DATE 
WHERE VALUE='1753/1/1 01:00:00.000' REPLACE WITH PARAMETER1

CRUDE EDIT (do not have access to debugger currently)

I cannot remember but I get an error like

nothing should be mapped on something

when I try to insert record with null date! THIS HAPPENS ONLY WHEN USING VB.NET in C# it works fine. Do you know why??

5
  • Why can't you assign null to a datetime in SQL Server, by the way? Commented Aug 9, 2011 at 8:35
  • i cannot remember but i get and error something saying nothing should be mapped on something when i insert record with null date! THIS HAPPENS ONLY WHEN USING VB.NET in csharp it works fine. do you know why?? Commented Aug 9, 2011 at 8:45
  • Post your code which raises error when saving null, perhaps it could be because DateTIme is value type so you can't assign NULL, so you have to save SqlDateTime() objects into the dateTime columns Commented Aug 9, 2011 at 8:48
  • thats exactly the case, i shall simulate the problem and post asap Commented Aug 9, 2011 at 8:54
  • Are you filtering on a "startdate" too? Commented Aug 9, 2011 at 8:54

5 Answers 5

4
SELECT * 
FROM TABLE
WHERE END_DATE = 
 CASE VALUE 
   WHEN VALUE = '1753/1/1 01:00:00.000' THEN PARAMETER1 
   ELSE VALUE
 END

EDIT: Answer to comments

You can not assign NULL to DateTime fields because DateTime is a Value Type so it can't persist NULL. Use SqlDateTime.NULL to pass NULL value to stored procedure or query itself. If you can provide code which should pass a NULL - I can give you more detailed example.

Sign up to request clarification or add additional context in comments.

Comments

1

Although I think that null should be used to indicate that something does not have a value, you can try:

SELECT MyDate = CASE END_DATE   
    WHEN '1753/1/1 01:00:00.000' THEN YourDefaultValue
    ELSE END_DATE
END 

See msdn for details.

Comments

0
    SELECT field1, field2, field3, 
        'NEW_END_DATE' = CASE CAST(END_DATE AS DATE) = '1753-1-1' 
             THEN @Parameter 
             ELSE END_DATE 
         END
    FROM TABLE;

Comments

0

You need to use a case

SELECT case when date_field = '1753/1/1 01:00:00.000' then @PARAMETER1
ELSE date_field END
FROM TABLE 

Comments

0

I guess you're probably looking for something like this (note you can't do it with a SELECT *; you'll have to explicitly select the columns you need, unless you want to add the calculated column using a different name.)

SELECT 
   CASE 
      WHEN END_DATE = > '1753/1/1 01:00:00.000' THEN END_DATE 
      ELSE PARAMETER1 
   END 'END_DATE',
   other_columns...
FROM
   TABLE 

But really, I'd recommend changing your design to use NULLs instead of this weird end date you're using. There's nothing special about DATETIME columns that stops them being NULLable. I worked on a project once where someone had done a similar thing with a dopey "invalid" date value, and it caused no end of problems.

2 Comments

this happens only using vb.net really! have any idea why.
Well, it sounds like @sll has a handle on that problem, so I'd follow that advice and switch to using NULL. It'll probably make your life easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.