0

I have an application where there are multiple date fields that can be edited by an Admin when needed. The query updates every field in the table updating it with any new information.

By default, my 3 date fields are empty in the table. When I run my update query, even though I don't add a date it updates the column to:

1900-01-01 00:00:00.000

My update statement is here:

UPDATE tuitionSubmissions
SET reimbursementDate = COALESCE (@reimbursementDate, NULL),
    empGradDate       = COALESCE (@gradDate, NULL),
    payPeriod         = COALESCE (@payPeriod, NULL),
    [status]          = @status,
    notes             = @notes,
    managerApproval   = @empID,
    approvalDate      = COALESCE (GETDATE(), NULL)
WHERE id = @tid;`

From my understanding, I thought that if those variables are empty then it would just keep the field NULL.

Is there another way to do this making it so the date field remains NULL until an actual date / value is sent to the stored procedure?

2
  • Is the field nullable? You can check with SELECT COLUMNPROPERTY(OBJECT_ID('TableName', 'U'), 'ColumnName', 'AllowsNull'); Commented May 21, 2014 at 14:30
  • Looks like the client code is passing a non-null value of zero (0) to the update statement. Try changing the coalesce statements to : NullIf(@reimbursementDate, '1 jan 1900') Commented May 21, 2014 at 14:35

1 Answer 1

2

Your usage of COALESCE is meaningless. It same as IF @reimbursementDate IS NULL THEN NULL. Use it like this:

SET reimbursementDate = COALESCE (@reimbursementDate, reimbursementDate)

In this case the column will stay unchanged if parameter not passed (i.e. parameter is NULL)

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

2 Comments

I updated it to this however as soon as I send other fields to the database, it updates those fields from NULL to 1900-01-01 00:00:00.000
This worked, didnt notice my vars was set as varchar and not datetime

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.