刚写错了 你这个值已经是日期格式了,不用转换! 非要转换的话,试试下面的SQL--非要转换的话,假如2009-04-02 这个字段是dtColumn select convert(datetime,dtColumn) from tableA
假设字段名是:addDate varchar(10),表名是MyTable,SQL语句如下: select * from MyTable where convert(varchar(10),datetiem,120)<getDate() 查询添加小于今天的记录(其实就是所有的记录)。话说回来,用varchar来存储日期时间,这种数据库设计本身就有问题,每次查询都需要查询转换,效率很低。
--假如2009-04-02 这个字段是dtColumn (varchar),给它加10天, select dateadd(day,10,convert(datetime,dtColumn)) from tableA
不知道你的判断一下是什么意思? 比如与当前系统时间再加10天比较,返回日期小于当前系统时间加10天的所有数据, 可以参考如下SQL代码:--假如2009-04-02 这个字段是dtColumn (varchar),查询小于系统时间加10天的所有数据 select * from tableA where convert(datetime,dtColumn)<dateadd(day,10,getdate())
where convert(varchar(10),你的那个varchar字段,120)<getDate()
declare @Date varchar(30) set @Date='2009-04-02' if (Convert(varchar(10),@Date,120)<=getdate()) begin print '小于等于' end else begin print '大于' end
select * from table where convert(varchar(10),[datetiem],120) =convert(varchar(10),getdate(),120)
declare @Date varchar(30) set @Date='2009-04-02' if (Convert(varchar(10),@Date,120)<=dateadd(day,10,getdate())) begin print '小于等于' end else begin print '大于' end
select * from table where convert(varchar(10),[datetiem],120) =convert(varchar(10),dateadd(day,10,getdate(),120)
select * from MyTable where convert(varchar(10),datetiem,120)<getDate()
可以转换成2009-04-02这种格式
--非要转换的话,加入2009-04-02 这个字段是dtColumn
select convert(datetime,dtColumn) from tableA
不带世纪数位 (yy) (1) 带世纪数位 (yyyy) 标准 输入/输出 (3)
-
0 或 100 (1, 2)
默认设置
mon dd yyyy hh:miAM(或 PM)
1
101
美国
mm/dd/yyyy
2
102
ANSI
yy.mm.dd
3
103
英国/法国
dd/mm/yy
4
104
德国
dd.mm.yy
5
105
意大利
dd-mm-yy
6
106 (1)
-
dd mon yy
7
107 (1)
-
mon dd, yy
8
108
-
hh:mm:ss
-
9 或 109 (1, 2)
默认设置 + 毫秒
mon dd yyyy hh:mi:ss:mmmAM(或 PM)
10
110
美国
mm-dd-yy
11
111
日本
yy/mm/dd
12
112
ISO
yymmdd
-
13 或 113 (1, 2)
欧洲默认设置 + 毫秒
dd mon yyyy hh:mm:ss:mmm(24h)
14
114
-
hh:mi:ss:mmm(24h)
-
20 或 120 (2)
ODBC 规范
yyyy-mm-dd hh:mi:ss(24h)
-
21 或 121 (2)
ODBC 规范(带毫秒)
yyyy-mm-dd hh:mi:ss.mmm(24h)
-
126 (4)
ISO8601
yyyy-mm-ddThh:mm:ss.mmm(无空格)
127(6)
带时区 Z 的 ISO8601。
yyyy-mm-ddThh:mm:ss.mmmZ (无空格)
-
130 (1, 2)
回历 (5)
dd mon yyyy hh:mi:ss:mmmAM
-
131 (2)
回历 (5)
dd/mm/yy hh:mi:ss:mmmAM
DECLARE @dt1 VARCHAR(10)
DECLARE @dt2 DATETIME
SET @dt1 = '2009-04-02'
SET @dt2 = GETDATE()select @dt1 = replace(@dt1,'-','')
select @dt2 = convert(char(8), @dt1, 112)if(@dt1 > @dt2 )
print '1'
else print '2'
刚写错了
你这个值已经是日期格式了,不用转换!
非要转换的话,试试下面的SQL--非要转换的话,假如2009-04-02 这个字段是dtColumn
select convert(datetime,dtColumn) from tableA
select * from MyTable where convert(varchar(10),datetiem,120)<getDate()
查询添加小于今天的记录(其实就是所有的记录)。话说回来,用varchar来存储日期时间,这种数据库设计本身就有问题,每次查询都需要查询转换,效率很低。
select dateadd(day,10,convert(datetime,dtColumn)) from tableA
不知道你的判断一下是什么意思?
比如与当前系统时间再加10天比较,返回日期小于当前系统时间加10天的所有数据,
可以参考如下SQL代码:--假如2009-04-02 这个字段是dtColumn (varchar),查询小于系统时间加10天的所有数据
select * from tableA where convert(datetime,dtColumn)<dateadd(day,10,getdate())
declare @dt varchar(10)
set @dt='2009-03-08'if @dt<getdate()+10
print 'do sth.'
else
print 'do sth. else'
系统会自动做一些隐式转换。
set @Date='2009-04-02'
if (Convert(varchar(10),@Date,120)<=getdate())
begin
print '小于等于'
end
else
begin
print '大于'
end
select * from table where convert(varchar(10),[datetiem],120) =convert(varchar(10),getdate(),120)
set @Date='2009-04-02'
if (Convert(varchar(10),@Date,120)<=dateadd(day,10,getdate()))
begin
print '小于等于'
end
else
begin
print '大于'
end
select * from table where convert(varchar(10),[datetiem],120) =convert(varchar(10),dateadd(day,10,getdate(),120)
可以自己去看下幫助
这样转换
使用 CONVERT:
CONVERT (data_type[(length)], expression [, style])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(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1
select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177
可以转换成2009-04-02这种格式
select * from 表名 where datediff(dd,'2009-04-02',getdate())=0
----------
2009-04-09