Solved

import text datetime into SQL

Posted on 2012-12-28
8
433 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 167 total points
ID: 38728280
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
ID: 38728281
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
ID: 38728471
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dastaub
ID: 38728573
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
 
LVL 21

Expert Comment

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

Author Comment

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

Expert Comment

by:Dale Burrell
ID: 38728646
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
ID: 38729475
tried to sneak one in or keep the conversation going
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
Need time in SQL 8 30
SCOM to SQL port 14xx failed? 1 29
removing unwanted rows from an sql server ranked table 13 34
sql query 5 39
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

713 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