Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Creating a new column and inserting data on it from an existing column Expand / Collapse
Posted Monday, September 23, 2013 10:51 PM


Group: General Forum Members
Last Login: Monday, September 12, 2016 3:14 PM
Points: 171, Visits: 542
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


Post #1497649
Posted Monday, September 23, 2013 11:22 PM

Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Wednesday, February 24, 2016 6:28 AM
Points: 3,977, Visits: 6,431
Something like this perhaps?

WITH SampleData (MyString) AS
SELECT 'this,guy (Chicago)'
,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
Post #1497651
Posted Tuesday, September 24, 2013 5:20 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 12:04 AM
Points: 83, Visits: 500
CREATE TABLE addrname(addnam varchar(50))
insert into addrname


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


insert into separated
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
Post #1497773
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse