title: Import text file into sql Server
body: <p>I have read a text file with the bulk syntax but after that I have found another problem: In my text file it has the same header and detail in it so my problem now is how can I put the header in a <code>table1</code> and the detail in a <code>table2</code>?</p>
  
  <p>Another thing I create just a <code>field1</code> for put all my rows (header and detail), but they have <code>FIELDTERMINATOR = ‘|’</code>, but the header it has seven fields and the detail it has eleven fields so that's another problem because I cannot create just one table with the exact fields to put my detail and header.</p>
  
  <p>This an example of the text files I am reading:</p>
  
  <blockquote>
    <p>HR|001580/06|RG|11/01/2013 12:00|BG|3573|001580
    IT|001580/01|1|00147066||1200|852.3|830.3|1.35|UNIDAD|0|31/12/2014
    00:00
    IT|001580/02|1|00147066||200|852.3|830.3|1.35|UNIDAD|1|31/12/2014
    00:00
    IT|001580/03|1|00147066||100|852.3|830.3|1.35|UNIDAD|55|31/12/2014
    00:00
    IT|001580/04|2|00254276||200|852.3|830.3|1.35|UNIDAD|0|31/12/2014
    00:00
    IT|001580/05|3|00305359||1700|852.3|830.3|1.35|UNIDAD|0|31/12/2014
    00:00
    IT|001580/06|3|00305359||300|852.3|830.3|1.35|UNIDAD|1|31/12/2014
    00:00</p>
  </blockquote>
  
  <p>This is the query I have…</p>
  
  <pre><code>CREATE TABLE #temp
  (
      campo1 VARCHAR(max)
  )
  BULK INSERT #temp
  FROM ”
  WITH
  (
      FIELDTERMINATOR = ‘|’,
      ROWTERMINATOR = ‘\n’
  )
  </code></pre>
  
  <p>This firs query it is for the header</p>
  
  <pre><code>SELECT *
  FROM #temp
  WHERE SUBSTRING(campo1,1,2) = ‘HR’
  </code></pre>
  
  <p>This firs query it is for the detail</p>
  
  <pre><code>SELECT *
  FROM #temp
  WHERE SUBSTRING(campo1,1,2) = ‘IT’
  </code></pre>
  
  <p>How can I separate the fields for the header and the detail?</p>