Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-02
4
Medium Priority
?
772 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 1000 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

730 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