?
Solved

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

Posted on 2007-12-03
2
Medium Priority
?
1,959 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Assisted Solution

by:wizengamot
wizengamot earned 200 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 1800 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
In this article, we’ll look at how to deploy ProxySQL.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

770 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