Search code examples
sql-serversqlxml

Could not able to insert the xml data into a table


When i try to insert the values into the table am getting the below error. We cant remove the space in the XML because it is generated from Javascript. How to insert the below data into the XMLdata table.

Conversion failed when converting date and/or time from character string.

This is the sample data(@bbhdn5):

341300-02-1|04/10/2018 01:18:29|04/10/2018 06:18:29|133072;
261600-01-1|04/10/2018 06:18:29|04/10/2018 11:18:29|133073;
781100-R1-1|04/10/2018 11:18:29|04/10/2018 16:18:29|133074;

Code:

create table WMC_Savexmldata 
(
     XML nvarchar(max)
)

Declare @bbhdn5 nvarchar(max) 
set @bbhdn5='341300-02-1|04/10/2018 01:18:29|04/10/2018 06:18:29|133072; 261600-01-1|04/10/2018 06:18:29|04/10/2018 11:18:29|133073; 781100-R1-1|04/10/2018 11:18:29|04/10/2018 16:18:29|133074;'

insert into WMC_Savexmldata
    select @bbhdn5

Solution

  • Although this question is absolutely misleading, my magic crystal ball started to blink suddenly and told me, that you might be looking for this:

    Replacing the delimiters ; and | allows to transfer your CSV-string to this XML:

    <x>
      <y>341300-02-1</y>
      <y>04/10/2018 01:18:29</y>
      <y>04/10/2018 06:18:29</y>
      <y>133072</y>
    </x>
    <x>
      <y> 261600-01-1</y>
      <y>04/10/2018 06:18:29</y>
      <y>04/10/2018 11:18:29</y>
      <y>133073</y>
    </x>
    <x>
      <y> 781100-R1-1</y>
      <y>04/10/2018 11:18:29</y>
      <y>04/10/2018 16:18:29</y>
      <y>133074</y>
    </x>
    

    I use this to get the data as derived table:

    DECLARE @bbhdn5 NVARCHAR(MAX); 
    SET @bbhdn5=N'341300-02-1|04/10/2018 01:18:29|04/10/2018 06:18:29|133072; 261600-01-1|04/10/2018 06:18:29|04/10/2018 11:18:29|133073; 781100-R1-1|04/10/2018 11:18:29|04/10/2018 16:18:29|133074;';
    
    WITH Splitted AS
    (
        SELECT CAST('<x><y>' + REPLACE(REPLACE(@bbhdn5,'|','</y><y>'),';','</y></x><x><y>') + '</y></x>' AS XML) AS Casted
    )
    SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber
            ,A.x.value('y[1]','nvarchar(max)') AS RowCode
            ,CONVERT(DATETIME,A.x.value('y[2]','nvarchar(max)'),103) AS Date1
            ,CONVERT(DATETIME,A.x.value('y[3]','nvarchar(max)'),103) AS Date2
            ,A.x.value('y[4]','int') AS SomeNumber
    FROM Splitted
    CROSS APPLY Casted.nodes('/x[y/text()]') AS A(x);
    

    The result

    +-----------+-------------+-------------------------+-------------------------+------------+
    | RowNumber | RowCode     | Date1                   | Date2                   | SomeNumber |
    +-----------+-------------+-------------------------+-------------------------+------------+
    | 1         | 341300-02-1 | 2018-10-04 01:18:29.000 | 2018-10-04 06:18:29.000 | 133072     |
    +-----------+-------------+-------------------------+-------------------------+------------+
    | 2         | 261600-01-1 | 2018-10-04 06:18:29.000 | 2018-10-04 11:18:29.000 | 133073     |
    +-----------+-------------+-------------------------+-------------------------+------------+
    | 3         | 781100-R1-1 | 2018-10-04 11:18:29.000 | 2018-10-04 16:18:29.000 | 133074     |
    +-----------+-------------+-------------------------+-------------------------+------------+
    

    UPDATE

    Change the line within the CTE Splitted to this

        SELECT CAST('<x><y>' + REPLACE(REPLACE(REPLACE(REPLACE(@bbhdn5,CHAR(10),' '),CHAR(13),' '),'|','</y><y>'),';','</y></x><x><y>') + '</y></x>' AS XML) AS Casted
    

    This will replace CHAR(13) and CHAR(10) with blanks. Any odd line break within your input should disappear. Well, you might have some additional blanks in string values. But you can replace doubled blanks with single blanks again...