1、用VARCHAR类型
declare @t varchar(10)
select @t='10:23'
select @t结果:---------- 
10:23      (1 row(s) affected)
2、用DATETIME类型
declare @t datetime
select @t='10:23'
select @t
select convert(char(10),@t,108)结果:
                            
--------------------------- 
01 1 1900 10:23AM           (1 row(s) affected)           
---------- 
10:23:00   (1 row(s) affected)

解决方案 »

  1.   

    我不是这个意思
    我是说用一个变量来获得当前时间的小时和分的参数
    比如declare @char_time char(10)
        select @char_time=datepart(h,getdate())
       可是这样只能单独的获得一个参数:小时
        我要一起得到两个参数怎么的到那?
      

  2.   

    select convert(char(5),getdate(),108)
      

  3.   

    select @char_time=convert(char(5),getdate(),108)
      

  4.   

    如果把变量@char_time在转化成日期型是不是使用
    convert(datetime,@char_time)命令?
      

  5.   

    convert(datetime,@char_time)
    cast(@char_time as datetime)
    都行!
      

  6.   

    select @Char_SQL='insert into TempData_'+@Char_Room+' (Card_No,Stu_No,Exp_No,location,Exp_Type,Exp_Price,Exp_Cost,Recorder_No,PriceType,Exp_Date,Exp_Time,Elapsed_Time,Room_No)
                                                                      values('+@Char_CardNo+','+@Char_StuNo+','+@Char_ExpNo+','+@Char_Location+','+@Char_Type+','+cast(@Float_ExpPrice as char(10))+','+cast(@Float_ExpCost as char(10))+','+@Char_TeacherNo+','+@Char_PriceType+',convert(datetime,''' +@curymd+''')'+',convert(datetime,'''+@curhour+'''),'+cast(@Float_ElaspedTime as char(1))+','+@Char_Room+')'
    为什么总是提示“datetime”附近有错误那?
    主要是Convert(datetime,'''+@curhour+''')这个数据,
    其他的都没问题,可是只要加上这个数据就提示错误!!!
      

  7.   

    @curhour 放的是什么歌时的数据?
      

  8.   

    哈哈.....少一个单引号:
    select @Char_SQL='insert into TempData_'+@Char_Room+' (Card_No,Stu_No,Exp_No,location,Exp_Type,Exp_Price,Exp_Cost,Recorder_No,PriceType,Exp_Date,Exp_Time,Elapsed_Time,Room_No)
                                                                      values('+@Char_CardNo+','+@Char_StuNo+','+@Char_ExpNo+','+@Char_Location+','+@Char_Type+','+cast(@Float_ExpPrice as char(10))+','+cast(@Float_ExpCost as char(10))+','+@Char_TeacherNo+','+@Char_PriceType+',convert(datetime,''' +@curymd+''')'+','convert(datetime,'''+@curhour+'''),'+cast(@Float_ElaspedTime as char(1))+','+@Char_Room+')'
      

  9.   

    哪里?你是说第二个convert前面吗?好像不是啊,我改了以后说convert有语法错误
      

  10.   

    j9988(j9988) :
    原来是不多的,加了就多了!
     Singal(落花流水) :
       你把@curhour PRINT出来看看,可能个是不对!
      

  11.   

    想起了,有可能是你的@Char_SQL定一得不够长,被截掉了
      

  12.   

    select @nowhour=convert(varchar(2),datepart(hour,getdate()))+':'+convert(varchar(2),datepart(minute,getdate()))
      

  13.   

    真的是,呵呵!看来我够笨的了!
    还有一个问题,就是数据@Char_Room
    这个数据作为一个外部变量,
    我有一个表Temp_+@Char_Room
    的,可是每次exec procedure proc_name 's101'进行插入时
    总是说
    “在此上下文中不允许使用 's101'。此处只允许使用常量、表达式或变量。不允许使用列名。”
      

  14.   

    语句本身是没问题,那就象Yang_说的,是字段变量问题。
    用 print @Char_SQL
    拿出来单独执行看一看
      

  15.   

    可能是你的变量类型问题,一般应该用VARCHAR,会自动去掉后面的空格,如果用CHAR,那必须每次字符串+都要用RTRIM.
    估计是你中间有空格的问题.
      

  16.   

    使用print可以正确的得到's101',可是就是不知道为什么不能插入使用
      

  17.   

    这样的测试对你可能有帮助:语句:
    declare @a char(10)
    declare @b varchar(10)
    declare @c varchar(20)
    select @a='ab',@b='ab'
    select @c=@a+'1'
    select @c
    select @c=@b+'1'
    select @c结果:(1 row(s) affected)
    (1 row(s) affected)                     
    -------------------- 
    ab        1          (1 row(s) affected)
    (1 row(s) affected)                     
    -------------------- 
    ab1                  (1 row(s) affected)
      

  18.   

    我知道你的意思,不过Room和Char_Room的类型和大小都是一样的,
    而且没有其他的付值操作,这个insert操作是在存储过程的最后使用的,在这之前
    Char_Room都能正确的得到's101'这个值的,现在我把所有的外部变量类型
    都改成varchar了,可是还是不行
      

  19.   

    还是前面的语句吗?
    改成这样事实:select @Char_SQL='insert into TempData_'+@Char_Room+' (Card_No,Stu_No,Exp_No,location,Exp_Type,Exp_Price,Exp_Cost,Recorder_No,PriceType,Exp_Date,Exp_Time,Elapsed_Time,Room_No)
                                                                      values('+@Char_CardNo+','+@Char_StuNo+','+@Char_ExpNo+','+@Char_Location+','+@Char_Type+','+cast(@Float_ExpPrice as char(10))+','+cast(@Float_ExpCost as char(10))+','+@Char_TeacherNo+','+@Char_PriceType+',convert(datetime,''' +@curymd+''')'+',convert(datetime,'''+@curhour+'''),'+cast(@Float_ElaspedTime as char(1))+','''+@Char_Room+''')'
      

  20.   

    print @Char_SQL
    把结果字符串复制出来单独执行,应该知道错在那
      

  21.   

    还是不对,你的Room_No字段应该是字符型的,所以必须加引号!
      

  22.   

    你是说在values里面的字符型吧?可是其他的不用都没什么关系啊?
      

  23.   

    你是说values里面的数值吗?
    其他的不加引号也可以用啊!