Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I am looking for to print two variables of same datatype I have something like this

declare @one int ,@two int
set @one=1
set @two=2
print @one+@two

obviously it will give a result 3 but I need result 1 2

On search I got to convert the any one of these variable and come up this query.

print convert(varchar(10),@one)+@two 

but the result was same 3!! What should I do,to get the result? NB :I am using sql server

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You were nearly there... Just put a space between them (as that's how you want them to be formatted):

declare @one int ,@two int
set @one=1
set @two=2
print convert(varchar(10),@one)+' '+convert(varchar(10),@two)

Different datatypes:

declare @one int ,@two varchar(20)
set @one=1
set @two='zomgwtfbbq'
print convert(varchar(10),@one)+' '+@two;
share|improve this answer
    
Thanks phil ,but here you have converted both ,it that means i can't do with one datatype as int and the other as varchar –  Biju jose Oct 12 '12 at 10:56
    
@Bijujose - Your question is about two integers. –  Martin Smith Oct 12 '12 at 10:57
    
I was wondering myself... Edited the answer with an example anyway –  Phil Oct 12 '12 at 10:58
2  
@Bijujose Read the question title again: "Print variable of same datatype" –  Phil Oct 12 '12 at 11:00
    
@phil-So if the datatypes are same like int you need to convert it into varchar and if its different change any one of them to varchar am I right OR you should always convert all of them to varchar type in print command(except varchar datatype)? –  Biju jose Oct 12 '12 at 11:10

You can use RAISERROR instead of PRINT. With a severity of 0 the two are equivalent but there is some primitive string formatting functionality available as below.

RAISERROR('%d %d',0,1,@one, @two)
share|improve this answer
    
,it works fine,is there any good reason using raiserror instead of print –  Biju jose Oct 12 '12 at 10:58
    
Because print doesn't support the placeholder %d syntax (you would use %s for a string BTW) –  Martin Smith Oct 12 '12 at 10:58
1  
You can also use WTIH NOWAIT so that your PRINT commands aren't subject to Management Studio's output buffer limitations (in longer scripts you won't see your PRINT commands occurring in real time, the output will barf out a bunch of them at a time). –  Aaron Bertrand Oct 12 '12 at 14:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.