• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 21227
  • Last Modified:

Convert from Int to String

How do i convert a Int to a string.

I am trying to append a Integer value in a query.

SET @SQL = 'UPDATE status_table SET status' + CONVERT(varchar(10),@statusCount) + ' = ''' + @Old_status + ''''
0
bender007
Asked:
bender007
  • 4
  • 3
  • 2
  • +4
2 Solutions
 
rmacfadyenCommented:
That seems to work for me. Are you getting an error of some sort (i.e. maybe I don't understand your question)?

Btw... if @OldStatus contains a single quote your final SQL will be incorrect.

Rob
0
 
trainsdseCommented:
I agree with rmacfadyen, you might try casting instead though.  And here's how to fix @Old_Status too.

SET @SQL = 'UPDATE status_table SET status' + cast(@statusCount as varchar(10)) + ' = ''' + Replace(@Old_status,'''','''''') + ''''
0
 
appariCommented:

what is the datatype of @Old_status ? if it is int try something like this

SET @SQL = 'UPDATE status_table SET status' + CONVERT(varchar(10),@statusCount) + ' = ''' + convert(varchar,@Old_status) + ''''
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
karthikramCommented:
@old_status is varchar and StatusCount is int
0
 
appariCommented:
then your sql should be ok, are you getting any error?
post the error message here
0
 
HuyBDCommented:
If @Old_status is char or varchar

SET @SQL = 'UPDATE status_table SET status' + CONVERT(varchar(10),@statusCount) + ' = ''' + ltrim(rtrim(@Old_status)) + ''''

if @Old_status is integer

SET @SQL = 'UPDATE status_table SET status' + CONVERT(varchar(10),@statusCount) + ' = ''' + CONVERT(varchar(10),@Old_status) + ''''
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
bender007,
> I am trying to append a Integer value in a query.

You can use  CAST(urIntegerValue  as Varchar(10))
0
 
karthikramCommented:
Error:


Msg 207, Level 16, State 1, Line 1
Invalid column name 'testuser'.
0
 
bender007Author Commented:
Error i get :
Msg 207, Level 16, State 1, Line 1
Invalid column name 'testuser'.
0
 
rmacfadyenCommented:
For petesakes... can you post the entire query? I bet if you search your query you will find the character sequence t, e, s, t, u, s, e, r... maybe not on line 1... but somewhere in there. From Query Analyzer's EDIT menu select FIND, type t, e, s, t, u, s, e, r and click the FIND NEXT button. This will bring you to the first occurrence... warning there may be more than one occurance. Be prepared to click the FIND NEXT button again!

Btw... if you meant to use a variable named @TestUser this error means you forgot the @ character somewhere.
0
 
appariCommented:

but there is no 'testuser' column in your sql.
is the sql you are trying to execute is
SET @SQL = 'UPDATE status_table SET status' + CONVERT(varchar(10),@statusCount) + ' = ''' + @Old_status + ''''
or something different?

check the tables used in the error sql and make sure you have 'testuser' column in that table. or post your complete code(i think you are fixing some sp or batch sql)

are you using two ids bender007 and karthikram ?

0
 
bender007Author Commented:
DECLARE @statsCount int

SET @statsCount= (Select statusCount FROM status_table WHERE UserID = @user_id)

UPDATE status_table SET status' + CONVERT(varchar(10),@statusCount)  + '= @Old_status


This is what i was using initially.

testuser is a value in the table under UserID.


Yes I posted that message from a friends ID by mistake.

Thanks
0
 
rmacfadyenCommented:
What was the result of using the FIND function I suggested?
0
 
appariCommented:
try this post the results

DECLARE @statsCount int

select @statsCount as statcnt, @user_id uid;
SET @statsCount= (Select statusCount FROM status_table WHERE UserID = @user_id);
select @statsCount as statcnt, @user_id uid;
SET @SQL = 'UPDATE status_table SET status' + CONVERT(varchar(10),@statusCount) + ' = ''' + @Old_status + ''''

select @sql as sql;
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 4
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now