views:

987

answers:

5

I need to insert a year(eg:1988 ,1990 etc) in a database. When I used Date or Datetime data type, it is showing errors. Which datatype should I use.

+1  A: 

Just a year, nothing else ? Why not use a simple integer ?

Frederik Gheysels
Ohh god ,,By common sense
peter
Hey! Why are you trying to make things so simple? We will run out of SO questions!
Joe Philllips
+1  A: 

Use integer if all you need to store is the year. You can also use datetime if you think there will be date based calculations while querying this column

Ender
thanks,,thats my mistake
peter
+9  A: 

If you need to store a year in the database, you would either want to use an Integer datatype (if you are dead set on only storing the year) or a DateTime datatype (which would involve storing a date that basically is 1/1/1990 00:00:00 in format).

TheTXI
+2  A: 

regular 4 byte INT is way to big, is a waste of space!

You don't say what database you're using, so I can't recommend a specific datatype. Everyone is saying "use integer", but most databases store integers at 4 bytes, which is way more than you need. You should use a two byte integer (smallint on SQL Server), which will conserve space better.

KM
A: 

Hey,you can Use year() datatype in MySQL It is available in two-digit or four-digit format.

Note: Values allowed in four-digit format: 1901 to 2155. Values allowed in two-digit format: 70 to 69, representing years from 1970 to 2069

Subodh