Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7722509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:11:41+00:00 2026-06-01T04:11:41+00:00

I use SQL Server 2008 and have a table with 5 char typed columns.

  • 0

I use SQL Server 2008 and have a table with 5 char typed columns.

CREATE TABLE [dbo].[deviceDataBulk](
[f1] [char](9) NULL,
[f2] [char](5) NULL,
[f3] [char](7) NULL,
[f4] [char](7) NULL,
[f5] [char](6) NULL)

I also have a bcp format file ;

<RECORD>
 <FIELD ID="1" xsi:type="CharFixed" LENGTH="9" COLLATION="Turkish_CI_AS"/>
 <FIELD ID="2" xsi:type="CharFixed" LENGTH="5" COLLATION="Turkish_CI_AS"/>
 <FIELD ID="3" xsi:type="CharFixed" LENGTH="7" COLLATION="Turkish_CI_AS"/>
 <FIELD ID="4" xsi:type="CharFixed" LENGTH="7" COLLATION="Turkish_CI_AS"/>
 <FIELD ID="5" xsi:type="CharFixed" LENGTH="6" COLLATION="Turkish_CI_AS"/>
</RECORD>
<ROW>
 <COLUMN SOURCE="1" NAME="f1" NULLABLE="YES" xsi:type="SQLCHAR"/>
 <COLUMN SOURCE="2" NAME="f2" NULLABLE="YES" xsi:type="SQLCHAR"/>
 <COLUMN SOURCE="3" NAME="f3" NULLABLE="YES" xsi:type="SQLCHAR"/>
 <COLUMN SOURCE="4" NAME="f4" NULLABLE="YES" xsi:type="SQLCHAR"/>
 <COLUMN SOURCE="5" NAME="f5" NULLABLE="YES" xsi:type="SQLCHAR"/>
</ROW>

My data file contains fixed length char data with no field terminators in each line. So, a full line will be 34 characters long.

My problem is field 4 and field 5 may not be present for each row. I may have 21 characters long line or 28 characters long line in that file.

There is no case that field 5 exists and field 4 not.

Possible scenarios for text file are ;

f1 f2 f3 f4 f5
f1 f2 f3 f4
f1 f2 f3

I couldn’t insert this file with BULK INSERT. I want BULK INSERT to insert nulls when it doesn’t have those fields, if the tool reaches end of line, just insert nulls for the rest of the fields.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:11:42+00:00Added an answer on June 1, 2026 at 4:11 am

    How about a 2-step approach ? First load the data into a staging table as ‘big rows’, then use a second query to split the raw lines into their corresponding fields and handle the “missing f5 and/or f4 columns”-situation accordingly ?

    Would look (more or less) like this : (untested!)

    CREATE TABLE [dbo].[deviceDataBulk_staging](
     [rowid] int IDENTITY(1 , 1) PRIMARY KEY,
     [raw] [varchar](34) NOT NULL)
    
    GO
    BULK INSERT [deviceDataBulk_staging]
    FROM '<your file>' 
    -- not sure if you really need a format-file here, 
    -- simply make sure to pass the correct line-separator if it is 'exotic'.
    
    GO
    
    INSERT [deviceDataBulk] (f1, f2, f3, f4, f5)
    SELECT f1 = SubString([raw], 1 , 9),
           f1 = SubString([raw], 10 , 5),
           f1 = SubString([raw], 15 , 7),
           f1 = (CASE WHEN Length([raw] < 22 THEN NULL ELSE SubString([raw], 22 , 7) END),
           f1 = (CASE WHEN Length([raw] < 29 THEN NULL ELSE SubString([raw], 29 , 6) END)
      FROM [deviceDataBulk_staging]
     ORDER BY [rowid]
    

    The Staging file would then look like :

    The [rowid] is there to keep the order identical to the order originally in the file, you might not need it but IMHO the overhead is minimal and MSSQL isn’t too keen on HEAP tables anyway so having it there is “A good thing [Tm]”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use SQL Server 2008, C#, I have a table which contains about 20000
I have table-valued parameters in SQL Server 2008, e.g.: CREATE TYPE UserType AS TABLE
I have SQL Server 2008 database with 2 tables: Table A has columns ID
I use SQL Server 2008. I have a database (Database1) that has one table
I use SQL Server 2005. I have a simple logging table that my web
I have my own database (SQL Server 2008 RQ) and i have a table
I have a table in a database on my development(local workstation) SQL Server 2008
I have a table with two separate columns for date and time (SQL Server
I have an audit table in my SQL Server 2008 database which contains the
I have an EMPLOYEE table in a SQL Server 2008 database which stores information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.