开发者

SELECT, manipulate data temporary using SQL Server

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

开发者_JAVA技巧

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??


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.


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.


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


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 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜