Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. BULK INSERT with CONVERT or CAST?

BULK INSERT with CONVERT or CAST?

Scheduled Pinned Locked Moved Database
databasecsharpasp-netsql-serversysadmin
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yong Yau
    wrote on last edited by
    #1

    While i am use bulk insert to import the csv file into my SQL SERVER through my system develop using ASP.NET by VB script, it able to import all csv data into my database. Unfortunately, some data such as those decimal or money data type already change to vchar for all the data type after import in my database and cause them to be close by double quote ("1,111.00"). This unable let me to total up those data while i do it with my system. As i research, there have CONVERT and CAST function to convert the data type, can i use them while i import the csv file into my database through the BULK INSERT? Thanks.

    S 1 Reply Last reply
    0
    • Y Yong Yau

      While i am use bulk insert to import the csv file into my SQL SERVER through my system develop using ASP.NET by VB script, it able to import all csv data into my database. Unfortunately, some data such as those decimal or money data type already change to vchar for all the data type after import in my database and cause them to be close by double quote ("1,111.00"). This unable let me to total up those data while i do it with my system. As i research, there have CONVERT and CAST function to convert the data type, can i use them while i import the csv file into my database through the BULK INSERT? Thanks.

      S Offline
      S Offline
      Sage
      wrote on last edited by
      #2

      Bulk load to a staging table, then clean the data when transferring to your production tables. This will allow you to leave the BulkInsert task alone, and expand the tranfer task as changes to the underlying data or destination tables changes. This is why we call it ETL. Extract the data from the source, transform it match your systems and the Load it into your systems. - Sage

      Y 1 Reply Last reply
      0
      • S Sage

        Bulk load to a staging table, then clean the data when transferring to your production tables. This will allow you to leave the BulkInsert task alone, and expand the tranfer task as changes to the underlying data or destination tables changes. This is why we call it ETL. Extract the data from the source, transform it match your systems and the Load it into your systems. - Sage

        Y Offline
        Y Offline
        Yong Yau
        wrote on last edited by
        #3

        Excuse me, can you mention more detail and how it to be done such as give some simple example? Thank you for you response.

        S 1 Reply Last reply
        0
        • Y Yong Yau

          Excuse me, can you mention more detail and how it to be done such as give some simple example? Thank you for you response.

          S Offline
          S Offline
          Sage
          wrote on last edited by
          #4

          This was thrown together, but should work if you replace the tablenames, columnnames and filenames with values that apply to your application BULK INSERT DBName.dbo.StagingTable -- StagingTable can be a copy of your destination table From 'C:\YourFile.txt' -- actual path to your file With ( FieldTerminator = ',', RowTerminator = '\n' -- for new line ) Alter Table StagingTable ADD SafeToTransfer BIT NOT NULL Default(1) -- Run some integrity checks prior to attempting to tranfer if ( select Count(*) From DBName.dbo.StagingTable Where IsNumeric( ColumnA )=0 ) >0 BEGIN Update DBName.dbo.StagingTable Set SafeToTransfer = 0 Where IsNumeric( ColumnA )=0 END if (select Count(*) From DBName.dbo.StagingTable Where isDate( ColumnB )=0 ) >0 BEGIN Update DBName.dbo.StagingTable Set SafeToTransfer = 0 Where isDate( ColumnB )=0 END if ( select Count(*) From DBName.dbo.StagingTable Where isNumeric( ColumnD )=0 ) >0 BEGIN Update DBName.dbo.StagingTable Set SafeToTransfer = 0 Where isNumeric( ColumnD )=0 END Insert Into DBName.dbo.DestinationTable ( ColumnA, -- an INT Column ColumnB, -- a datetime column ColumnC, -- a varchar(100) column ColumnD, -- a numeric(9,5) column ColumnE -- another varchar(100) column ) SELECT CAST( ColumnA as int), -- an INT Column CAST( ColumnB as datetime), -- a datetime column LEFT( ColumnC, 100), -- a varchar(100) column CONVERT( numeric(9,5), ColumnD), -- a numeric(9,5) column LEFT( ColumnE, 100) -- another varchar(100) column From DBName.dbo.StagingTable Where SafeToTransfer = 1 Alter Table StagingTable DROP COLUMN SafeToTransfer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups