Solved

How to Convert varchar Date String into DateTime in a Stored Procedure

Posted on 2011-03-02
4
744 Views
Last Modified: 2012-08-13
I have this stored procedure:
USE [ETL]
GO
/****** Object:  StoredProcedure [dbo].[LoadALSTable]    Script Date: 03/02/2011 17:33:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[LoadALSTable]
AS

    SET NOCOUNT ON 

    TRUNCATE TABLE dbo.ALSImportTable

    BULK INSERT  dbo.ALSImportTable FROM 'C:\Sites\_IMPORT.csv'
    WITH (FIELDTERMINATOR = ',', FIRSTROW = 2)
         

    RETURN (@@ERROR)

Open in new window


I need to modify it so that as it's buk inserting date fields, it converts them into the datetime values for my datetime columns.

Any easy way to do this?
0
Comment
Question by:zberg007
4 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 35023149
What is the datatype of the column in ALSImportTable table? If it is datetime, then you need not to convert it to datetime again.
But the bulk insert fails if you try to insert non-date values to that column.
0
 
LVL 17

Assisted Solution

by:dbaSQL
dbaSQL earned 250 total points
ID: 35024165
This will convert a string to datetime:
SELECT CONVERT(CHAR(10),CAST(stringdate AS DATETIME),121) FROM dbo.tablename

But I don't believe this is usable with your bulk insert.

As sharath said, the bulk insert will fail if you try to insert non-datetime values into your datetime column.

You could possibly load the data to a working table, and then use ISDATE to determine what values are invalid datetime format.

SELECT stringdate FROM workingtable WHERE ISDATE([stringdate])=0
0
 
LVL 4

Expert Comment

by:samijsr
ID: 35025462
It is not advisable to convert varchar data to date time at bulk insert, as most it cause of failure.
if non datetime data occured.

Better you use bulk Insert as same varchar datatype and then convert that column data type.
How ever dbaSQL sugesstion is good for converting Varchar data to datetime
0
 

Author Closing Comment

by:zberg007
ID: 35029358
Both of these experts helped on this solution. Thanks!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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