如何将日期值从Excel导入包含零日期的SQL Server?

时间:2015-03-01 11:30:37

标签: sql-server excel import

当我尝试将日期值从Excel导入SQL Server并且值包含零日期(如2013-01-00)时,这些值将作为空值保存在数据库中。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您没有说明您用于导入的方法/环境,因此一般建议如下:

  • 进行特定替换:

    如果(ImportValue ='2013-01-00')则导入('1/1/1970')其他导入(ImportValue)

  • 进行IsDate测试和替换:

    如果IsDate(ImportValue)然后导入(ImportValue)Else导入('1/1/1970')

  • 导入后,使用SQL替换所有空日期值,并更改为实际日期

让我们知道您正在使用的环境以获得更好的示例。

答案 1 :(得分:0)

由于您使用的是向导,因此您无法创建“If ... Then ...”语句,但可以轻松导入数据,然后使用SQL Server Management Studio更改NULL值(SQL-SMS)工具。

使用向导将Excel数据导入SQL Server:

Launch the Import and Export Data (32-bit), click Next
Specify Excel Data Source and workbook name,
  Hopefully, the existing spreadsheet has column names in the first row
    if so then check "First row has column names"
  click Next
Choose the Destination SQL Server, Authentication, and Database, click Next
Click "Copy data from one or more tables or views", click Next
Check-mark the Sheet within the workbook you want to import from
  If you want a specific table name, edit the "Destination" column
    For this example, I am importing to table "TestExcelImport"
  Make sure the desired Source Sheet stays hi-lighted, click the "Edit Mappings" button,
  depending on if the spreadsheet had column names in the first row
    you may see those column names in the "Source" column
    you can change the "Destination" column names if you prefer a different field name
  you can click the "Preview..." button to see approximately what your imported data will look like
  click Next
Check-mark "Run immediately", click Next
Review the summary
  you should see something like "Copy rows from 'Sheet1$' to [TestExcelImport]"
  click Finish
The wizard will execute, and show you details of the process
  click Close

更新日期字段为NULL的行:

Open SQL-SMS, connect to the SQL Server you imported to
Browse to the database, then expand "Tables"
  Tip: if you already had SQL-SMS open, you may need to right-click "Tables" and select "Refresh"
Right-click the table you imported to, then click "Edit top 1000 rows"
  In my example above, this would be table "TestExcelImport"
  You should see your table with it's data (including some NULL date values)
We want to filter for records that have NULL date values,
  From the menu, choose: Query Designer > Pane > Criteria
  Tip: you can optionally choose to see the SQL pane also, handy for studying SQL syntax
  In the upper "Criteria" pane, find the date row, then over in the "Filter" column type: null
  (it will change to "IS NULL" automatically)
  click the red exclamation mark button in the toolbar, labeled "Execute SQL"
  you will see in the lower pane, only the records that have NULL for date
Change the Query Type to update records
  From the menu, choose: Query Designer > Change Type > Update
  You will now in the upper "Criteria" pane an new column named "New Value"
  In the date value row, in the "New Value" column, specify the date you want to replace NULL
    Example: type "1/1/1980" in the "New Value" column if you want that date to replace all NULL date values
  Note: be SURE the "IS NULL" is still in the "Filter" column
  click the red exclamation mark button in the toolbar to run the UPDATE query.
Close then re-open this table to see NULL date values replaced with 1/1/1980
  Optionally: do a "Change Type" back to "Select", and remove the "IS NULL" filter

希望这有帮助!! (68米)