Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL DataType - How to store a year?

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.

like image 243
peter Avatar asked Mar 30 '09 09:03

peter


People also ask

Is there datatype to store time?

The best data type would be DATETIME.


3 Answers

regular 4 byte INT is way too 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 as 4 bytes, which is way more than you need. You should use a two byte integer (smallint on SQL Server), which will conserve space.

like image 175
KM. Avatar answered Oct 17 '22 22:10

KM.


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).

like image 33
TheTXI Avatar answered Oct 17 '22 22:10

TheTXI


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

like image 14
user786900 Avatar answered Oct 17 '22 22:10

user786900