Project

General

Profile

Bug #754 » spUtilSplitToTable7Column.sql

M Azid Wahyudi, 01/18/2022 11:14 AM

 
1
USE [Bintang_Prod]
2
GO
3
/****** Object:  StoredProcedure [dbo].[spUtilSplitToTable7Column]    Script Date: 1/18/2022 9:19:56 AM ******/
4
SET ANSI_NULLS ON
5
GO
6
SET QUOTED_IDENTIFIER ON
7
GO
8
ALTER PROCEDURE [dbo].[spUtilSplitToTable7Column]
9
	@sInputList VARCHAR(MAX)
10
	, @sDelimiterRow VARCHAR(MAX)
11
	, @sDelimiterColumn VARCHAR(MAX)
12
AS
13

    
14
--declare @sInputList VARCHAR(MAX)='|S4. Leadership (Planning & Monitoring, Decision Making, People Development),25,Level 03,,-3,0,0||T13. Bintang Product Knowledge,5,Level 03,,-3,0,0'
15
--declare @sDelimiterRow VARCHAR(MAX)='|'
16
--declare @sDelimiterColumn VARCHAR(MAX)=','
17

    
18
	--Variable Declaration
19
	DECLARE @row_temp TABLE (item varchar(MAX))
20
	DECLARE @col_temp TABLE (
21
		col1 varchar(MAX),
22
		col2 varchar(MAX),
23
		col3 varchar(MAX),
24
		col4 varchar(MAX),
25
		col5 varchar(MAX),
26
		col6 varchar(MAX),
27
		col7 varchar(MAX)
28
	)
29
	DECLARE @row_count int, @col_pos int
30
	DECLARE @col1 varchar(MAX),
31
		@col2 varchar(MAX),
32
		@col3 varchar(MAX),
33
		@col4 varchar(MAX),
34
		@col5 varchar(MAX),
35
		@col6 varchar(MAX),
36
		@col7 varchar(MAX)
37
	
38
	--Processing Row
39
	INSERT INTO @row_temp
40
	SELECT item FROM dbo.fnUtilSplit(@sInputList, @sDelimiterRow)
41
	SELECT @row_count = COUNT(*) FROM @row_temp
42
	
43
	--Processing Column
44
	IF (@row_count > 0)
45
	BEGIN
46
		DECLARE cur CURSOR
47
		FOR SELECT item FROM @row_temp 
48
		
49
		DECLARE @cur_status int, @col_txt varchar(MAX), @col_txt_reverse varchar(MAX)
50
		SET @cur_status = 0
51
		
52
		OPEN cur
53

    
54
		WHILE @cur_status = 0
55
		BEGIN
56
			SET @col_txt = ''
57
			SET @col_txt_reverse = ''
58
			FETCH NEXT FROM cur INTO @col_txt
59
			SET @cur_status = @@FETCH_STATUS
60
			IF @cur_status = 0
61
			BEGIN
62
				SET @col_txt_reverse = REVERSE(@col_txt)
63
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
64
				SET @col6=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
65
				SET @col6 =  REVERSE(@col6)
66
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
67
				
68
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
69
				SET @col5=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
70
				SET @col5 =  REVERSE(@col5)
71
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
72

    
73
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
74
				SET @col4=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
75
				SET @col4 =  REVERSE(@col4)
76
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
77

    
78
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
79
				SET @col3=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
80
				SET @col3 =  REVERSE(@col3)
81
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
82

    
83
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
84
				SET @col2=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
85
				SET @col2 =  REVERSE(@col2)
86
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
87

    
88
				SET @col_pos = CHARINDEX(@sDelimiterColumn, @col_txt_reverse, 0)
89
				SET @col1=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,1,@col_pos-1)))
90
				SET @col1 =  REVERSE(@col1)
91
				SET @col_txt_reverse=RTRIM(LTRIM(SUBSTRING(@col_txt_reverse,@col_pos+LEN(@sDelimiterColumn),LEN(@col_txt_reverse))))
92

    
93
				INSERT INTO @col_temp VALUES( REVERSE(@col_txt_reverse), @col6, @col5, @col4, @col3, @col2, @col1)
94
			END
95
		END
96
				
97
		CLOSE cur
98
		DEALLOCATE cur
99
	END
100
	
101
	SELECT * FROM @col_temp
(3-3/4)