You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add -f codepage flag for input/output encoding
Implements the -f flag for specifying input/output file encoding:
- Format: codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage]
- Use 65001 for UTF-8
- --list-codepages shows all supported encodings
Windows uses native MultiByteToWideChar/WideCharToMultiByte APIs for
codepages not in the golang.org/x/text registry.
Copy file name to clipboardExpand all lines: README.md
+74Lines changed: 74 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -151,6 +151,7 @@ The following switches have different behavior in this version of `sqlcmd` compa
151
151
- To provide the value of the host name in the server certificate when using strict encryption, pass the host name with `-F`. Example: `-Ns -F myhost.domain.com`
152
152
- More information about client/server encryption negotiation can be found at <https://docs.microsoft.com/openspecs/windows_protocols/ms-tds/60f56408-0188-4cd5-8b90-25c6f2423868>
153
153
-`-u` The generated Unicode output file will have the UTF16 Little-Endian Byte-order mark (BOM) written to it.
154
+
-`-f` Specifies the code page for input and output files. See [Code Page Support](#code-page-support) below for details and examples.
154
155
- Some behaviors that were kept to maintain compatibility with `OSQL` may be changed, such as alignment of column headers for some data types.
155
156
- All commands must fit on one line, even `EXIT`. Interactive mode will not check for open parentheses or quotes for commands and prompt for successive lines. The ODBC sqlcmd allows the query run by `EXIT(query)` to span multiple lines.
156
157
-`-i` doesn't handle a comma `,` in a file name correctly unless the file name argument is triple quoted. For example:
@@ -255,6 +256,79 @@ To see a list of available styles along with colored syntax samples, use this co
255
256
:list color
256
257
```
257
258
259
+
### Code Page Support
260
+
261
+
The `-f` flag specifies the code page for reading input files and writing output. This is useful when working with SQL scripts saved in legacy encodings or when output needs to be in a specific encoding.
262
+
263
+
#### Format
264
+
265
+
```
266
+
-f codepage # Set both input and output to the same codepage
267
+
-f i:codepage # Set input codepage only
268
+
-f o:codepage # Set output codepage only
269
+
-f i:codepage,o:codepage # Set input and output to different codepages
270
+
-f o:codepage,i:codepage # Same as above (order doesn't matter)
271
+
```
272
+
273
+
#### Common Code Pages
274
+
275
+
| Code Page | Name | Description |
276
+
|-----------|------|-------------|
277
+
| 65001 | UTF-8 | Unicode (UTF-8) - default for most modern systems |
- When no `-f` flag is specified, sqlcmd auto-detects UTF-8/UTF-16LE/UTF-16BE BOM (Byte Order Mark) in input files and switches to the appropriate decoder. If no BOM is present, UTF-8 is assumed.
317
+
- UTF-8 input files with BOM are handled automatically.
318
+
- On Windows, additional codepages installed on the system are available via the Windows API, even if not shown by `--list-codepages`.
319
+
- Use `--list-codepages` to see the built-in code pages with their names and descriptions.
320
+
321
+
#### Differences from ODBC sqlcmd
322
+
323
+
| Aspect | ODBC sqlcmd | go-sqlcmd |
324
+
|--------|-------------|-----------|
325
+
|**Default encoding (no BOM, no `-f`)**| Windows ANSI code page (locale-dependent, e.g., 1252) | UTF-8 |
326
+
|**UTF-16 codepages (1200, 1201)**| Rejected by `IsValidCodePage()` API | Accepted |
|**`--list-codepages`**| Not available | Available |
329
+
330
+
**Migration note**: If you have UTF-8 encoded SQL scripts without a BOM that worked with ODBC sqlcmd on Windows, they should work identically or better with go-sqlcmd since go-sqlcmd defaults to UTF-8. However, if you have scripts in Windows ANSI encoding (e.g., Windows-1252) without a BOM, you may need to explicitly specify `-f 1252` with go-sqlcmd.
rootCmd.Flags().StringVarP(&args.ChangePasswordAndExit, "change-password-exit", "Z", "", localizer.Sprintf("New password and exit"))
504
+
rootCmd.Flags().StringVarP(&args.CodePage, "code-page", "f", "", localizer.Sprintf("Specifies the code page for input/output. Use 65001 for UTF-8. Format: codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage]"))
505
+
rootCmd.Flags().BoolVar(&args.ListCodePages, "list-codepages", false, localizer.Sprintf("List supported code pages and exit"))
0 commit comments