1

I Have a powershell script that executes a sql query, then exports the information to a CSV file. However the CSV file reformats all DateTimes entry to mm/d/yyyy hh:mm:ss. I need the entries to be in the format yyyy-mm-dd-hh.mm.ss.00

$SqlCmd.CommandText = $sqlSelectCVS
$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.Fill($export)

#Close the SQL connection
$SqlConnection.Close()

#Creates the New file with out Column header inforamtion
$export.tables[0] |export-csv -Path ($ExportTo + '\' + $ExportName) -notypeinformation 
(Get-Content ($ExportTo + '\' + $ExportName)| Select-Object -Skip 1) | Set-Content      ($ExportTo + '\' + $ExportName)

The database connections and query work just fine. Also as a side note Ive tried opening it in notepad with no luck and the date is a future date.. I am still very new to powershell so if you could explain any suggestions so i could understand it, i would appreciate it.

Thanks

1 Answer 1

1

You can use .NET libraries in PowerShell to format the string, like so:

"{0:yyyy-mm-dd-hh.hh.mm.ss.00}" -f [datetime]$stringOrWhateverDateVariable

$stringOrWhateverDateVariable would be the date variable from the SQL table.

I cannot test on an SQL datababe, but it works for strings to your DateTime format.

Sign up to request clarification or add additional context in comments.

2 Comments

Yours works for write-output. but when i transfer it over to a CSV file it changes to "length" "25"
@user2788724 Can you update the question , append an edit at the end with your current export with the method I listed?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.