CONVERT nvarchar to datetime

  • 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

  • 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.

  • 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

  • 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

  • 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.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply