Sql server 日期型与数值型的转换....

---问题---
/*
现在遇到一个问题: 数据库是sql server2000,前台录入的是时间,但是数据库中存的确是int型,
如果我要按照日期查找(例如找2007-3-12到2007-3-24的数据),我应该如何写sql语句. 假设表名为test,
保存日期的字段名为open_date.
*/

declare
@test table
(open_date int)

declare @i_date datetime
set @i_date='2007-02-01'
insert into @test select convert(int,@i_date)i_date
set @i_date='2007-03-01'
insert into @test select convert(int,@i_date)i_date
set @i_date='2007-03-12'
insert into @test select convert(int,@i_date)i_date
set @i_date='2007-03-13'
insert into @test select convert(int,@i_date)i_date
select * from @test
select open_date, convert(datetime,open_date)as new_date from @test
where convert(datetime,open_date)>='2007-3-12'
and convert(datetime,open_date)<='2007-3-24'


---运行结果--

open_date  
-----------
39112
39140
39151
39152

(4 row(s) affected)

open_date   new_date                                              
----------- ------------------------------------------------------
39151       2007-03-12 00:00:00.000
39152       2007-03-13 00:00:00.000

(2 row(s) affected) 

posted on   封起De日子  阅读(4221)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2007年3月 >
25 26 27 28 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7
点击右上角即可分享
微信分享提示