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


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;
  • 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
  • @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)
  • ,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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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