select * from user_table where 
right(convert(char(8),user_csny,112),4)=right(convert(char(8),getdate(),112),4)附:select CONVERT(varchar(10), getDate(),121) --不要时间2002-01-01
select CONVERT(varchar(10), getDate(),120) --不要时间2002-1-1
select convert(char(8),getdate(),112) ----20020101
select convert(char(8),getdate(),108)  ---06:05:05 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 - dd mon yy 
7 107 - mon dd, yy 
8 108 - hh:mm:ss 
- 9 或 109 (*)  默认值 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM) 
10 110 美国 mm-dd-yy 
11 111 日本 yy/mm/dd 
12 112 ISO yymmdd 
- 13 或 113 (*)  欧洲默认值 + 毫秒 dd mon yyyy hh:mm:ss:mmm(24h) 
14 114 - hh:mi:ss:mmm(24h) 
- 20 或 120 (*)  ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff] 
- 21 或 121 (*)  ODBC 规范(带毫秒) yyyy-mm-dd hh:mm:ss[.fff] 
- 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格) 
- 130* 科威特 dd mon yyyy hh:mi:ss:mmmAM 
- 131* 科威特 dd/mm/yy hh:mi:ss:mmmAM 

解决方案 »

  1.   

    字段是varchar(50)需稍改一下:
    select * from user_table where 
    right(convert(char(8),cast(user_csny as datetime),112),4)=right(convert(char(8),getdate(),112),4)
    测试:
    declare @a varchar(50)
    set @a = '1979-11-29'
    select right(convert(char(8),cast(@a as datetime),112),4)
    -------- 
    1129(所影响的行数为 1 行)
      

  2.   

    要么是表中不是"11-29"這種格式, 要么是like後得到的值不是"%11-29%". 後面估計您是用getdate()來做的. 但您轉換後不是"%11-29%"
      

  3.   

    --数据测试
    IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
          WHERE TABLE_NAME = 'user_table')
       DROP TABLE user_table
    GOCREATE TABLE user_table(
    [ID] INT,
    [NAME]CHAR(10),
    [user_csny]varchar(50)
    )
    GOINSERT user_table SELECT 0001,'小王','1979-11-29'
    UNION SELECT 0002,'小李','1980-06-18'
    GOSELECT * FROM user_table WHERE user_csny LIKE '%11-29%'/*
    ID          NAME       user_csny   
    ----------- ---------- ------------
    1           小王         1979-11-29(所影响的行数为 1 行)
    */--或者SELECT [ID],[NAME],CAST([user_csny]AS DATETIME)AS 出生日期
    FROM user_table
    WHERE user_csny LIKE '%11-29%'/*
    ID          NAME       出生日期                    
    ----------- ---------- ---------------------------
    1           小王         1979-11-29 00:00:00.000(所影响的行数为 1 行)
    */
    --删除测试环境DROP TABLE user_table
      

  4.   

    我用我的表测试过:
    select  right(user_csny,5) as user_csny from user_table where right(user_csny,5) like '11-29'
      

  5.   

    CREATE TABLE user_table(
    [ID] INT,
    [NAME]CHAR(10),
    [user_csny]varchar(50)
    )
    GOINSERT user_table SELECT 0001,'小王','1979-11-29'
    UNION SELECT 0002,'小李','1980-06-18'
    GOselect * from user_table
    where month(user_csny)=month(getdate()) and day(user_csny)=day(getdate())
      

  6.   

    select * from user_table whre user_csny like '%-11-29%'
      

  7.   


    用zjcxc(邹建) ( ) 的这个很简单嘛,为什么要复杂化呢?!
      

  8.   

    这样就万无一失了。
    select * from (
    select * from user_table where isdate(user_csny) = 1
    ) aa
    where right(convert(char(8),cast(user_csny as datetime),112),4)=right(convert(char(8),getdate(),112),4)
      

  9.   


    SELECT * FROM user_table where convert(char,cast(user_csny as datetime),120) LIKE '%-11-29%'
      

  10.   

    select * from user_table where cast(user_csny as char(8)) like '%11-29%'
      

  11.   

    我也觉得邹建得很简单,但没实际验证过,这里不是基础类嘛,对新手来说,好多关键字我们都不知道什么意思,在你们留下好的程序时,能不能多加些注释,谢谢!
    请问那个right是什么意思?