Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke-sqlcmd output without "quotes" and headers

Output example:

#TYPE System.Data.DataRow <---
"PersonalNr" <---
"00001005"
"00001008"
"00001009"
"00001013"
"00001019"
"00001024"

Requirements:

I want a output without the 2 first lines and without the quote symbols, how can i do that?

For the headers I tried the options -h -1 but the output is still the same. Is it even possible with Sqlcmd?

Current Code:

Invoke-Sqlcmd -ServerInstance SERVER -h -1 -Query $QueryFmt | Export-CSV $AttachmentPath
like image 643
Moek Avatar asked Mar 09 '23 12:03

Moek


2 Answers

This code should work:

Invoke-Sqlcmd -ServerInstance "" -Database "" -Query "" | ConvertTo-Csv -NoTypeInformation -Delimiter "," | Select-Object -Skip 1 | % {$_ -replace '"', ""} | Out-File ("C:\test.csv") -Force -Encoding ascii

Invoke-Sqlcmd produces:

PersonalNr       
---    
00001005  
00001001  
00001006  
00001007  
00001008 

With Export-Csv, the file looks like:

#TYPE System.Data.DataRow
"PersonalNr"
"00001005"
"00001001"
"00001006"
"00001007"
"00001008"

Using my above mentioned code, I get the file looking like this:

00001005
00001001
00001006
00001007
00001008
like image 76
dbso Avatar answered Mar 21 '23 10:03

dbso


Try this,

$query_result | Format-Table -AutoSize -Wrap -HideTableHeaders

like image 29
Nirav Mistry Avatar answered Mar 21 '23 09:03

Nirav Mistry