Link to home
Start Free TrialLog in
Avatar of zberg007
zberg007

asked on

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

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?
ASKER CERTIFIED SOLUTION
Avatar of Sharath S
Sharath S
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of samijsr
samijsr

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
Avatar of zberg007

ASKER

Both of these experts helped on this solution. Thanks!