Solved

Importing Dates from Strings in "DD-MMM-YYYY HH.NN.SS.ssss" Format

Posted on 2007-12-03
2
1,930 Views
Last Modified: 2008-02-01
Hello,

I have a data file that I receive daily from another source.  I cannot change the format of the datafile.  I need to import the contents of the file into my SQL database (MS SQL Server 2000), and then perform various analyses.  Two of the fields are datetime fields in the format: "DD-MMM-YYYY HH.NN.SS.ssss".  I need the fields to be true datetime fields in my database.

A simple CAST or CONVERT gives me an error, saying that the string could not be converted to a date, presumably because of the month being in MMM format?

Is there a way to manipulate these date strings so that they can be properly imported?
0
Comment
Question by:gethryn
2 Comments
 
LVL 1

Assisted Solution

by:wizengamot
wizengamot earned 50 total points
Comment Utility
The short answer is YES.  SQL supports the full ISO date / time format.  If your can modify your time string to meet the following standard

1995-12-31 23:59:59 or in other words YYYY-MM-DD HH:MM:SS

You can lop off what you don't need, so if say you don't want seconds, just don't include it in your string and it will be assigned a value of 00.  You cannot lop off from the other end but I don;t think you would want to lose the year value anyways.

Please note that the values range from large to small.  If the value is exactly as I have listed it here, it can be inserted into a datetime field in sql 2000 with no troubles.  This standard is the ISO 8601 date standard.  I have written a function in vb.net to peform this task for me but due to copyright laws with where I work I cannot post it as is, and I do not have a copy that I have written on my own time yet.  

The statement that would insert this into a database datetime field follows in the code block.
Insert Into MyTable (Currentdate) Values('1995-12-31 23:59:59')

Open in new window

0
 
LVL 51

Accepted Solution

by:
tedbilly earned 450 total points
Comment Utility
Actually it was the time that caused a problem on my system.  See the following.
DECLARE @s CHAR(25)

SET @s = '01-JAN-2007 13.13.13.1313'
 

--PRINT CONVERT(DATETIME, @s) -- Fails

PRINT CONVERT(DATETIME, LEFT(@s,11))+CONVERT(DATETIME, LEFT(RIGHT(REPLACE(@s,'.',':'),13),12))

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now