Solved

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

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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

707 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