SQL语句获取指定时间的数据

galvinsun 2009-03-06 02:39:17
我想请问一下,我要在sql语句中提取SQLServer数据库里面时间字段和系统时间作对比。有如下两个问题该怎么做?
1、我数据里有一个为decimal型的CreateDate数据,日期数据格式为20090306,我想获取今天创建的数据,where语句该怎么写呢?
2、如果我要获取昨天、前天或者其他时间的数据又该怎么写呢?
...全文
229 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
guxiaoshi 2009-03-06
  • 打赏
  • 举报
回复

select * from tb where CreateDate=CONVERT(varchar(8) ,getdate(), 112 )
Roc_Lee 2009-03-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sdhdy 的回复:]
SQL code--日期转换参数
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08

select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20040912110608

select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12

select CONVERT(varchar(12) , getdate(), 112 )
20040912

select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12

select CONVERT(varch…
[/Quote]
这个全面
cnkiminzhuhu 2009-03-06
  • 打赏
  • 举报
回复
CREATE TABLE #temp (id INT IDENTITY(1,1),CreateDate DECIMAL)
INSERT INTO #temp SELECT 20090306
SELECT * FROM #temp WHERE CreateDate=CONVERT(varchar(8) ,getdate(), 112 )
DROP TABLE #temp
sdhdy 2009-03-06
  • 打赏
  • 举报
回复
select * from tb where  datediff(dd,CreateDate,getdate())=0  --今天

select * from tb where datediff(dd,CreateDate,getdate())=1 --昨天

依次类推
pl_mm 2009-03-06
  • 打赏
  • 举报
回复

select * from tb where datediff(dd,createdate,getdate())=0

select * from tb where datediff(dd,createdate,getdate())=1

select * from tb where datediff(dd,createdate,getdate())=-1
sdhdy 2009-03-06
  • 打赏
  • 举报
回复
select * from tb where  CreateDate=CONVERT(varchar(8) ,getdate(), 112 )
sdhdy 2009-03-06
  • 打赏
  • 举报
回复
--日期转换参数
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08

select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20040912110608

select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12

select CONVERT(varchar(12) , getdate(), 112 )
20040912

select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12

select CONVERT(varchar(12) , getdate(), 108 )
11:06:08

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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