Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
st3ady
Starting Member
2 Posts |
Posted - 2006-07-20 : 16:44:52
|
How could I go about doing the following:I have a column with lets sayAHA1AHA2AHA3AHE1AHE2ARL1ARL2How could I make a column next to it that is basically the same but cuts off anything after the third letter? So this new column would look like:AHAAHAAHAAHEAHEARLARLThanks! |
|
|
tonymorell10
Yak Posting Veteran
90 Posts |
Posted - 2006-07-20 : 17:13:36
|
| select col1, left(col1,3)... |
 |
|
|
st3ady
Starting Member
2 Posts |
Posted - 2006-07-20 : 17:15:53
|
awesome, thanks tony! |
 |
|
|
Tahsin
Starting Member
34 Posts |
Posted - 2006-07-20 : 17:17:01
|
| Since you are trying to update a column right next to what you currently have, you can use the update statement directly: CREATE TABLE #X(COL1 VARCHAR(4) NULL,COL2 VARCHAR(4) NULL)goINSERT INTO #X (COL1) VALUES ('AHA1');INSERT INTO #X (COL1) VALUES ('AHA2');INSERT INTO #X (COL1) VALUES ('AHA3');INSERT INTO #X (COL1) VALUES ('AHE1');INSERT INTO #X (COL1) VALUES ('AHE2');INSERT INTO #X (COL1) VALUES ('ARL1');INSERT INTO #X (COL1) VALUES ('ARL2');goSELECT * FROM #XgoUPDATE #XSET COL2 = LEFT(COL1,3) SELECT * FROM #XgoDROP TABLE #Xgo |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-21 : 05:51:33
|
quote: Since you are trying to update a column right next to what you currently have, you can use the update statement directly:
Or you can created a computed column. Although I suspect you only wanted to add a column to your results, not to your table, so I'm pretty sure it's irrelevant...--dataCREATE TABLE #X (COL1 VARCHAR(4) NULL)INSERT INTO #X (COL1) VALUES ('AHA1');INSERT INTO #X (COL1) VALUES ('AHA2');INSERT INTO #X (COL1) VALUES ('AHA3');INSERT INTO #X (COL1) VALUES ('AHE1');INSERT INTO #X (COL1) VALUES ('AHE2');INSERT INTO #X (COL1) VALUES ('ARL1');INSERT INTO #X (COL1) VALUES ('ARL2');--calculationalter table #x add COL2 as LEFT(COL1, 3)select * from #X/*resultsCOL1 COL2 ---- ---- AHA1 AHAAHA2 AHAAHA3 AHAAHE1 AHEAHE2 AHEARL1 ARLARL2 ARL*/--tidy updrop table #XRyan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|
|
|