Sql 更改查询输出的默认日期格式

Sql 更改查询输出的默认日期格式,sql,sql-server,powershell,format,date-format,Sql,Sql Server,Powershell,Format,Date Format,我正在运行一个庞大的PowerShell脚本,它在数据库中的每个表和视图中循环,并为每个表/视图创建*.CSV文本文件,其中包含整个数据集。 (从中选择*) DATE或DATETIME的任何列都以MM/DD/YYYY 我需要以YYYY/MM/DD 有超过1200个表和视图,因此我无法手动指定每个受影响列的格式 我可以在服务器级别更改默认输出格式吗? ... 还是在数据库级别? ... 还是在会话级别 这是我的PowerShell代码: $server = "myserver" $database

我正在运行一个庞大的PowerShell脚本,它在数据库中的每个表和视图中循环,并为每个表/视图创建*.CSV文本文件,其中包含整个数据集。
中选择*)

DATE
DATETIME
的任何列都以
MM/DD/YYYY

我需要以
YYYY/MM/DD

有超过1200个表和视图,因此我无法手动指定每个受影响列的格式

我可以在服务器级别更改默认输出格式吗?
... 还是在数据库级别?
... 还是在会话级别

这是我的PowerShell代码:

$server = "myserver"
$database = "mydb"
$tablequery = "SELECT s.name AS schema_name, t.name AS table_name FROM sys.tables t LEFT JOIN sys.schemas s ON s.schema_id = t.schema_id UNION ALL SELECT s.name, v.name FROM sys.views v LEFT JOIN sys.schemas s ON s.schema_id = v.schema_id"

#Delcare Connection Variables
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $tablequery
$command.Connection = $connection

#Load up the Tables in a dataset
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$SqlAdapter.SelectCommand.CommandTimeout=600
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()



# Loop through all tables and export a CSV of the Table Data
foreach ($Row in $DataSet.Tables[0].Rows)
{
    $queryData = "SELECT * FROM [$($Row[0])].[$($Row[1])]"

    #Specify the output location of your dump file
    $extractFile = "C:\mydb\$($Row[0])_$($Row[1]).csv"

    $command.CommandText = $queryData
    $command.Connection = $connection

    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $command
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $connection.Close()

    $DataSet.Tables[0]  | Export-Csv $extractFile -NoTypeInformation
}

如果您添加这一行:

[System.Threading.Thread]::CurrentThread.CurrentCulture=[System.Globalization.CultureInfo]"fr-CA"
这样:

    ...
    ...
# Loop through all tables and export a CSV of the Table Data
[System.Threading.Thread]::CurrentThread.CurrentCulture=[System.Globalization.CultureInfo]"fr-CA"
foreach ($Row in $DataSet.Tables[0].Rows)
{
    $queryData = "SELECT * FROM [$($Row[0])].[$($Row[1])]"

    #Specify the output location of your dump file
    $extractFile = "C:\mydb\$($Row[0])_$($Row[1]).csv"

    $command.CommandText = $queryData
    $command.Connection = $connection

    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $command
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $connection.Close()

    $DataSet.Tables[0]  | Export-Csv $extractFile -NoTypeInformation
}

否没有全局设置来指定日期时间结果的格式。数据库返回具有日期或日期时间数据类型的列。这取决于客户端如何呈现它想要的格式。你可以用一些难看的动态sql来强制它。您需要查询sys.columns。然后,select语句需要指定每一列,而不是*。然后用大小写表达式包装每一列,如果该列的数据类型是date或datetime,则可以使用FORMAT函数以所需格式获取数据。@SeanLange Yeah。。。对于我正在处理的所有表和列,该查询可能有数万行长。所以这绝对不是一个选择(由于PowerShell运行在.NET之上,您是否考虑过修改
Thread.CurrentThread.CurrentCulture
设置以影响客户端上DateTime对象的默认序列化格式?由于日期格式众所周知且易于识别,您也可以尝试在查询输出上使用正则表达式repl对所有日期进行自动排序。有关类似的解决方案,请参见此: