Solved

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

Posted on 2011-03-02
4
750 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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