# Rails, SQL Server 2000 and Dates Pre 1970

**URL:** <https://www.ruby-forum.com/t/rails-sql-server-2000-and-dates-pre-1970/110474>\
**Category:** Rails\
**Created:** [July 26, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/rails-sql-server-2000-and-dates-pre-1970/110474 "2007-07-26T17:33:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![leei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5fc32e/32.png) [@leei](https://www.ruby-forum.com/u/leei)\
**Post date:** [July 26, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/rails-sql-server-2000-and-dates-pre-1970/110474/1 "2007-07-26T17:33:56Z")

</div>

When using dates in Rails with SQL server everything seems to work  
great until I attempt to use a date prior to 1970. I can see the date  
is correct in the database but when I attempt to read the value it is  
returned as nil, if I ask for it again I get the correct date.

I can write dates prior to 1970 into the database however I think is  
facilitated by validates\_date plugin which seems to work a charm.  
Without the plugin I think I would get errors.

If in my controller I ask for the date to be formated as string and  
just rescue the failure as it always fails first time, then the date  
works from then on, but I would really like to get this fixed so I  
dont have to perform this little dance every time I use a date field.

I have found a bug report [http://dev.rubyonrails.org/ticket/3430](http://dev.rubyonrails.org/ticket/3430) which  
was opened around 2 years ago and there are a couple of suggestions  
but none seem to work for me.

Does anyone have any suggestions?

---

<div class="post-metadata">

**Author:** ![leei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5fc32e/32.png) [@leei](https://www.ruby-forum.com/u/leei)\
**Post date:** [July 26, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/rails-sql-server-2000-and-dates-pre-1970/110474/2 "2007-07-26T17:45:57Z")

</div>

forgot to add that its rails 1.2.3

Here is the console feedback showing the issue as well.

> > can = Candidate.find(1)  
> > can = Candidate.find(1)  
> > =\> #\<Candidate:0x4658160 @attributes={“canid”=\>"", “minority\_id”=\>nil,  
> > “adjustment\_assessment”=\>"", “adjustment\_learning”=\>"",  
> > “sensory\_id”=\>nil, “gender”=\>“Male”, “firstname”=\>“B “,  
> > “lastname”=\>“Jones”, “id”=\>1, “assessment\_id”=\>1,  
> > “note\_take\_method\_id”=\>nil, “help\_achieve”=\>””,  
> > “additional\_evidence”=\>"", “date\_of\_birth”=\>“1901/04/04 00:00:00”}\>
> 
> > can.date\_of\_birth  
> > can.date\_of\_birth  
> > =\> nil
> 
> > can.date\_of\_birth  
> > can.date\_of\_birth  
> > =\> #\<DateTime: 4830957/2,0,2299161\>
> 
> >

Its just plain weird.

---

<div class="post-metadata">

**Author:** ![leei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5fc32e/32.png) [@leei](https://www.ruby-forum.com/u/leei)\
**Post date:** [July 26, 2007, 6:42pm UTC](https://www.ruby-forum.com/t/rails-sql-server-2000-and-dates-pre-1970/110474/3 "2007-07-26T18:42:02Z")

</div>

I know I keep talking to myself but here goes nothing.

In the model I have added:

def date\_of\_birth  
Date.parse(self.date\_of\_birth\_before\_type\_cast)  
end

Which means that the date returns first time, so the problem is  
definately to do with the casting of the field. How I fix this I don’t  
know yet, but at least the workaround seems to do the trick. And I  
only need to add it to each model using dates but better than in the  
controllers.
