Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 hwo to fix this

Author  Topic 

jeff06
Posting Yak Master

166 Posts

Posted - 2009-11-11 : 14:36:07
I have code:

declare @d varchar(7)
select @d='2005/09'

EXEC('
select top 10 Claim_YearMonth
from tbl
where Claim_YearMonth>' +@d

)

Claim_YearMonth is a varchar(7) like 'yyyy/mm'


I got error.

Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value '2004/03' to data type int.

how I can fix this problem?
Thanks.
Jeff

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-11 : 14:42:56
declare @d varchar(7)
select @d='2005/09'

EXEC('
select top 10 Claim_YearMonth
from tbl
where Claim_YearMonth>''' +@d+''''
)


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-11 : 14:43:08
[code]
declare @d char(7)
set @d = '2009/12'

exec('
select top 10 Claim_YearMonth
from tbl
where Claim_YearMonth > ''' + left(@d, 4) + right(@d, 2) + '01'''
)
[/code]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-12 : 01:47:11
Why are you using dynamic SQL?


declare @d varchar(7)
select @d='2005/09'

select top 10 Claim_YearMonth
from tbl
where Claim_YearMonth+'/01'>@d+'/01'


It is better you always use datetime datatype to store dates

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -