[ 新手 ] 请问几个关于时间的SQL语句

benjamin9 2003-09-12 10:15:32
现有表结构如下:
TA(DTst datetime,DTed datetime)

求问:
(1)获得DTst字段上年份是2002年的全部记录的SQL语句
(2)获得DTst字段上月份是2002年8月的全部记录的SQL语句
(3)获得DTed字段上时间大于2002年1月21日13点30分00秒并且小于等于2002年1月30日17点00分00秒的全部记录的SQL语句
(4)获得DTed减去DTst之后的秒数,即两个时间之间的时间差(以秒为单位的SQL语句)
...全文
53 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-09-12
  • 打赏
  • 举报
回复
1.
select * from ta where year(dtst)=2002

2.
select * from ta where convert(varchar(7),dtst,120)='2002-08'

3.
select * from ta where dted>'2002-01-21 13:30:00' and dted<='2002-01-30 17:00:00'

4.
select datediff(second,dtst,dted) from ta
cloudchen 2003-09-12
  • 打赏
  • 举报
回复
1.select * from ta where year(DTst) = 2002
2.select * from ta where year(Dtst) = 2002 AND Month(Dtst) = 8
3.select * fromo ta where DTed > '2002-01-21 13:30:00' AND DTed < '2002-01-30 17:00:00'
4.select DateDiff(ss,DTed,DTst) from ta
cloudchen 2003-09-12
  • 打赏
  • 举报
回复
1.select * from ta where year(DTst) = 2002
2.select * from ta where year(Dtst) = 2002 AND Month(Dtst) = 8
3.select * fromo ta where DTed > '2002-01-21 13:30:00' AND DTed < '2002-01-30 17:00:00'
4.select DateDiff(ss,DTed,DTst) from ta
tiny_yan 2003-09-12
  • 打赏
  • 举报
回复
DATEPART
返回代表指定日期的指定日期部分的整数。

语法
DATEPART ( datepart , date )

参数
datepart

是指定应返回的日期部分的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。

日期部分 缩写
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
Hour hh
minute mi, n
second ss, s
millisecond ms


week (wk, ww) 日期部分反映对 SET DATEFIRST 作的更改。任何一年的 1 月 1 日定义了 week 日期部分的开始数字,例如:DATEPART(wk, 'Jan 1, xxxx') = 1,此处 xxxx 代表任一年。

weekday (dw) 日期部分返回对应于星期中的某天的数,例如:Sunday = 1、Saturday = 7。weekday 日期部分产生的数取决于 SET DATEFIRST 设定的值,此命令设定星期中的第一天。

date

是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。对 1753 年 1 月 1 日之后的日期用datetime 数据类型。更早的日期存储为字符数据。当输入 datetime 值时,始终将其放入引号中。因为 smalldatetime 只精确到分钟,所以当用 smalldatetime 值时,秒和毫秒总是 0。

如果只指定年份的最后两位数字,则小于或等于"两位数年份截止期"配置选项的值的最后两位数字的数字所在世纪与截止年所在世纪相同。大于该选项的值的最后两位数字的数字所在世纪为截止年所在世纪的前一个世纪。例如,如果 two digit year cutoff 为 2049 (默认),则 49 被解释为 2049,2050 被解释为 1950。为避免模糊,请使用四位数的年份。

有关时间值指定的更多信息,请参见时间格式。有关日期指定的更多信息,请参见 datetime 和 smalldatetime。

返回类型
int

注释
DAY、MONTH、和 YEAR 函数分别是 DATEPART(dd, date)、DATEPART(mm, date)、和 DATEPART(yy, date) 的同义词。
txlicenhe 2003-09-12
  • 打赏
  • 举报
回复
1:
Select * from TA where year(DTst) = 2002
2:
Select * from TA where year(DTst) = 2002 and month(DTst) = 8
or
Select * from TA where convert(char(6),DTst,112) = '200208'
3:
Select * from TA where DTed > '2003-01-21 13:30:00' and DTed <= '2003-01-30 17:00:00'
4:
Select Datediff(second,DTst,DTed) as 时间差 from TA
tiny_yan 2003-09-12
  • 打赏
  • 举报
回复
DATEDIFF
返回跨两个指定日期的日期和时间边界数。

语法
DATEDIFF ( datepart , startdate , enddate )

参数
datepart

是规定了应在日期的哪一部分计算差额的参数。下表列出了 Microsoft® SQL Server™ 识别的日期部分和缩写。

日期部分 缩写
year yy, yyyy
quarter qq, q
Month mm, m
dayofyear dy, y
Day dd, d
Week wk, ww
Hour hh
minute mi, n
second ss, s
millisecond ms


startdate

是计算的开始日期。startdate 是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。

因为 smalldatetime 只精确到分钟,所以当用 smalldatetime 值时,秒和毫秒总是 0。

如果您只指定年份的最后两位数字,则小于或等于"两位数年份截止期"配置选项的值的最后两位数字的数字所在世纪与截止年所在世纪相同。大于该选项的值的最后两位数字的数字所在世纪为截止年所在世纪的前一个世纪。例如,如果 two digit year cutoff 为 2049(默认),则 49 被解释为 2049,2050 被解释为 1950。为避免模糊,请使用四位数的年份。

有关时间值指定的更多信息,请参见时间格式。有关日期指定的更多信息,请参见 datetime 和 smalldatetime。

enddate

是计算的终止日期。enddate 是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。

返回类型
integer

注释
startdate 是从 enddate 减去。如果 startdate 比 enddate 晚,返回负值。

当结果超出整数值范围,DATEDIFF 产生错误。对于毫秒,最大数是 24 天 20 小时 31 分钟零 23.647 秒。对于秒,最大数是 68 年。

计算跨分钟、秒和毫秒这些边界的方法,使得 DATEDIFF 给出的结果在全部数据类型中是一致的。结果是带正负号的整数值,其等于跨第一个和第二个日期间的 datepart 边界数。例如,在 1 月 4 日(星期日)和 1 月 11 日(星期日)之间的星期数是 1。

tiny_yan 2003-09-12
  • 打赏
  • 举报
回复
(3)获得DTed字段上时间大于2002年1月21日13点30分00秒并且小于等于2002年1月30日17点00分00秒的全部记录的SQL语句

select * from ta where DTed>'2002-01-21 13:30' and DTed<='2002-01-30 17:00'

(4)获得DTed减去DTst之后的秒数,即两个时间之间的时间差(以秒为单位的SQL语句)

select datediff(ss,DTst,DTed) from ta
txlicenhe 2003-09-12
  • 打赏
  • 举报
回复
1:
Select * from TA where year(DTst) = 2002
2:
Select * from TA where year(DTst) = 2002 and month(DTst) = 8
or
Select * from TA where convert(char(6),DTst,112) = '200208'
3:
Select * from TA where DTed > '2003-01-21 13:30:00' and DTed <= '2003-01-30 17:00:00'
4:
Select Datediff(second,DTst,DTed) as 时间差 from TA
cloudchen 2003-09-12
  • 打赏
  • 举报
回复
1.select * from ta where left(convert(char(8),DTst,112),4) = '2002'
2.select * from ta where select convert(char(8),DTst,111) = '2003/08/'
3.select * fromo ta where DTed > '2002-01-21 13:30:00' AND DTed < '2002-01-30 17:00:00'
4.select DateDiff(ss,DTed,DTst) from ta
sdhdy 2003-09-12
  • 打赏
  • 举报
回复
3、select * from TA where DTst between '2002-01-21 13:30:00' and '2002-01-30 17:30:00'
sdhdy 2003-09-12
  • 打赏
  • 举报
回复
4、select datediff(ss,DTst,DTed) from TA
tiny_yan 2003-09-12
  • 打赏
  • 举报
回复
(2)获得DTst字段上月份是2002年8月的全部记录的SQL语句

select * from ta where datepart(yy,DTst)=2002 and datepart(mm,DTst)=8
tiny_yan 2003-09-12
  • 打赏
  • 举报
回复
(2)获得DTst字段上月份是2002年8月的全部记录的SQL语句

select * from ta where datepart(yy,DTst)=2002 and datepart(mm,DTst)=8
dlkfth 2003-09-12
  • 打赏
  • 举报
回复
select * , datediff(second,DTst,DTed)
from ta
dlkfth 2003-09-12
  • 打赏
  • 举报
回复
select * from TA where year(DTst)=2002
select * from TA where year(DTst)=2002 and month(DTst)
select * from TA where DTst>'2002-01-21 13:30:00' and DTst<'2002-01-30 17:30:00'

select * from ta where datediff(second,DTst,DTed)
sdhdy 2003-09-12
  • 打赏
  • 举报
回复
1、 select * from TA where year(DTst)=2002
2、 select * from TA where convert(varchar(7),DTst,120)='2002-08'
or
select * from TA where year(DTst)=2002 and month(DTst)=8
tiny_yan 2003-09-12
  • 打赏
  • 举报
回复
(1)获得DTst字段上年份是2002年的全部记录的SQL语句

select * from ta where datepart(yy,DTst)=2002

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧