Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

string with int Expand / Collapse
Author
Message
Posted Sunday, November 25, 2012 6:43 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, December 12, 2012 8:04 AM
Points: 297, Visits: 354
pretty basic so appreciate the help here,getting conversion error , what is the way to have int variable in the string?

declare @abc int
set @abc =1
declare @cmd varchar(100)

select @cmd='select'+@abc+'hello'
print @cmd



Post #1388423
Posted Sunday, November 25, 2012 6:50 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, May 19, 2015 5:50 PM
Points: 249, Visits: 2,410
This is what i do with my dynamic sql.

CONVERT(varchar(20),@abc)

so it would be
declare @abc int
set @abc =1
declare @cmd varchar(100)

select @cmd='select'+CONVERT(varchar(20),@abc)+'hello'
print @cmd

just remember to change varchar(20) to something sensible for your data
Post #1388424
Posted Sunday, November 25, 2012 7:05 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, December 12, 2012 8:04 AM
Points: 297, Visits: 354
thank you , i was trying to use cast but that did not working , thanks again


Post #1388425
Posted Sunday, November 25, 2012 7:08 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, May 19, 2015 5:50 PM
Points: 249, Visits: 2,410
This was my first answered question so thank you
Post #1388426
Posted Monday, November 26, 2012 3:18 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, April 22, 2015 4:35 AM
Points: 2,920, Visits: 5,354
CELKO (11/25/2012)

...
CAST(@abc AS VARCHAR(10)) should work and it much preferred over the old 1970's Sybase CONVERT string function...


CAST and CONVERT are similar but not the same. CAST in T-SQL offers limited (basic) data conversion functionality. I cannot see why someone would prefer one over another... It's not matter of preference, it is matter of suitability. Where CAST functionality is sufficient, you use CAST, where it's not enough (requires specific format key/switch) - use CONVERT.
For example, (especially for standards-loving J.C.) using CAST you can not explicitly convert DATETIME into string in ISO format...


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1388494
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse