Solved

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

Posted on 2007-12-03
2
1,933 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Read about achieving the basic levels of HRIS security in the workplace.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

932 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

12 Experts available now in Live!

Get 1:1 Help Now