-
Notifications
You must be signed in to change notification settings - Fork 723
Expand file tree
/
Copy pathExport_Query_To_Csv.ps1
More file actions
40 lines (35 loc) · 1.54 KB
/
Copy pathExport_Query_To_Csv.ps1
File metadata and controls
40 lines (35 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#requires -version 5.0
#requires -modules sqlserver
<#
.SYNOPSIS
Export SQL server table to csv file with quotes (if needed) and header
.DESCRIPTION
Piping Invoke-Sqlcmd into csv file with quotes only if needed.
Also replace False and True on 0 and 1 for bit types columns.
.NOTES
Original link: http://www.sqlmovers.com/removing-quotes-from-csv-created-by-powershell/
Author: Russ Loski
Version: 1.3
Modified: 2020-01-09 by Konstantin Taranov
Source link: https://github.com/ktaranov/sqlserver-kit/blob/master/Powershell/Export_Query_To_Csv.ps1
#>
$databaseName = "master";
$fileName = $tableName = "sys.objects";
$delimiter = ","; # possible values "`t", ",", ";", "|"
if ($delimiter -eq "|") {
$delimiterReg = $delimiterBit = "\|";
} else {
$delimiterReg = $delimiterBit = $delimiter;
};
$regReplace = '\G(?<start>^|' + $delimiterReg + '+)(("(?<output>[^' + $delimiterReg + '"]*?)"(?=' + $delimiterReg + '|$))|(?<output>".*?(?<!")("")*?"(?=' + $delimiterReg + '|$)))';
Invoke-Sqlcmd -Query "SELECT * FROM $tableName;" `
-Database $databaseName `
-Server localhost |
ConvertTo-CSV -NoTypeInformation `
-Delimiter $delimiter |
% {$_ -replace $regReplace, '${start}${output}'} |
% {$_ -replace ($delimiterBit + 'False'), ($delimiter + '0')} |
% {$_ -replace ('^False'), '0'} |
% {$_ -replace ($delimiterBit + 'True'), ($delimiter + '1')} |
% {$_ -replace ('^True'), '1'} |
Out-File "$fileName.csv" -fo -en utf8;