I’m using MS SQL Server 2005 and I need to store date values partially. For example, the day part of a date may remain unknown in fact but MS SQL Server constrains to specify the full date like the follow:
INSERT foo(dt) VALUES('2001-10-31');
I would like to use something like this though:
INSERT foo(dt) VALUES ('2001-10-??');
Of course, MS SQL cannot accept such a date, and I’ve found a rough example that converts date parts into multipliers:
SET NOCOUNT ON
CREATE TABLE foo (
dt INT
)
INSERT foo VALUES (
DATEPART(YEAR, GETDATE()) * 10000
+ DATEPART(MONTH, GETDATE()) * 100
+ DATEPART(DAY, GETDATE())
)
SELECT dt FROM foo
DROP TABLE foo
… hm, I can’t believe whether it’s the only way to solve the problem, so I would like to ask: how do I resolve this issue in the best way?
Thanks in advance.
UPD 2014:
Also see Storing partial dates in a database
here you can store each date part, and still constrain them to a valid date: