Solved

import text datetime into SQL

Posted on 2012-12-28
8
426 Views
Last Modified: 2012-12-29
How would I get the last column of the below text table into an SQL field that is a datetime field?  The first two columns are imported into varchar fields.  The last column needs to be converted into a datetime.


0016070|06|20141001|
0016071|06|20141001|
0016072|06|20141001|
0016073|06|20141001|
0016074|06|20141001|
0016075|06|20141001|
0016076|06|20141001|
0016077|06|20141001|
0016078|06|20141001|
001607B|06|20141001|
00160J0|06|20141001|
00160J1|06|20141001|
0
Comment
Question by:dastaub
8 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 167 total points
Comment Utility
cast(thatpart as date)
will work, as the format yyyymmdd is implicitly ok.

see also this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html
0
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 167 total points
Comment Utility
convert(datetime, {value}, 112)

ref http://msdn.microsoft.com/en-us/library/ms187928.aspx
0
 
LVL 12

Assisted Solution

by:sachitjain
sachitjain earned 166 total points
Comment Utility
You may need to first import or bulk insert these records into some staging table with all varchar columns then you could do like this

begin transaction
      insert into ActualTable (Col1, Col2, Col3)
      select Col1, Col2, CONVERT(datetime, Col3, 112) from StagingTable
      delete from StagingTable
commit
0
 

Author Comment

by:dastaub
Comment Utility
INSERT INTO orders1 ( order_date , ordering_client_id )
VALUES ( cast('20121215' as datetime), 2 )

The above works.  How do you conver this to import hundreds of row?
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
Depends how are you currently importing your hundreds of rows?
0
 

Author Comment

by:dastaub
Comment Utility
i do not know how to import hundreds of rows
0
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
Your question only asks how to import the datetime component, you say "The first two columns are imported into varchar fields". which implies you are already importing the varchar data.

Your question as such has been answered - you should probably ask another question if you need to know how to go about doing an import. And you'll need to specify what format the data is in that you are trying to import e.g. is it just a text file with the data as you listed, and roughly how many rows are there?
0
 

Author Comment

by:dastaub
Comment Utility
tried to sneak one in or keep the conversation going
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

728 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

10 Experts available now in Live!

Get 1:1 Help Now