如何在使用OpenXML读取XML时将UTC转换为本地时间?

时间:2016-10-18 19:39:47

标签: sql sql-server-2008 tsql utc sql-server-openxml

数据存储为XML(样本): -

<root>
    <data>
        <checked>true</checked>
        <datetimestamp>2016-10-18T08:11:12-06:00</datetimestamp>
    </data>
    <data>
        <checked>true</checked>
        <datetimestamp>2016-10-18T08:13:20-06:00</datetimestamp>
    </data>
</root>

我正在使用OpenXML读取数据: -

DECLARE @XData XML
DECLARE @doc INT

DECLARE @TempXML TABLE
(
    checked BIT,
    datetimestamp DATETIME,
)  

SELECT @XData = Data FROM Table1 WHERE ID = @ItemId

EXEC sp_xml_preparedocument @doc OUTPUT, @XData

INSERT INTO @TempXML(checked,datetimestamp)  
    SELECT * FROM OPENXML (@doc, '/root/data',1)
    WITH (
    checked BIT 'checked',
    DateTimeStamp DATETIME 'datetimestamp',
    )

DateTime以XML格式存储为UTC。在上面的SP我正在使用openXML读取xml数据并将数据存储在临时表中。 我需要在插入时将UTC日期时间转换为本地时间。我怎样才能做到这一点。 以下代码行将UTC完全转换为本地时区但无法弄清楚如何将其与Openxml 一起使用: -

SELECT DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), MyTable.UtcColumn) 
       AS ColumnInLocalTime
FROM MyTable

1 个答案:

答案 0 :(得分:0)

您是否只能在INSERT INTO @TempXML(checked,datetimestamp) SELECT checked ,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), datetimestamp) as datetimestamp FROM OPENXML (@doc, '/root/data',1) WITH ( checked BIT 'checked', DateTimeStamp DATETIME 'datetimestamp' ) 声明中指定列?

$sqlserver = 'currentserver'
$testservername = 'newcurrentserver'
$orgineel_submap = 'D:\Jobs_from_Server\Jobs_Orgineel_opdr_2.3'
$orgineel_submapnew ='D:\Jobs_from_Server\GUHQTSQL1281_TDBMSBI\Jobs_Orgineel_opdr_2.3\succes'
$orgineel_failure = 'D:\Jobs_from_Server\Jobs_Orgineel_opdr_2.3\failure'

$fileserver = Get-ChildItem $orgineel_submaporgineel *.sql 
$stringfile = '@command=N''/FILE'
$stringisserver = '@command=N''/ISSERVER'
$commandline = '@command=N'
$replaceline ="/Server " + """" + "\" + """" + $sqlserver + '\"" /Par "\"$ServerOption::LOGGING_LEVEL(Int16)\"";1 /Par "\"$ServerOption::SYNCHRONIZED(Boolean)\"";True /CALLERINFO SQLAGENT' 


foreach ($fileser in $fileserver) {

   # $doelbestand = $fileser.name + '.goed'

   $succes = "$submapchanged\" + $fileser.name #.replace("_oudeserver","_goed")
   $failure =  "$submapfailed\" + $fileser.name.replace("_goed","_fout")

    $filename = $fileser.fullname 
    $content = Get-Content $filename

 #Replace servername if the@command in the sql job is '@command=N''/ISSERVER'
    if ($content -match $commandline_new){
                 $content = $content -replace $sourceserver, $newsqlserver}

 #Here the @command lines that are now '@command=N''/FILE' are being traformed to the '@command=N''/ISSERVER' standard

    if ($content -match $commandline_old){
       $step1 =$content -replace "/CONFIGFILE.*\"" " 
       $step2 = $step1 -replace "/CHECKPOINTING OFF" , $stringtoreplace
       $step3 = $step2 -replace """\\""D:.*dtsx", $tijdelijkeveranderingregeltest 
       $content = $step3 -replace $commandline_old, $commandline_new

       } 

ForEach ($line in $content) 
{ 
  #write-host $line 
  if ($line.Contains($commandline)) 
  { 
     $index = $line.IndexOf($commandline) 
     $command = $line.Substring($index + 11, 9)

     if ($command -eq '/ISSERVER')
     {
     #       write-host 'GOED!'
      $content |out-file  $succes 
     }
     else
     {
           # write-host 'FOUT!'

      $content |out-file  $failure 
     }
 }} } 

#Removes the File die rejected is maar nog wel in de Job_aangepast staan
$rejected =  Get-ChildItem  $submapfailed *.sql  
foreach ($reject in $rejected) {  
    $fullname = $reject.fullname.replace("_fout", "_goed").replace("Jobs_Gefaald","Jobs_Aangepast")
    Remove-Item $fullname -erroraction silentlycontinue
    }