Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now