Solved

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

Posted on 2007-12-03
2
1,938 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
ID: 20399972
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:
Ted Bouskill earned 450 total points
ID: 20400158
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

791 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