日期列的数据类型 [英] Data Type for date column

查看:100
本文介绍了日期列的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们应该把列的数据类型用于在sql表中存储日期?

Datetime还是varchar?

What should we take the data type for a column to store date in sql table?
Datetime or varchar?

推荐答案

Varchar仅供白痴使用。 DateTime,或没有时间的日期的日期。这就是SQL Server提供它们的原因。



理由不使用varchar



1 - 任何尝试使用值作为日期(什么他们是),将涉及将它们投射到日期或日期时间



2 - 除非您使用YYYY-MM-DD,否则任何按日期对列进行排序的尝试都将无效。



3 - varchar列可以包含任何字符串,因此将其用于日期意味着您的数据没有完整性,值如下周二,在三周内 '和'我喜欢猫',都可以接受。
Varchar is only used by morons. DateTime, or Date for a date with no time. That's why SQL Server offers them.

Reason not to use varchar

1 - any attempt to work with the values as dates ( what they are ), will involve casting them to date or date time

2 - any attempt to sort columns by date won't work unless you used YYYY-MM-DD.

3 - A varchar column can contain any string, so using it for dates means your data has no integrity, values like 'next Tuesday', 'In Three Weeks' and 'I like cats', are all acceptable.


这篇关于日期列的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆