Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the value with desired text in TSQL using sql server 2005

I want to select a value from my customers datatable. I have been asked this question in an interview.

Select cust_Name from customers where cust_Id=5;

will result as Naresh.

Now I want to print the value as

Customer Name is Naresh

How can I print the value like this.Thank you

like image 431
Learner Avatar asked May 15 '13 04:05

Learner


People also ask

How do I print the value of a variable in SQL?

Usually, we use the SQL PRINT statement to print corresponding messages or track the variable values while query progress. We also use interactions or multiple loops in a query with a while or for a loop. We can also use the SQL PRINT statement to track the iteration.


2 Answers

You can use concat()

Select concat('Customer Name is',cust_Name) as values from customers where cust_Id=5;

Click here to learn more about concat()

like image 186
Ishaan007 Avatar answered Sep 30 '22 15:09

Ishaan007


Select 'Customer Name is ' + cust_Name from customers where cust_Id=5;

Or

Declare @CustomerName varchar(50)
Select @CustomerName = cust_Name from customers where cust_Id=5;
Print 'Customer Name is ' + @CustomerName ;
like image 44
Shahid Iqbal Avatar answered Sep 30 '22 14:09

Shahid Iqbal