Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CONVERT nvarchar to datetime


CONVERT nvarchar to datetime

Author
Message
2Tall
2Tall
Say Hey Kid
Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)

Group: General Forum Members
Points: 708 Visits: 1203
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

Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24295 Visits: 37999
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.

Cool
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)
2Tall
2Tall
Say Hey Kid
Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)

Group: General Forum Members
Points: 708 Visits: 1203
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 Smile

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

2Tall
2Tall
Say Hey Kid
Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)Say Hey Kid (708 reputation)

Group: General Forum Members
Points: 708 Visits: 1203
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

Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24295 Visits: 37999
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.

Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search