Split 1 column into multiple columns
Hi
How can we acheive this in Sql:
Col 1
abc def ghi jkl
convert to:
Col 1
Col 2
Col 3
Col 4
abc
def
ghi
jkl
Royal Thomas
The best approach is to NOT split them out into individual columns... It violates the 1st normal form...
Under first normal form, all occurrences of a record[sic] type must contain the same number of fields[sic].
First normal form excludes variable repeating fields[sic] and groups.
The better approach is to split them into rows, in their own table, with a foreign key reference back to the table you are pulling them away from.
If you check my 1st post you'll see that I split the string with a function. That same a function can be used to populate the new table.
Something along these lines...
IF OBJECT_ID('tempdb..#BadOldDesign') IS NOT NULL
DROP TABLE #BadOldDesign;
CREATE TABLE #BadOldDesign (
ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
ListOfCodes VARCHAR(100) NOT NULL
INSERT #BadOldDesign (ListOfCodes) VALUES
('abc htr tbj yfd hjk mth'),
('bbb hyt tbj vyk hjk htd'),
('vvv nud'),
('yyy hyn tbj bdr '),
('htr htf tbj yfd hjk mko'),
('kio bhn tbj bjd hjk'),
('byd loj tbj yfd hjk cds');
SELECT * FROM #BadOldDesign bod
IF OBJECT_ID('tempdb..#ShinyNewTable') IS NOT NULL
DROP TABLE #ShinyNewTable;
SELECT
bod.ID,
sc8.ItemNumber,
sc8.Item
INTO #ShinyNewTable
FROM
#BadOldDesign bod
CROSS APPLY dbo.SplitCSVToTable8K(bod.ListOfCodes, ' ') sc8;
ALTER TABLE #ShinyNewTable ALTER COLUMN id INT NOT NULL;
GO
ALTER TABLE #ShinyNewTable ALTER COLUMN ItemNumber INT NOT NULL;
GO
ALTER TABLE #ShinyNewTable ADD CONSTRAINT pk_ShinyNewTable PRIMARY KEY CLUSTERED (ID, ItemNumber);
SELECT * FROM #ShinyNewTable snt
The dbo.SplitCSVToTable8K function that I'm using can be found here... http://www.sqlservercentral.com/articles/Tally+Table/72993/
HTH,
Jason
Jason Long
Similar Messages
-
How can I separate one column into multiple column?
How can I separate one column into multiple column?
This is what I have:
BUYER_ID ATTRIBUTE_NAME ATTRIBUTE_VALUE
0001 PHONE_NUMBER 555-555-0001
0001 EMAIL [email protected]
0001 CURRENCY USD
0002 PHONE_NUMBER 555-555-0002
0002 EMAIL [email protected]
0002 CURRENCY USD
0003 PHONE_NUMBER 555-555-0003
0003 EMAIL [email protected]
0003 CURRENCY CAD
This is what I would like to have:
BUYER_ID PHONE_NUMBER EMAIL CURRENCY
0001 555-555-0001 [email protected] USD
0002 555-555-0002 [email protected] USD
0003 555-555-0003 [email protected] CAD
Any help would be greatly appreciated.This is another solution. Suppose your actual table's name is test(which has the redundant data). create a table like this:
CREATE TABLE test2 (BUYER_ID number(10),PHONE_NUMBER varchar2(50),EMAIL varchar2(50),CURRENCY varchar2(50));
then you will type this procedure:
declare
phone_number_v varchar2(50);
EMAIL_v varchar2(50);
CURRENCY_v varchar2(50);
cursor my_test is select * from test;
begin
for my_test_curs in my_test loop
select ATTRIBUTE_VALUE INTO phone_number_v from test
where person_id=my_test_curs.person_id
and attribute_name ='PHONE_NUMBER';
select ATTRIBUTE_VALUE INTO EMAIL_v from test
where person_id=my_test_curs.person_id
and attribute_name ='EMAIL';
select ATTRIBUTE_VALUE INTO CURRENCY_v from test
where person_id=my_test_curs.person_id
and attribute_name ='CURRENCY';
INSERT INTO test2
VALUES (my_test_curs.person_id,phone_number_v,EMAIL_v,CURRENCY_v);
END LOOP;
END;
Then you will create your final table like this:
create table final_table as select * from test2 where 1=2;
After that write this code:
INSERT ALL
into final_table
SELECT DISTINCT(BUYER_ID),PHONE_NUMBER,EMAIL,CURRENCY
FROM TEST2;
If you have a huge amount of data in your original table this solution may take a long time to do what you need. -
To split a column into multiple columns
Hi,
I have column and i want to split it into three columns as shown below given inputs and required outputs. I have written this query to get P_1, and p_2 but dont know how to get p_3.
SELECT ADDRESS,SUBSTR(ADDRESS,1, INSTR(ADDRESS,' ')-1) P_1
,SUBSTR(ADDRESS, instr(address,' ',-1)+1) P_2
FROM TT
Input:
ADDRESS
1 AVENUE OF THE AMERICAS
2 NORTH CAROLINE STREET EAST
236 TURPENTINE ROAD COOPER CREEK
REQUIRED OUTPUT
P_1 P_2 P_3
1 AMERICAS AVENUE OF THE
2 EAST NORTH CAROLINE STREET
236 CREEK TURPENTINE ROAD COOPER
Thanks in advanceIf you're on 10g or 11g you can do something like this ...
SQL*Plus: Release 10.2.0.1.0 - Production on Tue Mar 4 21:18:07 2008
Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
column p_1 format a3
column p_2 format a10
column p_3 format a30
with data as
select '1 AVENUE OF THE AMERICAS' as ADDRESS from dual union all
select '2 NORTH CAROLINE STREET EAST' as ADDRESS from dual union all
select '236 TURPENTINE ROAD COOPER CREEK' as ADDRESS from dual
select
regexp_replace( address, '^([^ ]+) (.+?) ([^ ]+)$', '\1' ) as p_1 ,
regexp_replace( address, '^([^ ]+) (.+?) ([^ ]+)$', '\3' ) as p_2 ,
regexp_replace( address, '^([^ ]+) (.+?) ([^ ]+)$', '\2' ) as p_3
from
data
P_1 P_2 P_3
1 AMERICAS AVENUE OF THE
2 EAST NORTH CAROLINE STREET
236 CREEK TURPENTINE ROAD COOPER
3 rows selected.
-- or try this shorter version --
with data as
select '1 AVENUE OF THE AMERICAS' as ADDRESS from dual union all
select '2 NORTH CAROLINE STREET EAST' as ADDRESS from dual union all
select '236 TURPENTINE ROAD COOPER CREEK' as ADDRESS from dual
select
regexp_replace( address, '^([^ ]+).*', '\1' ) as p_1 ,
regexp_replace( address, '.*?([^ ]+)$', '\1' ) as p_2 ,
regexp_replace( address, '^([^ ]+) (.+?) ([^ ]+)$', '\2' ) as p_3
from
data
P_1 P_2 P_3
1 AMERICAS AVENUE OF THE
2 EAST NORTH CAROLINE STREET
236 CREEK TURPENTINE ROAD COOPER
3 rows selected.See SQL Snippets: SQL Features Tutorials - Regular Expressions if you're unfamiliar with regular expressions.
If you can't / don't want to use regular expressions then you'll need to settle for a more boring approach like this one.
with data as
select '1 AVENUE OF THE AMERICAS' as ADDRESS from dual union all
select '2 NORTH CAROLINE STREET EAST' as ADDRESS from dual union all
select '236 TURPENTINE ROAD COOPER CREEK' as ADDRESS from dual
select
substr( address, 1, instr( address, ' ' ) - 1 ) as p_1 ,
substr( address, instr( address, ' ', - 1 ) + 1 ) as p_2 ,
substr
( address,
instr( address, ' ', + 1 ) + 1,
instr( address, ' ', - 1 ) - instr( address, ' ' ) - 1
) as p_3
from
data
P_1 P_2 P_3
1 AMERICAS AVENUE OF THE
2 EAST NORTH CAROLINE STREET
236 CREEK TURPENTINE ROAD COOPER--
Joe Fuda
SQL Snippets -
Split single column into multiple column using sql /plsql
create table test (name varchar2(255);
insert into test values('DH RED 20 12/10 10 2 ');
insert into test values('PM STUD 20 15/10 20 29.55' );
insert into test values('LS MENTHOl FILTER ASC 18/70 60 240.66');
insert into test values('WINSTON WHITE CLASSIC 13 18/70 60 240.66');
My Output should be like as below in other table :
create table test_result (brand varchar2(255),packet varchar2(50),amount varchar2(25),total varchar2(25));
BRAND PACKET AMOUNT TOTAL
DH RED 20 12/10 10 2
PM STUD 20 15/10 20 29.55
LS MENTHOl FILTER ASC 18/70 60 240.66
WINSTON WHITE CLASSIC 13 18/70 60 240.66can you please help me to solve this issue
thanks in advance
Edited by: A on Apr 21, 2012 11:33 PM
Edited by: A on Apr 21, 2012 11:34 PM
Edited by: A on Apr 21, 2012 11:34 PMh4. # Database should be 10g. If version is below 10g query need to be modified as per string format.
h4. # Split string into two parts by '/'. First part contain brand + packet(1), Second part contain packet(2) + amount + total
h4. # Your brand name can be of any length.
String Format* : <Brand Name><space><packet(1)>/<packet(2)><space><amount><space><total>
SELECT name,
REGEXP_SUBSTR(first_part,'.+[[:space:]]',1,1) brand,
REGEXP_SUBSTR(first_part,'[^[:space:]]+/',1,1) || REGEXP_SUBSTR(second_part,'[^[:space:]]+[[:space:]]',1,1) packet,
REGEXP_SUBSTR(second_part,'[^[:space:]]+[[:space:]]',1,2) amount,
REGEXP_SUBSTR(second_part,'[^[:space:]]+$',1) total
FROM
(SELECT trim(name) name,
trim(substr(name,1, instr(name,'/'))) first_part,
trim(substr(name,instr(name,'/')+1)) second_part
FROM test )
{code} -
Spliting the column Into multiple column diff secenario?
I have one table say tab_det having two columns
Tab_det
Tab_tablename varchar2(50),
Tab_tablename varchar2(50)
Select * from tab_det;
Tab_tablename ----> Tab_tablename
------------------ ------> ---------------------
Emp -------> empno,empname,dept
Emp_acct ------> empno,accno,accname,age
I want to split the each column name from <Tab_tablename > because it contains all the
Column name with comma separated and as a single value
I want to split the column name for each < Tab_tablename> Corresponding to
< Tab_tablename>
And I want to pass the column names dynamically and the output need to be written into the File
The Required Output In File
Empno empname dept
1 aaa cs
2 bbb ec
3 ccc ec
Empno accno accname age
1 12 aaa 34
2 13 bbb 36
3 14 ccc 38
4 14 ddd 40Then do so.
Use INSTR to identify the locations of the commas and SUBSTR to separate the pieces.
We are not here to do your school work for you. -
Convert 2 columns into multiple column table
Hi,
I have table with two columns. See below. I need to create a kind of matrix presentation of the table, so that values in row B become column titles, see second table. Amount of data is too big to create this manually.
TC1
Req2
TC2
Req1
TC3
Req0
TC4
Req1
TC5
Req2
TC6
Req3
TC7
Req4
TC8
Req5
TC9
Req6
TC10
Req7
TC11
Req8
TC12
Req9
TC13
Req10
TC14
Req11
TC15
Req12
TC16
Req7
TC17
Req8
TC18
Req3
TC19
Req13
TC20
Req14
TC21
Req15
TC22
Req16
Req1
Req2
Req3
Req4
Req5
Req6
Req7
Req8
TC1
X
TC2
X
TC3
X
TC4
X
TC5
TC6
X
X
TC7
X
TC8
TC9
X
X
TC10
TC11
X
TC12
X
TC13
X
X
TC14
X
X
TC15
X
TC16
TC17
X
TC18
X
TC19
TC20
X
X
x
TC21
X
TC22
X
Any suggestions how to proceed are greatly appreciated!
Question: Would Excel's PivotTable do the trick? No scripting needed?Hi and thanks for your reply! I added leading zeros. I tried the formula and the result looks like this from my first sample:
Req01
Req02
Req03
Req04
Req05
Req06
Req07
Req08
Req09
Req10
Req11
Req12
Req13
Req14
Req15
Req16
TC1
x
TC2
x
TC3
TC4
TC5
TC6
x
TC7
x
TC8
x
TC9
x
TC10
x
TC11
x
TC12
x
TC13
x
TC14
x
TC15
x
TC16
TC17
TC18
TC19
x
TC20
x
TC21
x
TC22
x
So row value remain empty if column value has earlier been met. Eg. TC17. Any ideas? -
How can I Make one column into Multiple columns
Id
SCM-01-015
SCM-01-020
SFA-02-021
SFC-02-042
STB-03-035
STP-04-167
SVF-06-150
I want the output like this
Id 01 02 03 04 06
SCM-01-015 5
SCM-01-020 10
SFA-02-021 15
SFC-02-042 15
STB-03-035 20
STP-04-167 50
SVF-06-150 5
thank you in advanceHi,
801668 wrote:
Id
SCM-01-015
SCM-01-020
SFA-02-021
SFC-02-042
STB-03-035
STP-04-167
SVF-06-150
I want the output like this
Id 01 02 03 04 06
SCM-01-015 5
SCM-01-020 10
SFA-02-021 15
SFC-02-042 15
STB-03-035 20
STP-04-167 50
SVF-06-150 5
thank you in advanceWhenever you post formatted text (such as query output) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after formatted text, to preserve spacing.
Do you want output something like this, which is from the scott.emp table?ENAME ANALYST CLERK MANAGER PRESIDENT SALESMAN
ALLEN 1600
JONES 2975
FORD 3000
CLARK 2450
MILLER 1300
SMITH 800
WARD 1250
MARTIN 1250
SCOTT 3000
TURNER 1500
ADAMS 1100
BLAKE 2850
KING 5000
JAMES 950
(ALLEN is a SALESMAN, JONES is a MANAGER, ..., JAMES is a CLERK.)
If so, do a pivot, somehting like this:SELECT ename
, MIN (CASE WHEN job = 'ANALYST' THEN sal END) AS analyst
, MIN (CASE WHEN job = 'CLERK' THEN sal END) AS clerk
, MIN (CASE WHEN job = 'MANAGER' THEN sal END) AS manager
, MIN (CASE WHEN job = 'PRESIDENT' THEN sal END) AS president
, MIN (CASE WHEN job = 'SALESMAN' THEN sal END) AS salesman
FROM scott.emp
GROUP BY ename
Always say which version of Oracle you're using.
The query above will work in Oracle 8.1 or higher, but starting in Oracle 11 there's a better way: SELECT ... PIVOT. -
Split column into multiple text and number columns
I'm trying to figure out how to split this column into multiple columns with power query. One column for the company name/person name, one for the address, one for the zip. Some of the addresses have a three or four digit code before the address, which
I would like in its own column too. It's the 170 on the lastname, firstname line. Does anyone have any pointers on this? I'm familiar with PQ advanced editor, but struggling with this one.
COMPANY INC. 195 MAIN ST MYCITY ST 12345
LASTNAME, FIRSTNAME 170 477 ANY STREET CIRCLE MYCITY ST 12345
Thanks for your help!HI Gil,
We have column with more than one numbers separated by space or comma or semicolon.
We need to add the row for each number by keeping all other column value same.
Here is a original table
Col1
Col2
Col3
A
B
11 22,33 44; 55
C
D
10 20
and expected output should be
Col1
Col2
Col3
A
B
11
A
B
22
A
B
33
A
B
44
A
B
55
C
D
10
C
D
20
Please let us know the best way to solve this... -
Splitting field into two columns
HI,
I'm told it is possible to have a field be split up into multiple columns in the details section of a report rather than having it listed in a single column. How can this be accomplished? I've heard it has something to do with subreports.
For instance, instead of this:
Bob
Jane
Sally
Barbara
Jim
Dave
It could look like this (columns not lined up in this example, not sure how to do it on this forum):
Bob Jane
Sally Barbara
Jim Dave
ThanksNo need to use subreports
In section expert for the deatils section check box - Format with multiple columns
A new tab appear - Layout - Define width to set up number of columns required.
Ian -
Split one column value and insert into multiple columns
hi
am new to plsql .
i want to split a characters from one column and insert into multiple columns
i tried used substr function the symbol ',' vary his place dynamically ,so i can't apply substr function.
for eg: before split
col1 :
col2 :
col3 :
col4 :
colu5: adsdf,fgrty,erfth,oiunth,okujt
after split
col1 :adsd
col2 :fgrty
col3 :erfth
col4 :oiunth
col5 : adsdf,fgrty,erfth,oiunth,okujt
can anyone help me
thanks
Edited by: 800324 on Dec 23, 2010 8:28 AM
Edited by: 800324 on Dec 23, 2010 8:36 AMHow about:
SQL> create table t
2 (col1 varchar2(30)
3 ,col2 varchar2(30)
4 ,col3 varchar2(30)
5 ,col4 varchar2(30)
6 ,col5 varchar2(30)
7 );
Table created.
SQL> insert into t (col5) values ('adsdf,fgrty,erfth,oiunth,okujt');
1 row created.
SQL> insert into t (col5) values ('x,y');
1 row created.
SQL> insert into t (col5) values ('a,b,c,d');
1 row created.
SQL> select * from t;
COL1 COL2 COL3 COL4 COL5
adsdf,fgrty,erfth,oiunth,okujt
x,y
a,b,c,d
3 rows selected.
SQL>
SQL> merge into t a
2 using ( with t1 as ( select col5||',' col5
3 from t
4 )
5 select substr(col5, 1, instr(col5, ',', 1, 1)-1) col1
6 , substr(col5, instr(col5, ',', 1, 1)+1, instr(col5, ',', 1, 2)- instr(col5, ',', 1, 1)-1) col2
7 , substr(col5, instr(col5, ',', 1, 2)+1, instr(col5, ',', 1, 3)- instr(col5, ',', 1, 2)-1) col3
8 , substr(col5, instr(col5, ',', 1, 3)+1, instr(col5, ',', 1, 4)- instr(col5, ',', 1, 3)-1) col4
9 , rtrim(col5, ',') col5
10 from t1
11 ) b
12 on ( a.col5 = b.col5 )
13 when matched then update set a.col1 = b.col1
14 , a.col2 = b.col2
15 , a.col3 = b.col3
16 , a.col4 = b.col4
17 when not matched then insert (a.col1) values (null);
3 rows merged.
SQL> select * from t;
COL1 COL2 COL3 COL4 COL5
adsdf fgrty erfth oiunth adsdf,fgrty,erfth,oiunth,okujt
x y x,y
a b c d a,b,c,d
3 rows selected.
SQL> Assuming you're on 9i... -
Split flat file column data into multiple columns using ssis
Hi All, I need one help in SSIS.
I have a source file with column1, I want to split the column1 data into
multiple columns when there is a semicolon(';') and there is no specific
length between each semicolon,let say..
Column1:
John;Sam;Greg;David
And at destination we have 4 columns let say D1,D2,D3,D4
I want to map
John -> D1
Sam->D2
Greg->D3
David->D4
Please I need it ASAP
Thanks in Advance,
RH
sqlImports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
Imports System.IO
Public Class ScriptMain
Inherits UserComponent
Private textReader As StreamReader
Private exportedAddressFile As String
Public Overrides Sub AcquireConnections(ByVal Transaction As Object)
Dim connMgr As IDTSConnectionManager90 = _
Me.Connections.Connection
exportedAddressFile = _
CType(connMgr.AcquireConnection(Nothing), String)
End Sub
Public Overrides Sub PreExecute()
MyBase.PreExecute()
textReader = New StreamReader(exportedAddressFile)
End Sub
Public Overrides Sub CreateNewOutputRows()
Dim nextLine As String
Dim columns As String()
Dim cols As String()
Dim delimiters As Char()
delimiters = ",".ToCharArray
nextLine = textReader.ReadLine
Do While nextLine IsNot Nothing
columns = nextLine.Split(delimiters)
With Output0Buffer
cols = columns(1).Split(";".ToCharArray)
.AddRow()
.ID = Convert.ToInt32(columns(0))
If cols.GetUpperBound(0) >= 0 Then
.Col1 = cols(0)
End If
If cols.GetUpperBound(0) >= 1 Then
.Col2 = cols(1)
End If
If cols.GetUpperBound(0) >= 2 Then
.Col3 = cols(2)
End If
If cols.GetUpperBound(0) >= 3 Then
.Col4 = cols(3)
End If
End With
nextLine = textReader.ReadLine
Loop
End Sub
Public Overrides Sub PostExecute()
MyBase.PostExecute()
textReader.Close()
End Sub
End Class
Put this code in ur script component. Before that add 5 columns to the script component output and name them as ID, col1, co2..,col4. ID is of data type int. Create a flat file destination and name it as connection and point it to the flat file as the source.
Im not sure whats the delimiter in ur flat file between the 2 columns. I have use a comma change it accordingly.
This is the output I get:
ID Col1
Col2 Col3
Col4
1 john
Greg David
Sam
2 tom
tony NULL
NULL
3 harry
NULL NULL
NULL -
Split the result of one column to multiple columns
Hi Experts,
Could you please help me to split the result of below query into multiple columns:
EXEC master..xp_cmdshell 'wmic service where "Caption Like ''%sql%''" get Caption , StartName, state'
I needs result of this query something like below but in tabular format:
Caption
StartName
State
SQL Server Integration Services 11.0
NT Service\MsDtsServer110
Running
SQL Full-text Filter Daemon Launcher (MSSQLSERVER)
NT Service\MSSQLFDLauncher
Running
SQL Server (MSSQLSERVER)
NT Service\MSSQLSERVER
Running
SQL Server Analysis Services (MSSQLSERVER)
NT Service\MSSQLServerOLAPService
Running
--------------------------------- Devender BijaniaRefer below query,
DROP TABLE #TEMP
CREATE TABLE #TEMP(COLS NVARCHAR(300))
INSERT INTO #TEMP
EXEC master..xp_cmdshell 'wmic service where "Caption Like ''%sql%''" get Caption , StartName, state'
SELECT *,
PATINDEX('% %',COLS) C1,
LEFT(COLS, PATINDEX('% %',COLS)) Caption,
LTRIM(RIGHT(COLS,LEN(COLS)-PATINDEX('% %',COLS))) ServiceName,
PATINDEX('% %',LTRIM(RIGHT(COLS,LEN(COLS)-PATINDEX('% %',COLS)))) C2,
LTRIM(RIGHT(LTRIM(RIGHT(COLS,LEN(COLS)-PATINDEX('% %',COLS))), LEN(LTRIM(RIGHT(COLS,LEN(COLS)-PATINDEX('% %',COLS)))) - PATINDEX('% %',LTRIM(RIGHT(COLS,LEN(COLS)-PATINDEX('% %',COLS)))))) State
FROM #TEMP
WHERE COLS IS NOT NULL AND RTRIM(LEFT(COLS, PATINDEX('% %',COLS))) <> 'Caption'
Regards, RSingh -
To display comma separted value into multiple column
Hi,
I want to display value into multiple column like below
data is like this
col1
res_menaHome:MenaHome
res_menaHomeEmp:MenaHome Employee
res_MDSpecialSer:MD Special Services
res_Smart:Smart
now i want to display like
col1 col2
res_menaHome MenaHome
res_menaHomeEmp MenaHome Employee
res_MDSpecialSer MD Special Services
res_Smart Smart
Thanks in advance.You mean like this?
with q as (select 'res_menaHome:MenaHome' myString from dual)
select substr(myString, 1, instr(myString, ':') - 1) col1,
substr(myString, instr(myString, ':') + 1) col2
from q
COL1,COL2
res_menaHome,MenaHome -
Splitting Single Column into 3 columns.
I have a column data like this
85052/123755 LAURITSEN/H HENRIK S MR
87432/119975 MORTENSEN/P PIA BUBANDT MS
73784/127530 PEDERSEN/M MORTEN H MR
88579/128347 JENSEN/M MORTEN MR
./AIRTECH STEFANSEN/P PER MR
10024/AIRTECH HOFFMANN/M MICHAEL KJUL MR
75922/128415 MOELLER/F FRANK MR
17639/128440 THOMSEN/P PETER MR
53198/127655 THYGESEN/E EBBE MR
85960/128473 ORAEINAMZADI/M MAZYAR MR
52663/128270 MAINZ/J JAN MR
17639/128440 THOMSEN/P PETER MR
27510/. HANSEN/N NILS HARLADSTED
43885/126300 LARSEN/J JOHNNI S MR
87580/125410 STEFANSEN/R RASMUS MR
63215/128445 NIELSSON/J JESPER MR
80594/123334 OLSEN/H HENRIK W MR
I need to split this into three columns
85052/123755 LAURITSEN/H HENRIK S MR
should be split as
Column1: 85052
Column2: 123755
Column3: LAURITSEN/H HENRIK S MR
but in some cases it may also be like this
./AIRTECH STEFANSEN/P PER MR
this should be split into
Column1: null
Column2: null
Column3: AIRTECH STEFANSEN/P PER MR
one more scenario is like this
27510/. HANSEN/N NILS HARLADSTED
Column1: 27510
Column2: null
Column3: HANSEN/N NILS HARLADSTED
Pls Help
Thanks
Vinoth.Based on the sample data you provided,
SQL> WITH T
2 AS (SELECT '85052/123755 LAURITSEN/H HENRIK S MR' str FROM DUAL
3 UNION ALL
4 SELECT './AIRTECH STEFANSEN/P PER MR' str FROM DUAL
5 UNION ALL
6 SELECT '10024/AIRTECH HOFFMANN/M MICHAEL KJUL MR' str FROM DUAL
7 UNION ALL
8 SELECT '27510/. HANSEN/N NILS HARLADSTED' str FROM DUAL)
9 SELECT REGEXP_REPLACE (REGEXP_SUBSTR (str, '[^/]+', 1, 1), '[^[:digit:]]') col1
10 ,REGEXP_REPLACE (REGEXP_SUBSTR (str, '[^/]+', 1, 2), '[^[:digit:]]') col2
11 ,REGEXP_SUBSTR (str, '[[:alpha:]].*', 1, 1) col3
12 FROM T;
COL1 COL2 COL3
85052 123755 LAURITSEN/H HENRIK S MR
AIRTECH STEFANSEN/P PER MR
10024 AIRTECH HOFFMANN/M MICHAEL KJUL
27510 HANSEN/N NILS HARLADSTED
SQL> G. -
Splitting one column into different columns.
Hello Experts,
How do i split datetime column into different columns while doing a Select statement.
Ex:
The column "REC_CRT_TS" has data like "2014-05-08 08:23:09.0000000".The datatype of this column is "DateTime". And i want it in SELECT statement like;
SELECT
YEAR(DATETIME) YEAR,
MONTH(DATETIME) MONTH,
DATENAME(DATETIME) MONTHNAME,
DATEPART(DATETIME) WEEKNUM,
DAY(DATETIME) DATE,
DATEPART(DATETIME) HOUR
FROM TABLE_NAME;
The output should look like this;
--YEAR| MONTH | MONTHNAME| WEEKNUM | DATE | HOUR
--2014| 5 | May | 25 | 08 |08
Any suggestions please.
Thanks!
RahmanI made a very quick research and I see in this blog post
http://www.jamesserra.com/archive/2011/08/microsoft-sql-server-parallel-data-warehouse-pdw-explained/
that It also uses its own query engine and not all features of SQL
Server are supported. So, you might not be able to use all your DBA tricks. And you wouldn’t want to build a solution against SQL Server and then just hope to upsize it to Parallel Data Warehouse Edition.
So, it is quite possible that this function doesn't exist in PDW version of SQL
Server. In this case you may want to implement case based month name or do it in the client application.
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles
Maybe you are looking for
-
Greetings, I scripted in 10.7 the mounting of a Windows share (SMB) for some back end tasks. My script needs to dump collected data over to a Windows share for further processing. My code seems to have broken since my upgrade to 10.8. Here is the lin
-
Multiple bin-release folders after 4.6 upgrade
Hi, I've searched for an answer for this and I assume its a dumb question, but after upgrading to Flash Builder 4.6 from 4.5 whenever I run an Export Release Build I get a new bin-releaseX folder. Is there a configuration item I can set to make it re
-
Issue with blend mode colour dodge?
So I'm not all that new to photoshop, I'd probably put myself in the intermediate category, but for some reason I haven't been able to figure this issue out, and I would love some help. I was doing a tutorial, and it mentioned we needed to change the
-
Do While loop with query?
There seems to be no way to do a do-while loop without using Cfscript. Is there a way to place a cfquery inside cfscript?
-
Hi. I'm trying to run a test program(C program) on Timesten and getting an error like below. SQLERR [SQLExecute: st_no_sp] : 7005 [TimesTen][TimesTen 11.2.1.2.0 ODBC Driver][TimesTen]TT7005: Procedure `SP_CREATE_ORDER' not found -- file "saCanon.c",