2

I have looked for answers on this subject and I have posted in another forum but this seems to be the font of all knowledge. I am trying to pull data from a SQL Server 2000 database using PowerShell. The Powershell script calls a stored procedure, this then extracts the data and export-csv outs it to, well a CSV. The problem is that the datetime fields in the outputted data have lost their milliseconds. This happens when PowerShell extracts the data from the database into a temporary table. Here are the two bits of code.

PowerShell script

#VARIABLES
$SqlQuery = "SAP_proc"
#Connection Strings
$Server = "server"
$Database = "db_dab"
#Output Files
$OutputPath = "c:\Sapout.csv"
#END OF VARIABLES

#SQL Connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$Server;Database=$Database;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "SAP_proc"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$DataOut = $SqlAdapter.Fill($DataSet)
#$DataOut = $SqlAdapter.FillSchema($DataSet,[System.Data.SchemaType]::Source)
#Data Manipulation
$DataOut | Out-Null
#Export Data to Hash Table
$DataTable = $DataSet.Tables[0] 
#Export Has table to CSV
$DataTable | Export-CSV -Delimiter "," -Encoding Unicode -Path $OutputPath -NoTypeInformation
$SqlConnection.Close()

Stored procedure

ALTER proc [dbo].[SAP_proc]
as
DECLARE @return_value int
DECLARE @starttime datetime
DECLARE @endtime datetime

SET @starttime = DATEADD (dd, -30, CURRENT_TIMESTAMP)
SET @endtime = CURRENT_TIMESTAMP

EXEC    @return_value = [dbo].[sp_agent]
        @starttime
        ,@endtime 

SELECT  'Return Value' = @return_value

Because the other stored procedure SP_agent is created by the software I can't edit it. Also I don't want to replicate the software defined stored procedure (with SELECT convert to varchar for datetime) in my command text string as it is a behemoth stored procedure.

Any help would be massively useful.

1
  • No unfortunately not. When i run the stored procedure SAP_proc (which i created to be able to use dateadd and current_timestamp) it returns the DateTime with milliseconds, the problem comes when extracting to the DataSet in PowerShell, when I step through the script and I simply display DataTable as opposed to export it to a CSV there is no millisecond data. If I run the SP through SQL manager and export the result grid to CSV i have millisecond data. Yes i know SP_agent is badly named. I didn't create it the software on the server did.
    – wbbigdave
    Oct 12, 2012 at 11:15

1 Answer 1

1

It's not a Powershell issue or a temp table issue. This is because your datetime column is converted to a string when you call export-csv using the default tostring method which doesn't include milliseconds. If you want milliseconds then specify it in the tostring method call:

$a = get-date
$a.ToString("d/M/yyyy hh:mm:ss.fff tt")

#To to something similar with your export-csv of a datatable you can create an expression:

$DataTable | select column1, column2, @{n=datecolumn;e={$_.datecolumn.ToString("d/M/yyyy hh:mm:ss.fff tt")}} | export-csv <rest of code>

Edited 10/17/2012 It seems like you are having trouble with this still. So here's a complete script which I've tested outputs milliseconds. You'll need to change the variable section to your environment. I hope this helps:

#VARIABLES
$SqlQuery = "select 'test' as column1, 'milliseconds' as column2, getdate() as datecolumn"
#Connection Strings
$Server = "$env:computername\sql1"
$Database = "tempdb"
#Output Files
$OutputPath = "./millisec.csv"
#END OF VARIABLES

#SQL Connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$Server;Database=$Database;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$DataOut = $SqlAdapter.Fill($DataSet)
#$DataOut = $SqlAdapter.FillSchema($DataSet,[System.Data.SchemaType]::Source)
#Data Manipulation
$DataOut | Out-Null
#Export Data to Hash Table
$DataTable = $DataSet.Tables[0] 
#Export Has table to CSV
#$DataTable | Export-CSV -Delimiter "," -Encoding Unicode -Path $OutputPath -NoTypeInformation
$DataTable | select column1, column2, @{n='datecolumn';e={$_.datecolumn.ToString("M/d/yyyy hh:mm:ss.fff tt")}} | export-csv $OutputPath -NoTypeInformation -force

$SqlConnection.Close()

#Another code example with explanation. Added on 10/23/2012

#VARIABLES
$SqlQuery = "select getdate() as datecolumn"
#Connection Strings
$Server = "$env:computername\sql1"
$Database = "tempdb"


$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$Server;Database=$Database;Integrated Security=True"
$SqlConnection.Open() 
$SqlCmd = new-Object System.Data.SqlClient.SqlCommand($SqlQuery, $SqlConnection) 
$data = $SqlCmd.ExecuteScalar() 
$SqlConnection.Close()

#Notice NO millisecond
Write-Output $data

#See $data is an object of type System.DateTime
$data | gm

#There's a property called millisecond
#Millisecond          Property       int Millisecond {get;} 

#Although you don't "see" millisecond's on screen it's still there
$data.Millisecond


#Powershell uses a types and format rules to define how data is display on screen. You can see this by  looking at 
#C:\Windows\System32\WindowsPowerShell\v1.0\types.ps1xml and searching for "DataTime". This file along with format file define how data is displayed
#When you display datatime screen it's implicitly calling
#Searching for datetime in types.ps1xml you'll find this line:
#"{0} {1}" -f $this.ToLongDateString(), $this.ToLongTimeString()

#for our example

"{0} {1}" -f $data.ToLongDateString(), $data.ToLongTimeString()

#So millisecond is still there, but if you want millisecond in your output to CSV, screen or file you'll need to call a ToString method with a date format. Here's an example:
$data.ToString("M/d/yyyy hh:mm:ss.fff tt")
8
  • Ok I am trying this now but I am unsure of the syntax on the @{n=datecoumn; section, what is the datecolumn. I keep getting errors thrown out.
    – wbbigdave
    Oct 17, 2012 at 12:19
  • Ok an quick update, It is not when I export to CSV that this problem arrises. It's after I fill the $DataTable with the $DataSet.Table[0] command. If i call the $DataTable after that then I loose the millisecond data.
    – wbbigdave
    Oct 17, 2012 at 12:50
  • The problem I have is I can't use a select query as it is all based on calling a stored procedure. I can't use the stored procedure I call within my smaller stored procedure as it is too long (about three pages). Not sure what to do now. The millisecond data seems to be dropped when I fill the DataSet as I have displayed the dataset after it was filled.
    – wbbigdave
    Oct 23, 2012 at 11:15
  • 1
    I'll try to explain this in more detail. See my last code example of above. Oct 23, 2012 at 13:58
  • 1
    Glad to help. Also if you're only returning one row then you can use executescalar as shown in the last code example instead of filling a dataset/datatable. Oct 23, 2012 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.