web123456

Problem when using Like in DataTime type column in sqlserver

When there is a field type datetime in the data table, the query entered as follows is invalid and 0 records will be returned:

select * from V_Connection where MakeDate   like '%2012-11%'

The reason I thought of at firstDateTimeType cannot be like string type like and then write the following statement:

select * from V_Connection where CONVERT(varchar,MakeDate) like '%2012-11%'

But there was still no result. After searching online, I realized that the format needed to be specified when converting the date format, so the following results were obtained:

select * from V_Connection where CONVERT(varchar,MakeDate,120) like '%2012-11%'

The time formats searched from the Internet are as follows:

You can find the required results. In convertfunctionIn this case, the first parameter "varchar(50" is the result of the conversion type, the second parameter "addtime" is the field to be converted, and the third is the format for converting date data. The format meaning is as follows:

------------------------------------------------------------------------------------------------------------
Style (2 digits represent year)   | Style (4 digits represent year)   |   Input and output format
------------------------------------------------------------------------------------------------------------
0                               | 100                           |   mon dd yyyy hh:miAM(orPM)
------------------------------------------------------------------------------------------------------------
1                                                                                                                                                                                                                                                              �
------------------------------------------------------------------------------------------------------------
2                               | 102    ANSI               |   yy-mm-dd                                       
------------------------------------------------------------------------------------------------------------
3                               | 103    English-French                |   dd/mm/yy
------------------------------------------------------------------------------------------------------------
4                                                                                                                                                                                                                                                              �
------------------------------------------------------------------------------------------------------------
5                                                                                                                                                                                                                                                              �
------------------------------------------------------------------------------------------------------------
6                               | 106                            |   dd mon yy                                       
------------------------------------------------------------------------------------------------------------
7                               | 107                            |   mon dd,yy                                       
------------------------------------------------------------------------------------------------------------
8                               | 108                            |   hh:mm:ss                                        
------------------------------------------------------------------------------------------------------------
9                               | 109                            |   mon dd yyyy hh:mi:ss:mmmmAM(orPM)
------------------------------------------------------------------------------------------------------------
10
------------------------------------------------------------------------------------------------------------
11                             | 111    Japan                 |   yy/mm/dd
------------------------------------------------------------------------------------------------------------
12                             | 112    ISO                  |   yymmdd                                          
------------------------------------------------------------------------------------------------------------
13
------------------------------------------------------------------------------------------------------------
14
------------------------------------------------------------------------------------------------------------
20
------------------------------------------------------------------------------------------------------------
21

 

 

This table comes from:/xyzqiang/article/details/6823162