Solved

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

Posted on 2011-03-02
4
761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 41

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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query to Add Late Tolerance 10 85
upgrade sql 2005 32bit to sql 2008 32 or 64bit on a server 2008 r2 box 6 101
CROSS APPLY 4 69
Need help in debugging a UDF results 7 71
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Copy Database Wizard 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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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