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


Creating a new column and inserting data on it from an existing column


Creating a new column and inserting data on it from an existing column

Author
Message
SQLTestUser
SQLTestUser
Say Hey Kid
Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)

Group: General Forum Members
Points: 687 Visits: 548
Is there any way we can create a new column based on the results of a select. i am trying to create a new column that would store users address that is currently stored in his name as

name (nvarchar 500)

"this,guy (Chicago)"

into three new columns as

firstname lastname address
guy this newyork

thanks,
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: 17333 Visits: 6431
Something like this perhaps?


WITH SampleData (MyString) AS
(
SELECT 'this,guy (Chicago)'
)
SELECT MyString
,C1=RTRIM(MAX(CASE WHEN ItemNumber = 1 THEN Item END))
,C2=RTRIM(MAX(CASE WHEN ItemNumber = 3 THEN Item END))
,C3=RTRIM(MAX(CASE WHEN ItemNumber = 5 THEN Item END))
FROM SampleData a
CROSS APPLY dbo.PatternSplitCM(MyString, '[a-zA-Z]') b
WHERE [Matched]=1
GROUP BY MyString;




PatternSplitCM can be found in the 4th article in my signature links.

You may have to mess with it a bit if your actual data is not as clean as your sample.


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
super48
super48
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 530
CREATE TABLE addrname(addnam varchar(50))
insert into addrname
values('roshan,hrithik(india)')

go

create table separated(firstname varchar(50),lastname varchar(50),address varchar(50))

go

insert into separated
select
SUBSTRING(addnam,PATINDEX('%,%',addnam)+1,PATINDEX('(%',addnam)-1-PATINDEX('%,%',addnam))as firstname,
SUBSTRING(addnam,0,PATINDEX('%,%',addnam))as lastname,

SUBSTRING(addnam,PATINDEX('%(%',addnam)+1,PATINDEX('%)%',addnam)-1-PATINDEX('%(%',addnam))as address
from addrname


it looks messy check if it works for you
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