solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups MSSLQTips Giveaways MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Twitter Page MSSQLTips Google+ Page

SQL Product Highlight

Idera - SQL compliance manager

SQL compliance manager is a comprehensive auditing solution that tells you who did what, when and how on your SQL Servers. SQL compliance manager helps you ensure compliance with regulatory and data security requirements.

Learn more!








Converting Invalid Date Formats Using SQL Server 2000 DTS

By: | Read Comments | Print

Edgewood Solutions is a technology company focused on Microsoft SQL Server and founder of MSSQLTips.com.

Related Tips: More

Problem
Importing data into SQL Server 2000 using Data Transformation Services isn’t always picking a source and destination and letting it fly. Sometimes data is formatted in a way that SQL Server won’t accept. In these cases the phrase “Extract, Transform, and Load” (ETL) need to be treated as “Extract, Load, and Transform", with dates being especially troublesome. There are a number of ways to transform data as it comes into the database, the most popular of which is either through a view or ActiveX script transformation. This tip will discuss using a view to manipulate the data as it gets moved to the final table.

Solution
Let’s say we want to create a database application that reminds us of all the great upcoming concerts. We find a place that provides a text file of this information: event date, event name, and event location. Everything looks straightforward until we examine the date format. The dates in the text file look like this: “2006-31-10” (YYYY-DD-MM). To find out whether the date format is acceptable we open Query Analyzer and run the following query:

Using The IsDate statement to validate a date format

The IsDate keyword tells SQL Server to evaluate the string entered to see if it is an acceptable format. If it is valid then “1” will be returned, if not then “0” is returned. In this case it isn't, but we luck out because the date string is always 10 characters.

We decide on the following plan:

  • Create a database named “concertdates”
  • Create a “working” table named “tbltempconcertdates”. The table has an eventdate column of varchar(20), an eventname column of varchar(200), and an eventlocation of varchar(100):
    Working table properties


    Data as it appears in the working table

  • Create a view named vwconcerttransform that is based on the working table. The eventdate column in the view allows a re-arrangement of the table column to “MM/DD/YYYY”, a valid date format, and converts it from type varchar to smalldatetime. This is accomplished by concatenation using right(eventdate, 2) + '/' + substring(eventdate, 6, 2) + '/' + left(eventdate, 4):
    View properties

    Data as it appears in the view
  • Instead of using temp tables, we will keep the "working" table in the database permanently

Now we create our DTS package. First we create a SQL Connection to connect to the concertdates database:

SQL Connection in DTS Designer

Then we add an Execute SQL Task that checks to see if the “tbltempconcertdates” table exists. If it exists then it is truncated, otherwise it is created.

The Execute SQL Task statement

At this point we have a choice on how to import the data-either a Bulk Insert Task or Transform Data Task. Since we will be performing the data transformation once the data is inside the database, the Bulk Insert Task is the fastest way to get the information there.

The Bulk Insert Task

We create two database connections, both of which connect to the concertdates database. We choose the view as the source and the permanent table as the destination. Since the view is manipulating the data in the “working” table to a valid date format, the transformations are set to copy the data as is:

Screenshot of transformations

Here is a diagram of the package:

DTS Package for Invalid Date Transformation

Next Steps

  • When faced with invalid date formats that must be imported, consider the use of views to manipuate the format
  • Stay tuned for a future tip focusing on using ActiveX Scripts to load and convert data
  • Stay tuned for a future tip focusing on time the DTS data load time trails
  • Read MSDN's DTS Overview
  • Review information on the Bulk Insert Task and Execute SQL Task
  • Review information on the DTS Connections
  • Read additional tips on Data Transformation Services from MSSQLTIPS


Related Tips: More | Become a paid author


Last Update: 10/23/2006

Share: Share 






Comments and Feedback:


Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
"SQL doctor ROCKS! As soon as I ran it, problems that have been giving me headaches were identified and cured."

It takes just 5 minutes to connect your SQL Databases to source control. Got 5 minutes? Get started now.

SQL Servers keeping you up at night? Contact the Edgewood SQL Server DBAs for a Health Check. Prices start at $995.

Get SQL Server Tips Straight from Kevin Kline.

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

Demystify TempDB Performance and Manageability


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com