Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Msg 241 Level 16 (Conversion failed when converting datetime from character string) Expand / Collapse
Author
Message
Posted Monday, November 05, 2012 2:22 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 22, 2013 12:46 PM
Points: 14, Visits: 61
Hello everyone, I'm stumped right now. Here is the deal, I need to replicate data from one table to another (basically archiving data) but there is a TON of logic happening before the archiving query (shown below) is called. To give you some background, I am dynamically keeping my local table schemas mirrored to the external tables I am pulling down. Some of these tables have over 500 columns (yes I am getting that warning about exceeding my limit). Either way, the reason I share this is that dyncamic SQL is not going to work, because I can't fit the insert statement into a variable due to the amount of columns.

Now I have performed an sp_columns on BOTH table1 and table2, and they are EXACTLY the same... obviously table 1 has a column that table 2 doesnt (Record_Change_Date) and that column is set as a DateTime. Either way, when I run this, I get the "Conversion failed" error.

Any ideas??

--------- CODE ------------

Declare @changeDate As DateTime

Set @changeDate = GETDATE()

Insert Into [Table1]
Select *, Convert(DateTime, @changeDate) As [Record_Change_Date]
From [Table2]
Where 1=1
And Record_ID NOT IN (Select Record_ID From Table1)

Post #1381265
Posted Monday, November 05, 2012 2:30 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 4:59 PM
Points: 960, Visits: 1,924
Why are you trying to convert a datetime into datetime? isn't that redundant?


Luis C.
Please don't trust me, test the solutions I give you before using them.
Forum Etiquette: How to post data/code on a forum to get the best help
Post #1381266
Posted Monday, November 05, 2012 2:34 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 22, 2013 12:46 PM
Points: 14, Visits: 61
Yes... I was trying everything possible. I originally didn't do anything with it.. it used to be select *, @changeDate As [Record_Change_Date]

So I tried it just using the var, I tried casting it, I tried converting it. I went to the source table and made sure that every DateTime field in the source table returned 1 when the IsDate() function was called. (which meant getting rid of all the nulls).

So now every datetime field in the source table returns 1 when is checked against the IsDate function.

I have no idea why this is happening, and if this doesn't work, I'll need to rework the whole thing, which I really dont want to do.
Post #1381269
Posted Monday, November 05, 2012 2:48 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, May 20, 2013 4:33 AM
Points: 25, Visits: 112
Hi,

Execute the following on both the tables and paste the output
sp_help 'tableA'

Thanks
Satyen
Post #1381273
Posted Monday, November 05, 2012 2:53 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, May 20, 2013 4:33 AM
Points: 25, Visits: 112
also Please try executing the query without 'where 1=1' statement

Thanks
Satyen
Post #1381275
Posted Monday, November 05, 2012 3:14 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 22, 2013 12:46 PM
Points: 14, Visits: 61
LOL I dont know why everyone gets cross-eyed at the 1=1 (it's just a syntax used for easier troubleshooting and it's ignored by the query engine, so no overhead)

I've attached the output of sp_help to this post.

Oh and just FYI... to get you the appropriate sp_help, I went ahead and stuck the results of my select statement into a temp table, so the sp_help results for table 1 was pulled from a temp table.

In essence the file represents this:
Insert Into DestTable
Select T1.*, @changeDate As [Record_Change_Date]
Into ##tempTable
From SourceTable

so sp_help for 3 tables temp, source, and dest tables.


  Post Attachments 
sp_Help_Results.xls (11 views, 108.00 KB)
Post #1381279
Posted Monday, November 05, 2012 3:29 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 8,641, Visits: 8,273
Looks like all you need is:

Insert Into [Table1]
Select *, GETDATE()
From [Table2]
Where Record_ID NOT IN (Select Record_ID From Table1)

You could also make your Record_Change_date not null with a default of getdate().


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1381283
Posted Monday, November 05, 2012 3:45 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, May 20, 2013 4:33 AM
Points: 25, Visits: 112
Hi David,

You have an incorrect query thats the reason why I asked for sp_help tables.

1. When you do an insert with record_change_date the end then it means the last column is record_change_date
2. But record_change_date is your 128th column and not the last
3. Hence you will have to specify the query like this

Insert Into [TableB]
Select column1,column2...column 127th,getdate(),column128th...column146th
From [tableA]
And name NOT IN (Select name From TableA)


Regards
Satyen
Post #1381290
Posted Monday, November 05, 2012 3:48 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 22, 2013 12:46 PM
Points: 14, Visits: 61
Sean Lange (11/5/2012)
Looks like all you need is:

Insert Into [Table1]
Select *, GETDATE()
From [Table2]
Where Record_ID NOT IN (Select Record_ID From Table1)

You could also make your Record_Change_date not null with a default of getdate().


I dont think I can do this, how would SQL know where "GETDATE()" goes? I have to alias it so it can match it up to the destination schema.

I could use getdate(), I've just always followed a practice of putting into a variable so I can use the same date throughout my process.
Post #1381291
Posted Monday, November 05, 2012 3:50 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, April 22, 2013 12:46 PM
Points: 14, Visits: 61
menon.satyen (11/5/2012)
Hi David,

You have an incorrect query thats the reason why I asked for sp_help tables.

1. When you do an insert with record_change_date the end then it means the last column is record_change_date
2. But record_change_date is your 128th column and not the last
3. Hence you will have to specify the query like this

Insert Into [TableB]
Select column1,column2...column 127th,getdate(),column128th...column146th
From [tableA]
And name NOT IN (Select name From TableA)


Regards
Satyen


I really hope that's not true, I figured as long as the schema's match it would work (I even thought I tried this before). If i have to list the columns it's just not going to work. That would kill the ability to dynamically update the schemas.

I'll take what you say into account and realize it just might not be possible.
Thanks
Post #1381293
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse