bulk insert a date in YYYYMM format to date field in MS SQL table

前端 未结 3 730
别跟我提以往
别跟我提以往 2020-12-22 03:08

I have a large text file (more than 300 million records). There is a field containing date in YYYYMM format. Target field is of date type and I\'m using MS SQL 2008 R2 serve

相关标签:
3条回答
  • 2020-12-22 03:25

    You may not care about the day but SQL does.
    YYYYMM is not a valid SQL date.
    A date must have day component.
    In your example it parsed it down as YYMMDD.
    You could insert into a VarChar as Jaimal proposed then append a 01 and convert.

    I would read in the data in .NET add the 01 and use DateTime.ParseExact and insert row by row asynch. You can catch any parse that is not successful.

    Or you might be able to do a global replace in the csv "," to "01,". It is worth a try.

    0 讨论(0)
  • 2020-12-22 03:26

    There isn't a way to do this on Bulk Insert. You have 2 options.

    1. Insert the date as into a varchar field then run a query to convert the date into a DateTime field
    2. Use SSIS
    0 讨论(0)
  • 2020-12-22 03:49

    Run SET DATEFORMAT ymd before the bulk insert statement

    Note that yyyy-mm-dd and yyyy-dd-mm are not safe in SQL Server generally: which causes this. Always use yyyymmdd. For more, see best way to convert and validate a date string (the comments especially highlight misunderstandings here)

    0 讨论(0)
提交回复
热议问题