Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Converting date and/or time from character string.

$
0
0
Hi all, I have a basic problem (I hope) whereas I'm trying to insert 4m rows from one table to another, but I'm struggling with dates.The table the data is coming from are all nvarchar fields, and the target is a datetime. So, I'm having to put the date (dd/mm/yy) into the correct format as shown below.Msg 241, Level 16, State 1, Line 3Conversion failed when converting date and/or time from character string.Now, I've tried to only import where the field is actually recognised as a date by using the "isDate" function (see below), but even with this, I still get the same error.if I execute the 'select' part of the statement, 1000 rows return without error. its just when I'm trying to insert them.[code="sql"] insert into tbl_InvoiceHead ([Invoice_Number],[Invoice_Date],[Call_ETA_Date])select top 1000[invoice_number],convert(datetime,'20'+right(invoice_date,2)+'-'+ right(left(invoice_date,5),2)+'-'+left(invoice_date,2) ,102) as invoice_date,convert(datetime,'20'+right(call_eta_date,2)+'-'+ right(left(call_eta_date,5),2)+'-'+left(call_eta_date,2) ,102) as call_eta_dateFROM tbl_OldHeadTablewhere invoice_number not in (select distinct invoice_number from tbl_InvoiceHead) and isdate(convert(datetime,'20'+right(invoice_date,2)+'-'+right(left(invoice_date,5),2)+'-'+left(invoice_date,2) ,102)) =1and isdate(convert(datetime,'20'+right(call_eta_date,2)+'-'+right(left(call_eta_date,5),2)+'-'+left(call_eta_date,2) ,102)) =1[/code]On the destination table, there is one other datetime field, which has getdate() as default. This is called insertedDate.Any pointers would be much appreciated.Thanks in advance.Dave

Viewing all articles
Browse latest Browse all 3145

Trending Articles