Click here to Skip to main content
15,914,488 members
Home / Discussions / Database
   

Database

 
GeneralRe: Subtotaling.. Am I doing it the wrong way?? Pin
David Skelly7-Jul-09 22:58
David Skelly7-Jul-09 22:58 
QuestionMultiple columns or one formatted column ? [modified] Pin
Hakmeh Mohannad7-Jul-09 0:48
Hakmeh Mohannad7-Jul-09 0:48 
AnswerRe: Multiple columns or one formatted column ? Pin
dan!sh 7-Jul-09 1:15
professional dan!sh 7-Jul-09 1:15 
AnswerRe: Multiple columns or one formatted column ? Pin
Eddy Vluggen7-Jul-09 1:17
professionalEddy Vluggen7-Jul-09 1:17 
AnswerRe: Multiple columns or one formatted column ? Pin
Jeremy Likness9-Jul-09 8:34
professionalJeremy Likness9-Jul-09 8:34 
QuestionSQL Query Pin
theCPkid6-Jul-09 15:41
theCPkid6-Jul-09 15:41 
AnswerRe: SQL Query Pin
Niladri_Biswas6-Jul-09 16:05
Niladri_Biswas6-Jul-09 16:05 
AnswerRe: SQL Query Pin
Niladri_Biswas6-Jul-09 17:00
Niladri_Biswas6-Jul-09 17:00 
In my earlier post, I have given the idea.

Here is the solution

Say , I have a table tblFileNameSort where I am storing the entire file path

i.e.

FILEPATH
----------
D:\myfolder\my_file_name.txt
C:\newFile.txt
C:\abcFile.txt
E:\bop.txt

I have written a table valued split function

--Created by Niladri Biswas
ALTER FUNCTION [dbo].[fnSplit] 
(@oldstring as varchar(100),@delimeter as varchar(1))
RETURNS @mytab table(counter int,stringval varchar(100)) 
AS
Begin 
		
		Declare @newstring as varchar(100)
		Declare @pos as int
		Declare @i as int
		Declare @c as int	

		set @newstring = '';		
		set @i = 1
		set @c = 0

		set @pos = CHARINDEX(@delimeter, @oldstring) 

		WHILE (@i != 0)

			Begin

				set @c = @c +1
				insert into @mytab(counter,stringval) values(@c,@newstring + Substring(@oldstring,0, @pos))

				
				set @oldstring = Substring(@oldstring,@pos+1,len(@oldstring))

				set @pos = CHARINDEX(@delimeter, @oldstring)

				set @i = @pos;
				if (@i = 0)
                Begin
                    set @i = 0;
						set @c = @c +1
                
					insert into @mytab(counter,stringval) values(@c,@newstring + @oldstring)
					
                End

			End

			return 

End


And my stored proc reads like the following

--Created by Niladri Biswas
ALTER PROCEDURE Dbo.SortTxtFile
	-- Add the parameters for the stored procedure here
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	-- VARIABLE DECLARATION
	DECLARE @FILEPATH VARCHAR(50)

	DECLARE @FINALSTORAGE TABLE 
	(
		FILENAMES VARCHAR(100)
	)

    -- STEP 1: DECLARE A CURSOR
	DECLARE MYRANDOMCURSOR CURSOR FOR

		SELECT    FILEPATH
		FROM      tblFileNameSort

	-- STEP 2: OPEN THE CURSOR
	OPEN MYRANDOMCURSOR 

	FETCH MYRANDOMCURSOR INTO @FILEPATH

	-- STEP 3: START THE LOGIC

	WHILE @@Fetch_Status = 0

	BEGIN

		-- STEP 4: INSERT THE COMBINED RECORDS INTO TABLE      
                --          @FINALSTORAGE

			INSERT INTO @FINALSTORAGE

			SELECT stringval FROM dbo.fnSplit(@FILEPATH,'\')

			WHERE stringval LIKE '%.txt'
			
		
	-- STEP 5: GET THE NEXT RECORD
	FETCH MYRANDOMCURSOR INTO @FILEPATH           

   END

	--STEP 6: CLOSE THE CURSOR
	CLOSE MYRANDOMCURSOR

	--STEP 6: DEALLOCATE THE CURSOR
	DEALLOCATE MYRANDOMCURSOR

	SELECT * FROM @FINALSTORAGE ORDER BY FILENAMES
END
GO


The output is

FILENAMES
-------------
abcFile.txt
bop.txt
my_file_name.txt
newFile.txt

N.B.~ You must change the database design, as I posted in my earlier reply

Hope this helps.
Smile | :)

Niladri Biswas

GeneralRe: SQL Query Pin
theCPkid6-Jul-09 22:52
theCPkid6-Jul-09 22:52 
AnswerRe: SQL Query Pin
Mycroft Holmes6-Jul-09 20:06
professionalMycroft Holmes6-Jul-09 20:06 
GeneralRe: SQL Query Pin
Paul Unsworth6-Jul-09 22:19
Paul Unsworth6-Jul-09 22:19 
GeneralRe: SQL Query Pin
Mycroft Holmes6-Jul-09 23:07
professionalMycroft Holmes6-Jul-09 23:07 
GeneralRe: SQL Query Pin
theCPkid6-Jul-09 22:48
theCPkid6-Jul-09 22:48 
QuestionT-SQL USE with a variable Pin
TheComputerMan6-Jul-09 4:55
TheComputerMan6-Jul-09 4:55 
AnswerRe: T-SQL USE with a variable Pin
Blue_Boy6-Jul-09 6:13
Blue_Boy6-Jul-09 6:13 
GeneralRe: T-SQL USE with a variable Pin
TheComputerMan6-Jul-09 6:42
TheComputerMan6-Jul-09 6:42 
GeneralRe: T-SQL USE with a variable Pin
Blue_Boy6-Jul-09 7:09
Blue_Boy6-Jul-09 7:09 
GeneralRe: T-SQL USE with a variable Pin
TheComputerMan6-Jul-09 21:51
TheComputerMan6-Jul-09 21:51 
GeneralRe: T-SQL USE with a variable Pin
DoctorMick7-Jul-09 2:04
DoctorMick7-Jul-09 2:04 
GeneralRe: T-SQL USE with a variable Pin
TheComputerMan7-Jul-09 3:25
TheComputerMan7-Jul-09 3:25 
QuestionBest Way to Sync SQL Srv Databases Pin
Pago0076-Jul-09 3:58
Pago0076-Jul-09 3:58 
AnswerRe: Best Way to Sync SQL Srv Databases Pin
DoctorMick6-Jul-09 6:23
DoctorMick6-Jul-09 6:23 
AnswerRe: Best Way to Sync SQL Srv Databases Pin
dingoishere23-Feb-11 22:30
dingoishere23-Feb-11 22:30 
QuestionSchema Question Pin
Paul Unsworth6-Jul-09 3:13
Paul Unsworth6-Jul-09 3:13 
AnswerRe: Schema Question Pin
DoctorMick6-Jul-09 6:24
DoctorMick6-Jul-09 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.