Sql2005中datetime类型用like查找的问题

在数据表中有一字段类型为datetime时,输入如下的sql语句查询无效,会返回0个记录:

select count(*) as num from ziyuaninfo where addtime like '2009%'

刚开始不解,好像以前是用这条语句查询,一直是正确的?现在怎么就……。

稍一冷静蓦然想起过去常将存放日期数据的字段类型设为varchar(50),因而查询无误,时间一常便形成一种误解,以为datetime类型可以用上面的语句查询。在对日期类型数据查询时,必须要使用convert转换,正确的语句如下:

select count(*) as num from ziyuaninfo where convert(varchar(50),addtime,112) like '2009%'

即可查到所需结果。在convert函数中,第一个参数“varchar(50”是转换类型的结果,第二个参数“addtime”是要转换的字段,第三个是将日期数据转换的格式。格式含义如下:

------------------------------------------------------------------------------------------------------------
Style(2位表示年份)   | Style(4位表示年份)   |   输入输出格式                                   
------------------------------------------------------------------------------------------------------------
0                               | 100                           |   mon dd yyyy hh:miAM(或PM)             
------------------------------------------------------------------------------------------------------------
1                               | 101   美国                |   mm/dd/yy                                      
------------------------------------------------------------------------------------------------------------
2                               | 102    ANSI               |   yy-mm-dd                                       
------------------------------------------------------------------------------------------------------------
3                               | 103    英法                |   dd/mm/yy                                      
------------------------------------------------------------------------------------------------------------
4                               | 104    德国                |   dd.mm.yy                                       
------------------------------------------------------------------------------------------------------------
5                               | 105    意大利             |   dd-mm-yy                                       
------------------------------------------------------------------------------------------------------------
6                               | 106                            |   dd mon yy                                       
------------------------------------------------------------------------------------------------------------
7                               | 107                            |   mon dd,yy                                       
------------------------------------------------------------------------------------------------------------
8                               | 108                            |   hh:mm:ss                                        
------------------------------------------------------------------------------------------------------------
9                               | 109                            |   mon dd yyyy hh:mi:ss:mmmmAM(或PM)
------------------------------------------------------------------------------------------------------------
10                             | 110    美国                 |   mm-dd-yy                                        
------------------------------------------------------------------------------------------------------------
11                             | 111    日本                 |   yy/mm/dd                                       
------------------------------------------------------------------------------------------------------------
12                             | 112    ISO                  |   yymmdd                                          
------------------------------------------------------------------------------------------------------------
13                             | 113     欧洲默认值     |   dd mon yyyy hh:mi:ss:mmm(24小时制)
------------------------------------------------------------------------------------------------------------
14                             | 114                            |   hh:mi:ss:mmm(24小时制)                   
------------------------------------------------------------------------------------------------------------
20                             | 120     ODBC 规范     |    yyyy-mm-dd hh:mi:ss(24小时制)        
------------------------------------------------------------------------------------------------------------
21                             |   121                           |    yyyy-mm-dd hh:mi:ss:mmm(24小时制)
------------------------------------------------------------------------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值