日期:2014-05-18  浏览次数:20554 次

求助:帮忙改个SQL语句
declare    
  @name   char(50),
  @birthday     datetime

set     @name   = 'Mary '
set     @birthday   = '1986-04-05 '


declare   @sql   varchar(8000)
set   @sql= '
select   distinct   top   10   Code
from   student  
where   Code   is   not   null
  and   Name= '+@name+ '  
and   Date=convert(varchar, '+@birthday+ ',111),

'
print   @sql

字符类型和时间类型转换不过来,帮忙改改,谢谢了

------解决方案--------------------
declare @sql varchar(8000)
set @sql= 'select distinct top 10 Code from student where Code is not null and Name= ' ' '+@name+ ' ' '
and Date=convert(varchar, ' ' '+@birthday+ ' ' ',111)
------解决方案--------------------
declare
@name char(50),
@birthday datetime

set @name = 'Mary '
set @birthday = '1986-04-05 '


declare @sql varchar(8000)
set @sql= '
select distinct top 10 Code
from student
where Code is not null
and Name= '+@name+ '
and Date=convert(varchar,Convert(datetime, ' ' '+Convert(nvarchar,@birthday)+ ' ' '),111),
'
print @sql

-- 这样试试


select convert(varchar,Convert(datetime, 'Apr 5 1986 12:00AM '),111)
--
1986/04/05