I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 218, comments - 2281, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

How to store an incomplete date?

When doing apps that deals with date there almost always comes a question on

how to store an incomplete date.

For example:

 

Person A is born on 1980-02-17.

Person B is born on 1980-02 <- The person doesn't know the exact day (This is acctually a real life scenario)

 

How to store this wisely in db? You can't really put a datetime DateOfBirth Column.

I usually take 3 int columns (year, month, day) with constraints but i'm wondering...

 

Does anyone know of a better method?

 

kick it on DotNetKicks.com

Print | posted on Saturday, February 17, 2007 2:05 AM | Filed Under [ SQL Server ]

Feedback

Gravatar

# re: How to store an incomplete date?

hmm....
not a bad idea Denis. Not bad at all. :))

Thanx.
2/17/2007 6:16 PM | Mladen
Gravatar

# re: How to store an incomplete date?

How about:

if (object_id('dbo.Dates') is not null)
begin
drop table dbo.Dates
end
go

create table dbo.Dates
(
CompleteDate datetime
,IncompleteDate varchar(6)
constraint chk_Dates_IncompleteDate
check ((1
= case len(IncompleteDate)
when 6
then isdate(IncompleteDate + '01')
when 4
then isdate(IncompleteDate + '0101')
else 0
end)
or (IncompleteDate is null))
,constraint chk_Dates_CompleteDate
check ((CompleteDate is not null and IncompleteDate is null) or (CompleteDate is null and IncompleteDate is not null))
)
go

insert dbo.Dates
(
CompleteDate
,IncompleteDate
)
select null as CompleteDate
,'198002' as IncompleteDate
union all
select null
,'1980'
union all
select '19800204'
,null
go

select *
from dbo.Dates
go


All you need now is some additional logic for data adding/modifying/retrieval and you're set. ;)


ML
2/18/2007 2:48 PM | ML
Gravatar

# re: How to store an incomplete date?

Thanx Matija. nice one.

I like Denis's more because it's narrower and because both columns have to be always used i can add a Non clustered index on both of them.

Or have i missed something in your solution?
2/19/2007 10:19 AM | Mladen
Gravatar

# re: How to store an incomplete date?

On second look, Denis's solution is better. The only thing I'd change, though, is the DateCorrectness column - I'd go with the bitmap:

0 = correct
1 = day unknown
2 = month unknown
3 = 1 + 2
4 = year unknown
5 = 1 + 4
6 = 2 + 4
7 = 1 + 2 + 4

With this modification you can test the value using the logical AND (&) operator.


ML
2/20/2007 12:15 AM | ML
Gravatar

# re: How to store an incomplete date?

i was aiming for a bitmap also. :)
2/20/2007 11:25 AM | Mladen
Gravatar

# re: How to store an incomplete date?

This has been pondered in both the GEDCOM and ISO 8601 standards.
GEDCOM is for representing genalogical data, with all the uncertainty involved (born either the 30 or 31 of january 1532, by a gregorian calendar, etc)
ISO 8601 is an international standard for the textual representation of date/time/intervals.
ISO 8601 allows you times like 2007W32: Week 32 in year 2007.

It obviously depends what sort of unknowns you need to handle:
unknown: large/small set of values for date, month, year
same for: week/weekday (some sunday during 2007?)
between date x and date y
either date x, y, z
born during orthodox easter 1634
born during a full moon early 1314 or 1313 etc etc etc.

I'd suggest storing an ISO8601 compliant string - there should be libraries to translate back and forth.

gedcom: http://homepages.rootsweb.com/~pmcbride/gedcom/55gcch2.htm#DATE_EXACT
iso 8601: http://www.cl.cam.ac.uk/~mgk25/iso-time.html
4/25/2007 10:51 AM | Arne D Halvorsen
Gravatar

# re: How to store an incomplete date?

thanx for this Arne. very usefull.
4/25/2007 12:10 PM | Mladen
Gravatar

# re: How to store an incomplete date?

i disagree, can i go register in some real org and not give my home address ? I dont remember it, because i have it written down. What about aliens ? then dont have surname :> are you sure that one day an alien wont be using the system?

Person B is born on 1980-02-01 :>

I think you are worried about wrong problems, its just like a elevator desinger was worried about how blind and without hands people will use it :>
5/5/2007 6:11 AM | raveman
Gravatar

# re: How to store an incomplete date?

so to which problem are you acctually reffering to, raveman?

because we acctually have some people that don't know their exact birthdate.
5/5/2007 2:24 PM | Mladen
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET