sql的datetime和smalldatetime怎么用?
一般情况下就用datetime就是了,smalldatetime精度太低了,判断时间的时候很容易弄错,等你用熟练了sql再使用smalldatetime吧,在c#里面拼一个string类型的sql语句就可以存储时间,如:string TestSql=" inrt into TestTable t TestDateTime='"+ DateTime.Now.ToString() +"' ";
或者string TestSql=" inrt into TestTable t TestDateTime='2010-3-5' ";
或者string TestSql=" inrt into TestTable t TestDateTime='2010-3-5 11:03:11' ";
上面的代码复制粘贴到程序里去试试看就会有体会了。
smalldatetime和smalldate的区别
1、一直以为smalldatetime和datetime的差别只是在于时间范围:
smalldatetime的有效时间范围1900/1/1~2079/6/6
datetime的有效时间范围1753/1/1~9999/12/31
所以我判断如果该值不用到太远的日期范围,就会使用smalldatetime。
2、但我忽略了更关键的差别,那就是smalldatetime只精准到分,而datetime则可精准到3.33毫秒。
因此,当我怎么存,秒都是00后,才发现原来是smalldatetime惹的祸,待我将类型别改成datetime后,秒的部份就可以正常储存了。
*****************************************************************************************
SQL Server中,smalldatetime只能精确到分钟,而datatime可以精确到3%秒(3.33毫秒)。
smalldatetime占用4个字节,前2个字节存储ba date(1900年1月1日)之后的天数。后2个字节存储午夜后的分钟数。
datetime占用8个字节,前4个字节存储ba date(即1900年1月1日)之前或之后的天数,后4个字节存储午夜后的毫秒数。
由于datetime的精度是3%秒,这就涉及到小数,毫秒之前可以是冒号,也可以是小数点。使用冒号时表示的意义同时分秒间隔,11:11:11:21表示021毫秒,前面的0省略,而小数点表示的意义
smalldatetime数据类型精确到什么时间单位
Datetime:
时间格式,对应于数据库中的DateTime类型,对应于.NET里面的System.
DateTime类型。DateTime支持日期从1753年1月1日到9999年12月31日,时间部分的精确度是3.33毫秒,它需要8字节的存储空间。
Datetime2:
时间格式,就Sql
Server
2008
里面新增的字段类型。对应于数据库中的DateTime2格式,对应于.NET里面的System.
DateTime类型。DateTime2则支持从0001年01月01日到9999年12月31日,时间部分的精度是100纳秒,占用6到8字节的存储空间,取决于存储的精度。
datetime2数据类型,类似于之前的datetime类型,不过其精度比较高,可以精确到小数点后面7位(100ns)
sql的datetime和smalldatetime怎么用?
代表日期和一天内的时间的日期和时间数据类型。
datetime
从 1753 年 1 月 1 日到 9999 年 12 月 31 日的日期和时间数据,精确度为百分之三秒(等于 3.33 毫秒或 0.00333 秒)。如下表所示,把值调整到 .000、.003、或 .007 秒的增量。
示例 调整后的示例
01/01/98 23:59:59.999 1998-01-02 00:00:00.000
01/01/98 23:59:59.995,
01/01/98 23:59:59.996,
01/01/98 23:59:59.997, 或
01/01/98 23:59:59.998 1998-01-01 23:59:59.997
01/01/98 23:59:59.992,
01/01/98 23:59:59.993,
01/01/98 23:59:59.994 1998-01-01 23:59:59.993
01/01/98 23:59:59.990 或
01/01/98 23:59:59.991 1998-01-01 23:59:59.990
Microsoft® SQL Server™ 拒绝所有其不能识别为 1753 年到 9999 年间的日期的值。
smalldatetime
从 1900 年 1 月 1 日到 2079 年 6 月 6 日的日期和时间数据精确到分钟。29.998 秒或更低的 smalldatetime 值向下舍入为最接近的分钟,29.999 秒或更高的 smalldatetime 值向上舍入为最接近的分钟。
--returns time as 12:35
SELECT CAST('2000-05-08 12:35:29.998' AS smalldatetime)
GO
--returns time as 12:36
SELECT CAST('2000-05-08 12:35:29.999' AS smalldatetime)
GO
注释
Microsoft SQL Server 用两个 4 字节的整数内部存储 datetime 数据类型的值。第一个 4 字节存储 ba date(即 1900 年 1 月 1 日)之前或之后的天数。基础日期是系统参考日期。不允许早于 1753 年 1 月 1 日的 datetime 值。另外一个 4 字节存储以午夜后毫秒数所代表的每天的时间。
smalldatetime 数据类型存储日期和每天的时间,但精确度低于 datetime。SQL Server 将 smalldatetime 的值存储为两个 2 字节的整数。第一个 2 字节存储 1900 年 1 月 1 日后的天数。另外一个 2 字节存储午夜后的分钟数。日期范围从1900 年 1 月 1 日到 2079 年 6 月 6 日,精确到分钟。
datetime和timestamp的区别
datetime和timestamp是出现在MySQL和SqlServer数据库中的。
MySQL中datetime和timestamp的区别:
(一)范围不同。
“datetime” 以'YYYY-MM-DD HH:MM:SS'格式检索和显示DATETIME值。支持的范围为'1000-01-01 00:00:00'到'9999-12-31 23:59:59'TIMESTAMP值不能早于1970或晚于2037
(二)储存不同。
1,TIMESTAMP
①4个字节储存(Time stamp value is stored in 4 bytes)
②值以UTC格式保存( it stores the number of milliconds)
③时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。
2,datetime
①8个字节储存(8 bytes storage)
②实际格式储存(Just stores what you have stored and retrieves the same thing which you have stored.)
③与时区无关(It has nothing to deal with the TIMEZONE and Conversion.)
注:TIMESTAMP列的显示格式与datetime列相同。
SqlServer中datetime和timestamp在中用法区别基本都差不多。
datetime :
SqlServer中用两个 4 字节的整数内部存储 datetime 数据类型的值。第一个 4 字节存储 ba date(即 1900 年 1 月 1 日)之前或之后的天数。基础日期是系统参考日期。不允许早于 1753 年 1 月 1 日的 datetime 值。另外一个 4 字节存储以午夜后毫秒数所代表的每天的时间。
注:smalldatetime 数据类型存储日期和每天的时间,但精确度低于 datetime。SQL Server 将 smalldatetime 的值存储为两个 2 字节的整数。第一个 2 字节存储 1900 年 1 月 1 日后的天数。另外一个 2 字节存储午夜后的分钟数。日期范围从1900 年 1 月 1 日到 2079 年 6 月 6 日,精确到分钟。
timestamp:
timestamp 这种数据类型表现自动生成的二进制数,确保这些数在数据库中是唯一的。timestamp 一般用作给表行加版本戳的机制。存储大小为 8 字节。
access中用SQL建立表,按书上打的,但提示smalldatetime定义语法错误,是为什么
以下是Access语句:
createtableStudent
(Snochar(5)notnullunique,
Snamechar(20),
Sxchar(1),
BirthdayDATETIME,
Sdeptchar(15));
smalldatetime是SQLServer中的数据类型