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
 General SQL Server Forums
 New to SQL Server Programming
 converting the varchar value

Author  Topic 

kurtgr
Starting Member

25 Posts

Posted - 2009-07-26 : 17:49:14
Hi All

I am have a problem which i seem not to be able to solve. below is my code

DECLARE @EmployeeId INT, @EmployeeTypeId CHAR(1), @GenderId CHAR(1),
@Age INT, @WageTypeId Char(1), @DaysEmployed INT
@Command NCHAR(1000), @Filter VARCHAR(200)

SET @Command = 'SELECT LeaveTypeName,LeaveTypeId,WageTypeEligible, ' +
'EmployeeTypesEligible,MinDaysOfEmploymentForEligibility,AgeRange '+
'FROM #Temp1 T INNER JOIN AgeRange A ' +
'ON T.AgeRange = A.AgeId '

SET @Filter = ' WHERE MinDaysOfEmploymentForEligibility <= ' +@DaysEmployed

SET @Command = RTRIM(@Command) + ' ' + LTRIM(@Filter)

EXEC( @Command )

I am getting this error
Conversion failed when converting the varchar value ' WHERE MinDaysOfEmploymentForEligibility <= ' to data type int.

Just to note MinDaysOfEmploymentForEligibility is a INT field in the table

Thanks in advance for ur help

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-07-26 : 17:58:34
quote:
SET @Filter = ' WHERE MinDaysOfEmploymentForEligibility <= ' +@DaysEmployed


SET @Filter = ' WHERE MinDaysOfEmploymentForEligibility <= ' + Convert(varchar(500), @DaysEmployed)
Go to Top of Page

kurtgr
Starting Member

25 Posts

Posted - 2009-07-26 : 18:03:55
Thanks very much but could u say why it did not work the way i had it
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-07-26 : 18:30:16
because @Filter is a string. You're trying to append an integer value to a string. Need to explicitly cast the int to varchar to build the string with int in it.
Go to Top of Page

kurtgr
Starting Member

25 Posts

Posted - 2009-07-26 : 18:31:11
Ok understood and Thanks again
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-07-26 : 18:33:49
You're welcome. Glad to help
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-07-27 : 02:35:14
www.sommarsko.se/dynamic_sql.html

Madhivanan

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

- Advertisement -