Link to home
Create AccountLog in
Avatar of lulu50
lulu50Flag for United States of America

asked on

Convert string to date

Hi,

I NEED HELP PLEASE! :-)

I have an excel sheet that has a data from a vendor.
One of the field that I need from the excel sheet is the Submit_Date.
they have it as string nvarchar "February 12, 2015 2:07:21 PM EST"
I have created a table with that field as datetime.

the problem that I'm having when I do my import from excel to SQL. it import the Submit_Date as string from excel
but my table " ClearQuestReport" has Submit_Date as datetime .
when I run my stored Procedure I get this error

ERROR:
Msg 241, Level 16, State 1, Procedure CQ_ClearQuestReport, Line 24
Conversion failed when converting date and/or time from character string.

This is my stored Procedure.

what I need is to convert Submit_Date field from string to datetime field when I import the data into my table "ClearQuestReport"

UPDATE ClearQuestReport
SET 
id = ClearQuestReport_Import.id,
Headline = ClearQuestReport_Import.Headline,
Description = ClearQuestReport_Import.Description,
Submited_By = ClearQuestReport_Import.[Submited By],
Submit_Date = ClearQuestReport_Import.Submit_Date
FROM ClearQuestReport
    INNER JOIN ClearQuestReport_Import ON ClearQuestReport.id = ClearQuestReport_Import.id

--This will insert all non matching records
INSERT INTO ClearQuestReport(id,Headline,Description,Submited_By,Submit_Date)
SELECT id,Headline,Description,[Submited By],Submit_Date
FROM ClearQuestReport_Import
WHERE 
NOT EXISTS (SELECT 1 
                 FROM ClearQuestReport
                 WHERE ClearQuestReport.Id = ClearQuestReport_Import.Id)


SELECT * FROM ClearQuestReport

Open in new window


Thank you
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

To avoid this issue an excellent idea would be to ask the vendor for a .csv, not an Excel doc, and then import this file into SQL Server without ever opening in in Excel.

Part of the deal-i-o here is that when Excel opens it will enforce formatting which changes the presentation of the values in a way that causes it to not load into a datetime field correctly.
Avatar of lulu50

ASKER

Jim,

I don't have an access to the vendor.

I need to a way to convert it in my procedure while importing the data.
ASKER CERTIFIED SOLUTION
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hi ,
you can use REPLACE to avoid timezone like:
SELECT CAST( REPLACE('February 12, 2015 2:07:21 PM EST','EST','') AS datetime) AS Submit_Date
is return
2015-02-12 14:07:21.000
in your SP you can use Submit_Date instead of 'February 12, 2015 2:07:21 PM EST'

Best of Luck,,,
Avatar of lulu50

ASKER

Thank you