Skip to content

ODBC date/time format strings#721

Open
mmcnabb-vms wants to merge 8 commits into
FreeTDS:masterfrom
mmcnabb-vms:odbc_datefmt_attribute
Open

ODBC date/time format strings#721
mmcnabb-vms wants to merge 8 commits into
FreeTDS:masterfrom
mmcnabb-vms:odbc_datefmt_attribute

Conversation

@mmcnabb-vms

Copy link
Copy Markdown
Contributor

Allow specifying the format string for ODBC implicit datetime-to-char conversions. Fixes #715. The use case is for people migrating to TDSODBC from other database clients with different date/time formatting.

Configuration Examples :

  • C API: SQLSetConnectAttr(hdbc, SQL_COPT_TDSODBC_DATETIME_FORMAT, "%Y%m%d%H%M%S", SQL_NTS);
  • Defining a connection string DRIVER=FreeTDS;......;DATETIMEFMT={%Y%m%d%H%M%S}

Not urgent to review and merge this, so long as you're happy with the form of the above configuration methods.

The changes:

  • Add new connection attribute to set date, time, or datetime format string. This setting is saved in the DBC.
  • Add connection string attributes to match.
  • Use these format strings instead of hardcoded strings in odbc_tds2sql().
  • Update test suite to include coverage of the new features.
  • Redo the code to compute SQL_DESC_LENGTH and SQL_DESC_DISPLAY_SIZE to return a value based on the current format string. Also, this implicitly fixes what appears to be a bug in the MS ODBC driver regarding the length of smalldatetime.

No matching attribute has been added for ODBC DSNs or for freetds.conf. Does not affect conversion of char to datetime.

	Instead of duplicate instances of hardcoding.

Signed-off-by: Matt McNabb <matthew.mcnabb@vmssoftware.com>
Signed-off-by: Matt McNabb <matthew.mcnabb@vmssoftware.com>
	- Test MSDATETIME2 full precision display.
	- Test customized format strings.
	- Check display size is not exceeded.
	- Exit cleanly on test failure.

Signed-off-by: Matt McNabb <matthew.mcnabb@vmssoftware.com>
Signed-off-by: Matt McNabb <matthew.mcnabb@vmssoftware.com>
	This will be used by the date/time format length computation

Signed-off-by: Matt McNabb <matthew.mcnabb@vmssoftware.com>
Signed-off-by: Matt McNabb <matthew.mcnabb@vmssoftware.com>
Signed-off-by: Matt McNabb <matthew.mcnabb@vmssoftware.com>
@mmcnabb-vms mmcnabb-vms force-pushed the odbc_datefmt_attribute branch from fca5c88 to 8c0257b Compare April 7, 2026 02:35
@mmcnabb-vms

Copy link
Copy Markdown
Contributor Author

Repushed - Linux build has a script that checks connection string parameter names are in alphabetical order in include/freetds/odbc.h

Signed-off-by: Matt McNabb <matthew.mcnabb@vmssoftware.com>
@mmcnabb-vms

mmcnabb-vms commented Apr 7, 2026

Copy link
Copy Markdown
Contributor Author

In TDS 7.2 and older, date/time/datetime2 do not exist as wire formats (NVARCHAR is used instead), but datetime does exist. This formatting change only applies to conversion of datetime wire formats, as we have no way of knowing if an arriving NVARCHAR was originally a date or not.

Updated o_date test to check TDS version before testing those formats.

A problem is that in TDS 7.2 and older, SQL_DESC_LENGTH would be incorrect for datetime2, but correct for datetime, if the user specifies a datetime replacement format . I don't think we have any way to address this in FreeTDS since the source type is determined by the query string and/or database structure; the server converts the date to NVARCHAR before it gets to us. It's something user would have to be aware of if they are using TDS 7.2 or older.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ODBC date-to-char conversion

1 participant