SQL DataType - How to store a year?

前端 未结 9 886
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 06:27

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.

相关标签:
9条回答
  • 2020-12-03 07:06

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

    0 讨论(0)
  • 2020-12-03 07:06

    Storage may be only part of the issue. How will this value be used in a query?

    Is it going to be compared with another date-time data types, or will all the associated rows also have numeric values?

    How would you deal with a change to the requirements? How easily could you react to a request to replace the year with a smaller time slice? i.e. Now they want it broken down by quarters?

    A numeric type can be easily used in a date time query by having a look-up table to join with containing things like the start and stop dates (1/1/X to 12/31/x), etc..

    0 讨论(0)
  • 2020-12-03 07:09

    I don't think using an integer or any subtype of integer is a good choice. Sooner or later you will have to do other date like operations on it. Also in 2019 let's not worry too much about space. See what those saved 2 bytes costed us in 2000.

    I suggest use a date of year + 0101 converted to a true date. Similarly if you need to store a month of a year store year + month + 01 as a true date.

    If you have done that you will be able to properly do "date stuff" on it later on

    0 讨论(0)
  • 2020-12-03 07:14

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

    0 讨论(0)
  • 2020-12-03 07:14

    Storing a "Year" in MSSQL would ideally depend on what you are doing with it and what the meaning of that "year" would be to your application and database. That being said there are a few things to state here. There is no "DataType" for Year as of 2012 in MSSQL. I would lean toward using SMALLINT as it is only 2 bytes (saving you 2 of the 4 bytes that INT demands). Your limitation is that you can not have a year older than 32767 (as of SQL Server 2008R2). I really do not think SQL will be the database of choice ten thousand years from now let alone 32767. You may consider INT as the Year() function in MSSQL does convert the data type "DATE" to an INT. Like I said, it depends on where you are getting the data and where it is going, but SMALLINT should be just fine. INT would be overkill ... unless you have other reasons like the one I mentioned above or if the code requirements need it in INT form (e.g. integrating with existing application). Most likely SMALLINT should be just fine.

    0 讨论(0)
  • 2020-12-03 07:17

    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

    0 讨论(0)
提交回复
热议问题