SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL query help


T-SQL query help

Author
Message
etirem
etirem
SSC Veteran
SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)

Group: General Forum Members
Points: 240 Visits: 517
Hi..
I have data the data in the below temp table(SQL query)
IF object_id('TempDB..#tTemp1') IS NOT NULL
DROP TABLE #tTemp1
CREATE TABLE #tTemp1(ID int, Name varchar(20))
INSERT INTO #tTemp1(ID, Name)
SELECT 1, 'NewYork'
UNION
SELECT 2, 'Virginia'
UNION
SELECT 3, 'London'
UNION
SELECT 4, 'Sydney'
UNION
SELECT 5, 'Dubai'

SELECT * FROM #tTemp1
DROP TABLE #tTemp1



I need the SQL query to get the output as:


Thanks..
Attachments
Image.png (138 views, 6.00 KB)
Jason-299789
Jason-299789
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4963 Visits: 3232
Its really a simple solution with all you need to do is add a column that conactinates the fixed string 'This city name is ' and the Name Column.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94373 Visits: 38955
Yep, just this easy:



IF object_id('TempDB..#tTemp1') IS NOT NULL
DROP TABLE #tTemp1
CREATE TABLE #tTemp1(ID int, Name varchar(20))
INSERT INTO #tTemp1(ID, Name)
SELECT 1, 'NewYork'
UNION
SELECT 2, 'Virginia'
UNION
SELECT 3, 'London'
UNION
SELECT 4, 'Sydney'
UNION
SELECT 5, 'Dubai'

SELECT ID, Name, 'The city name is ' + Name as Comment FROM #tTemp1
DROP TABLE #tTemp1




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
ard5karthick
ard5karthick
Old Hand
Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)

Group: General Forum Members
Points: 329 Visits: 162
Hi ,

You can get the desired result quiet easily...

The below mentioned code will solve your problem.

Select ID, Name, 'The City name is ' + Name Comment
From #tTemp1
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17715 Visits: 6431
Another way is like this (a computed column):


IF object_id('TempDB..#tTemp1') IS NOT NULL
DROP TABLE #tTemp1

CREATE TABLE #tTemp1(ID int, Name varchar(20)
,Comment AS ('The city name is ' + Name))
INSERT INTO #tTemp1(ID, Name)
SELECT 1, 'NewYork'
UNION
SELECT 2, 'Virginia'
UNION
SELECT 3, 'London'
UNION
SELECT 4, 'Sydney'
UNION
SELECT 5, 'Dubai'

SELECT * FROM #tTemp1
DROP TABLE #tTemp1





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search