Share via

When you run sqlcmd with -s"\t", it does not interpret \t as a tab character

Salam ELIAS 302 Reputation points
2025-11-24T12:20:50.3066667+00:00

I run the following tsql with sqlcmd but output does not get formatted with Tabs

sqlcmd -S SQL2K12 -d Admin -E -s"\t" -W -Q "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='dbo' ORDER BY TABLE_NAME;"

Output:

TABLE_CATALOG\TABLE_SCHEMA\TABLE_NAME\TABLE_TYPE --------------------------------------------- Admin\dbo\Account\BASE TABLE Admin\dbo\AdminUser\BASE TABLE Admin\dbo\ArchiveUtilityChanges\BASE TABLE Admin\dbo\Audit_ByTrigger_Components\BASE TABLE Admin\dbo\Audit_ByTrigger_ComponentsDeleted\BASE TABLE Admin\dbo\BigTable\BASE TABLE Admin\dbo\BlitzWho\BASE TABLE Admin\dbo\BTSHealthStat\BASE TABLE .......... ..........

When asking Copilot, It suggested to use, in powershell a different delimiter and convert to tabs as follows

 .\sqlcmd -S SQL2K12 -d Admin -E -s"|" -W -Q "SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='dbo' ORDER BY TABLE_NAME;" |  ForEach-Object { $_ -replace '\|', "`t" }

which worked fine.

So the question is why -s"\t" does not get interpreted correctly? Thanks

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Marcin Policht 92,635 Reputation points MVP Volunteer Moderator
2025-11-24T12:51:08.41+00:00

AFAIK, The -s switch takes the delimiter literally. So -s"\t" means backslash + t, not an actual tab character. "\t" in PowerShell is just the characters \ and t. PowerShell's escape for a tab is "`t" (backtick-t), not backslash-t.

The workaround with | worked because you forced a delimiter that both PowerShell and sqlcmd can reliably treat as plain text (|), then replaced it with a real tab using PowerShell’s "t"` escape, which is interpreted as a tab.

Try the following

sqlcmd -S SQL2K12 -d Admin -E -s "`t" -W -Q "SELECT ...;"

If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

hth

Marcin

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.