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

CONVERT nvarchar to datetime Expand / Collapse
Author
Message
Posted Thursday, December 18, 2008 8:00 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 5:29 AM
Points: 621, Visits: 1,073
I am attempting to INSERT from a temporary table. I created the temporary table from an excel file. The data types for the temporary table do not match the table I wish to INSERT INTO.

The table I am selecting from has a field 'PurchaseDate' nvarchar(255). The date is held as follows:

2004-04-26 00:00:00

The field I want to INSERT INTO has a data type of datetime

My insert query is as follows:
INSERT INTO [MyDB].[dbo].[CrossReferences]
(
Product,
Supplier,
SupplierSequence,
NormalCost,
DateLastQuoted,
CreatedUser,
CreatedDate,
LastModifiedUser,
LastModifiedDate,
LeadTime,
Comments
)
SELECT
Products.Product,
Suppliers.Supplier,
998,
Price,
cast(substring(PurchaseDate,5,4) + substring(PurchaseDate,1,2) + substring(PurchaseDate,3,2) as datetime),
'admin',
getdate(),
'admin',
getdate(),
999,
' '
FROM dbo.temp
INNER JOIN dbo.Products ON temp.ProductId = Products.ProductID
INNER JOIN dbo.Suppliers ON temp.SupplierId = Suppliers.SupplierID

Error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.

Any Ideas,
Many Thanks,
Phil.


-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper
Post #622110
Posted Thursday, December 18, 2008 8:19 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:22 PM
Points: 22,510, Visits: 30,232
Philip Horan (12/18/2008)
I am attempting to INSERT from a temporary table. I created the temporary table from an excel file. The data types for the temporary table do not match the table I wish to INSERT INTO.

The table I am selecting from has a field 'PurchaseDate' nvarchar(255). The date is held as follows:

2004-04-26 00:00:00

The field I want to INSERT INTO has a data type of datetime

My insert query is as follows:
INSERT INTO [MyDB].[dbo].[CrossReferences]
(
Product,
Supplier,
SupplierSequence,
NormalCost,
DateLastQuoted,
CreatedUser,
CreatedDate,
LastModifiedUser,
LastModifiedDate,
LeadTime,
Comments
)
SELECT
Products.Product,
Suppliers.Supplier,
998,
Price,
cast(substring(PurchaseDate,5,4) + substring(PurchaseDate,1,2) + substring(PurchaseDate,3,2) as datetime),
'admin',
getdate(),
'admin',
getdate(),
999,
' '
FROM dbo.temp
INNER JOIN dbo.Products ON temp.ProductId = Products.ProductID
INNER JOIN dbo.Suppliers ON temp.SupplierId = Suppliers.SupplierID

Error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.

Any Ideas,
Many Thanks,
Phil.


Since you seem to indicate the date in PurchaseDate is already stored as yyyy-mm-dd hh:mi:ss, why are you trying to substring the data to convert it? Your code should be straight forward:

INSERT INTO [MyDB].[dbo].[CrossReferences]
(
Product,
Supplier,
SupplierSequence,
NormalCost,
DateLastQuoted,
CreatedUser,
CreatedDate,
LastModifiedUser,
LastModifiedDate,
LeadTime,
Comments
)
SELECT
Products.Product,
Suppliers.Supplier,
998,
Price,
cast(PurchaseDate as datetime),
'admin',
getdate(),
'admin',
getdate(),
999,
' '
FROM dbo.temp
INNER JOIN dbo.Products ON temp.ProductId = Products.ProductID
INNER JOIN dbo.Suppliers ON temp.SupplierId = Suppliers.SupplierID

If this isn't the case, then you need to provide us more information. I'd suggest that you take a little time to read the article that I have linked below in my signature block regarding asking for assistance.




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #622133
Posted Thursday, December 18, 2008 8:30 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 5:29 AM
Points: 621, Visits: 1,073
Hi Lynn. I tried that initially which returned error:

Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
The statement has been terminated.

I then came a cross a post you had subscribed to so I tried that example :)

Thanks,
Phil.


-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper
Post #622148
Posted Thursday, December 18, 2008 8:38 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 5:29 AM
Points: 621, Visits: 1,073
Back again. I am sure I am missing a trick having reviewed the last error posted so do not waste any time on this thread, I will revisit what I have done.
Many Thanks,
Phil.


-------------------------------------------------------------------------------------
A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

Tommy Cooper
Post #622163
Posted Thursday, December 18, 2008 8:40 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:22 PM
Points: 22,510, Visits: 30,232
If you provide the DDL (create statements) for your tables, sample data for the source table (as insert statements that can be cut, paste, and executed in SSMS) to load that is representative of the entire data set, we can see what is needed to be done.

If you have questions on how to do all this, it is in the article I pointed you toward in my last post.




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #622170
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse