smalldatetime java,如何在数据导入期间将平面文件中的dd-mmm-yy值格式化为smalldatetime?…[通俗易懂]

smalldatetime java,如何在数据导入期间将平面文件中的dd-mmm-yy值格式化为smalldatetime?…[通俗易懂]IhaveaflatfilewhichisimportedintoSQLServerviaanexistingSSISpackage.Ineedtomakeachangetothepackagetoaccommodateanewfieldintheflatfile.Thenewfieldisadatefieldwhi…

大家好,又见面了,我是你们的朋友全栈君。

smalldatetime java,如何在数据导入期间将平面文件中的dd-mmm-yy值格式化为smalldatetime?...[通俗易懂]

I have a flat file which is imported into SQL Server via an existing SSIS package. I need to make a change to the package to accommodate a new field in the flat file. The new field is a date field which is in the format dd-mmm-yy (e.g. 25-AUG-11). The date field in the flat file will either be empty (e.g. a space/whitespace) or populated with a date. I don’t have any control over the date format in the flat file.

I need to import the date field in the flat file into an existing SQL Server table and the target field data type is smalldatetime.

I was proposing to import the date as a string into a load table and then convert to smalldatetime when taking the data from the load table. But is there another possible way to parse the date format dd-mmm-yy to load this straight into a smalldatetime field without having to use convert to smalldatetime from the load table. I can’t quite think how to parse the date format, particularly the month. Any suggestions welcome.

解决方案

Here is an example that might give you an idea of what you can do. Ideally, in an SSIS package or in any ETL job, you should take into account that data may not be exactly what you would like it to be. You need to take appropriate steps to handle the incorrect or invalid data that might pop up now and then. That’s why SSIS comes up with lots of Transformation tasks within Data Flow Task which you can make use of to clean up the data.

In your case, you can make use of Derived Column transformation or Data conversion transformation to achieve your requirements.

The example was created in SSIS 2008 R2. It shows how to read a flat file containing the dates and load into an SQL table.

iMPG7.png

I created a simple SQL table to import the flat file data.

AMYy3.png

On the SSIS package, I have a connection manager to SQL and one for Flat file. Flat file connection is configured as shown below.

wHYja.png

4gY8N.png

rLQX5.png

m78d2.png

On the SSIS package, I placed a Data Flow Task on the Control Flow tab. Inside, the Data Flow task, I have a Flat File Source, Derived Column transformation and an OLE DB Destination. Since the Flat file source and OLE DB destination are straightforward, I will leave those out here. The Derived transformation creates a new column with the expression (DT_DBDATE)SmallDate. Note that you can also use Data Conversion transformation to do the same. This new column SmallDateTimeValue should be mapped to the database column in OLE DB Destination.

Tnr8M.png

If you execute this package, it will fail because not all the values in the file are valid.

902ep.png

The reason why it fails in your case is because the invalid data is directly inserted into the table. In your case, the table will throw an exception making the package to fail. In this example, the package fails because the default setting on the Derived column transformation is to fail the component if there is any error. So, let’s place a dummy transformation to redirect the error rows. We will Multicast transformation for this purpose. It won’t really do anything. Ideally, you should redirect the error rows to another table using OLE DB Destination or other Destination component of your choice so you can analyze the data that causes the errors.

Drag the red arrow from Derived transformation and connect it to the Multicast transformation. This will popup the Configure Error Output dialog. Change the values under the column Error and Truncation from Fail component to Redirect row. This will redirect any error rows to the Multicast transformation and will not get into the tables.

SOaLm.png

Now, if we execute the package, it will run successfully. Note the number of rows displayed in each direction.

ysZwu.png

Here is the data that got into the table. Only 2 rows were valid. You can look at the first screenshot that showed the data in the file and you can see only 2 rows were valid.

Hope that gives you an idea to implement your requirement in the SSIS package.

5P9mp.png

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/144398.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Matlab常用绘图颜色搭配[通俗易懂]

    Matlab常用绘图颜色搭配[通俗易懂]推荐几款常用的离散色系搭配,并且附带matlab箱线图作为实例参考!

    2022年5月24日
    124
  • html5 sexteen,Female Singaporean teacher jailed for teen sex

    html5 sexteen,Female Singaporean teacher jailed for teen sexAfemaleSingaporeanteacherwhohadsexwithaminorstudentwassentencedfortenmonthsinjailonMonday,localmediareported.AccordingtoChineselanguagenewspaperLianheZaobao,the32-year-ol…

    2022年5月10日
    47
  • Mysql 实现多种逻辑删除方案

    Mysql 实现多种逻辑删除方案Mysql实现多种逻辑删除方案新增逻辑删除字段方式多deleted值deleted:0代表未删除,删除时把deleted赋值为时间戳UNIX_TIMESTAMP(NOW())采用备份表方式最近在做公司项目的时候,对于表的逻辑删除,和其他同事出现了不同意见,故查阅了一些blog,结合自己的实际情况,再次做了笔记,以备后查。在实际的项目开发中,对于某些业务数据,一般都不会采用物理删除的方式,…

    2022年5月5日
    188
  • MyBatis 快速入门和重点详解(详解)「建议收藏」

    MyBatis 快速入门和重点详解(详解)「建议收藏」目录前言:准备工作:开始:1、创建项目(本博主就使用Eclipse,其他编辑器都可以,工具而已)2、创建数据库(mybatisdemo)及表(student)3、创建User对象4、在entity包下创建userMapper,xml文件,如下图5、创建MyBatis的配置文件6、创建MybatisTest.java进行测试前言:Mybatis概念、名词的…

    2022年6月13日
    27
  • Sicily 1700. Ping

    Sicily 1700. Ping

    2021年8月23日
    75
  • Javascript的5种基本数据类型总结

    Javascript的5种基本数据类型总结ECMAScript中有5种基本数据类型,分别是:Undefined,Null,Boolean,Number和String,还有一种复杂数据类型Object,Object本质上是由一组无序的名值对组成的。(这里就不细细讲述了)typeof操作符typeof操作符是负责检测给定变量的数据类型,下面是几个使用typeof操作符的例子这说明typeof操作符可以是变量,可以是数值字面量。注意,typeof是一个操作符而不是一个函数,因此例子中的圆括号尽管可以使用,但不是必须的。调用typeofnull

    2025年9月2日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号