select no,left(convert(datetime,date,112),6) from tablename

解决方案 »

  1.   

    CAST 和 CONVERT
    将某种数据类型的表达式显式转换为另一种数据类型。CAST 和 CONVERT 提供相似的功能。
      

  2.   

    LEFT
    返回从字符串左边开始指定个数的字符。
      

  3.   

    按照你说的方法我没有找到答案
    那你能不能告诉我
    year(date)  month(date)
    2005         6
    我想达到20056的效果应该怎么写?
    year(date)+month(date)?
      

  4.   

    1.duanduan1122
    说的select no,left(convert(datetime,date,112),6) from tablename
    不对,应该是
    select no,convert(varchar(6),date,112) from tablename2.如果要得到20056的效果:
    select no,convert(varchar(4),getdate(),112) + convert(varchar(2),month(getdate())) from tablename
      

  5.   

    --测试数据
    if object_id('ta') is not null drop table ta
    go
    create table ta(no int, date datetime)
    insert ta  select 1, '2005-6-24'
    union all  select 2, '2005-7-8'
    union all  select 3, '2005-11-1'
    --查询
    select* from ta
    select no,yymm=convert(varchar(6), date, 112) from ta--扫尾
    drop table ta
      

  6.   

    MorningTea:呵呵, 回复时,没看到你的回帖。
    你已经说得很明白了。是我多事了。
      

  7.   

    filebat(Mark)兄,您太客气,你说的也是对的呀,答案的对错与表达的前后没有关系,呵呵 你很认真,其他人要test,copy 你的语句就可以啦~