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小时制)
------------------------------------------------------------------------------------------------------------

你可能感兴趣的:(Sql2005中datetime类型用like查找的问题)