MS-SQL:时间戳转换为varchar类型的问题

lihuasoft 2011-01-04 09:57:36
我从前台向后台传递一个时间戳(Request["t"]),然后用这个时间戳作为条件值与dt字段进行比较的SQL查询:
select * from table where dt>......

dt字段是varchar(23)型的,格式是'2010-12-23 12:00:00.098'这样的

如何写这个SQL语句?

谢谢
...全文
708 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihuasoft 2011-01-05
  • 打赏
  • 举报
回复
解决:



DateTime requestDt = new DateTime(Convert.ToInt64(HttpContext.Current.Request["dt"]));

SQLStr = "select * from table where dt>'" + requestDt.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
华夏小卒 2011-01-04
  • 打赏
  • 举报
回复
drop table #tb
create table #tb(id int,dt timestamp)

insert #tb(id) select 1
insert #tb(id) select 2
insert #tb(id) select 3

select *,ddate=cast(dt as datetime)
from #tb


id dt ddate
----------- ------------------ -----------------------
1 0x00000000000007D1 1900-01-01 00:00:06.670
2 0x00000000000007D2 1900-01-01 00:00:06.673
3 0x00000000000007D3 1900-01-01 00:00:06.677

(3 行受影响)
华夏小卒 2011-01-04
  • 打赏
  • 举报
回复
时间戳和datetime 是一致的,

如果你的字段dt 中 都是时间类型,没有不规则的字符型的话,可以

select * from table where cast(dt as datetime)>时间戳值
lihuasoft 2011-01-04
  • 打赏
  • 举报
回复
那么说我需要放弃使用时间戳了吗?
mcaajie 2011-01-04
  • 打赏
  • 举报
回复
SQL Server timestamp 数据类型与时间和日期无关。SQL Server timestamp 是二进制数字,它表明数据库中数据修改发生的相对顺序存储大小为 8 字节。
wuyq11 2011-01-04
  • 打赏
  • 举报
回复
SQL Server timestamp 数据类型与时间和日期无关。SQL Server timestamp 是二进制数字,它表明数据库中数据修改发生的相对顺序存储大小为 8 字节。

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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