0

I have a table that looks like this:

enter image description here

And I need to split the filenames from extensions and put them both into separate columns.

it should look like this: enter image description here

Then I need to roll them all up by directory with another field containing comma separated list of all file extension in that folder.

EG: Here is what the end product should be:
Here is what I have so far:

Here is what I have so far:

 select 
    Length,
    (case when Name like '%.%'
      then (left((Name), charindex('.', (Name)) - 1))
      else ''
 end) as FileName
,(case when Name like '%.%'
      then reverse(left(reverse(Name), charindex('.', reverse(Name)) -                        1))
      else ''
 end) as Extension
 ,Directory
 FROM   dbo.[SourceRetail-V1] 
 WHERE Mode not like 'd--%'
 order by Directory asc

Issues:

  1. How do I get filenames with something like "FileName.MoreFileName.Txt"
    a. It should look like this "FileName.MoreFileName" but my code sees the period and then strips it to "FileName"
  2. How do I roll up by directory yet still keep a running list of all file extension in the directory in another field?

Here is the source in text:

Mode    Length  Name    Directory
-a---   78497   BSAS.map.xml    Y:\Data\Retail\BQ\Maps\SAP
-a---   4329    T052.tf.sql Y:\Data\Retail\BQ\Maps\SAP
-a---   24268   T052.map.txt    Y:\Data\Retail\BQ\Maps\SAP
-a---   53837   PAYR.map.xml    Y:\Data\Retail\BQ\Maps\SAP
-a---   4321    LFB1.tf.xml Y:\Data\Retail\BQ\Maps\SAP
-a---   146089  BSAK.map.xml    Y:\Data\Retail\BQ\Maps\SAP
-a---   4322    LFA1.tf.xml Y:\Data\Retail\BQ\Maps\SAP
-a---   4325    LFC1.tf.xml Y:\Data\Retail\BQ\Maps\SAP

Intermediary Table:

Mode    Length  Name    Extension   Directory
-a---   78497   BSAS.map    xml Y:\Data\Retail\BQ\Maps\SAP
-a---   4329    T052.tf sql Y:\Data\Retail\BQ\Maps\SAP
-a---   24268   T052.map    txt Y:\Data\Retail\BQ\Maps\SAP
-a---   53837   PAYR.map    xml Y:\Data\Retail\BQ\Maps\SAP
-a---   4321    LFB1.tf xml Y:\Data\Retail\BQ\Maps\SAP
-a---   146089  BSAK.map    xml Y:\Data\Retail\BQ\Maps\SAP
-a---   4322    LFA1.tf xml Y:\Data\Retail\BQ\Maps\SAP
-a---   4325    LFC1.tf xml Y:\Data\Retail\BQ\Maps\SAP

End Product:

Mode    Length  Directory   Extensions
a----   319998  Y:\Data\Retail\BQ\Maps\SAP  xml,sql,txt
VinnyGuitara
  • 605
  • 8
  • 26
  • 1
    pasting expected,sample,table results as text allows some one to test your data.By posting as images it will be hard and images are blocked in some domains as well,please paste the images data as text – TheGameiswar Aug 30 '16 at 14:46
  • Possible duplicate of [Concatenate many rows into a single text string?](http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string) – Matt Aug 30 '16 at 14:49
  • @TheGameiswar - Thanks for the suggestion, added source as text. – VinnyGuitara Aug 30 '16 at 14:50
  • After you cut up the string similar to what you have all you need to do is to concatenate teh rows into a string. this last part has been answered many times on SO, I tagged 1 for your the FOR XML PATH, which is 2008 R2 +, example in the accepted answer is pretty good method. – Matt Aug 30 '16 at 14:52
  • @VinnyGuitara:please paste endproduct also as text – TheGameiswar Aug 30 '16 at 14:53
  • @TheGameiswar - Updated as suggested. Thanks – VinnyGuitara Aug 30 '16 at 14:57

3 Answers3

1

For the first query you can use

select substring([FileName], 0, len([FileName])- charindex('.', reverse([FileName]))+1)
from tablename

For second question you can use For XML Path/STUFF to concatenate all the extensions

Kannan Kandasamy
  • 13,405
  • 3
  • 25
  • 38
  • So using this and my code to get the extension I have teh intermediary table. I am not understanding the next part. I would assume I have to group by the directory and sum the length but to create another field to concatenate on and roll them up is what is getting me now. – VinnyGuitara Aug 30 '16 at 15:10
  • Yes for part two you can use query in this link to merge all extensions http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string – Kannan Kandasamy Aug 30 '16 at 15:13
  • Using: Select distinct Inter2.Directory, SUM(Inter2.Length) as size, substring( (Select ','+ Inter1.Extension AS [text()] From dbo.Intermediary Inter1 Where Inter1.Directory = Inter2.Directory ORDER BY Inter1.Extension For XML PATH ('') ), 2, 1000) [Ext] From dbo.Intermediary Inter2 group by inter2.Directory And my result is: Directory size Ext Y:\Data\Retail\_GlobalRetail\Processing\Archives\NAS_FSA 6255017984 db,db,db,db,db,db,db,db,db,db,db How do I get distinct extensions to roll up – VinnyGuitara Aug 30 '16 at 15:27
1

Try using STUFF to do the extension list.

select distinct
    Directory,
    Ext = STUFF(( SELECT distinct 
                   case when t2.Name like '%.%' 
                      then ' | '+ reverse(left(reverse(t2.Name), charindex('.',reverse(t2.Name))-1))  
                      else '' 
                    end
                FROM @T t2
                WHERE t2.Directory = t1.Directory 
                ORDER BY 1
                FOR XML PATH('')), 1, 3, '')
from @T t1
jim31415
  • 8,588
  • 6
  • 43
  • 64
1

I only pasted two records into the the demonstrative table variable, but this should do.

Declare @YourTable table (Mode varchar(50),Length int,Name varchar(100),Directory varchar(250))
Insert into @YourTable values
('-a---',78497,'BSAS.map.xml','Y:\Data\Retail\BQ\Maps\SAP'),
('-a---',4329 ,'T052.tf.sql' ,'Y:\Data\Retail\BQ\Maps\SAP'),
('-a---',4329 ,'NoExtension' ,'Y:\Data\Retail\BQ\Maps\SAP')


Select A.Mode
      ,Length = sum(A.Length)
      ,A.Directory
      ,Extensions = max(B.Extensions)
 From @YourTable A
 Cross Apply (
               Select Extensions=Stuff((
                Select Distinct ',' + case when CharIndex('.',Name)=0 then 'None' else Right(Name,CharIndex('.',Reverse(Name))-1) end
                 From  @YourTable 
                 Where Directory=A.Directory
                 Order By 1  
                 For XML Path ('')),1,1,'')
              ) B
 Group By A.Mode,A.Directory

Returns

Mode    Length  Directory                   Extensions
-a---   87155   Y:\Data\Retail\BQ\Maps\SAP  None,sql,xml
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
  • This looked very promising but I am getting this with the real data: Msg 536, Level 16, State 2, Line 6 Invalid length parameter passed to the RIGHT function. Is there something off the top of your head that is causing this? null value maybe? – VinnyGuitara Aug 30 '16 at 15:33
  • I had some filenames with no extensions but this is the answer. Can you guide me on how to make this work with file names that do not have an ext or a ".". Those still need to be listed. – VinnyGuitara Aug 30 '16 at 15:37
  • @VinnyGuitara, you already have the check for extensions in your original query: case when Name like '%.%' – jim31415 Aug 30 '16 at 15:39
  • @VinnyGuitara Easy fix. When you say no extensions, do you mean no double extension or no extension at all? – John Cappelletti Aug 30 '16 at 15:41
  • @JohnCappelletti - 2 rows in the table have no extension at all: "FileName" is the actual full filename – VinnyGuitara Aug 30 '16 at 15:42
  • @VinnyGuitara in the cross apply query just modify the where clause to Where Directory=A.Directory and Name like '%.%' The total length will still be correct. That said, do want to show extensions like sql,xml,none,..., – John Cappelletti Aug 30 '16 at 15:46
  • Yea that seems good to put that none there where there is no file extension. This solution is great, thank you so much. – VinnyGuitara Aug 30 '16 at 16:03
  • @VinnyGuitara Updated answer to include NONE – John Cappelletti Aug 30 '16 at 16:13