|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-05-22 02:33 UTC] zippy1981 at gmail dot com
Description:
------------
Lets say I have the following temp table defined:
DROP TABLE IF EXISTS #dateTable;
CREATE TABLE #dateTable (
Id INT NOT NULL PRIMARY KEY CLUSTERED IDENTITY (1,1),
[timestamp] DATETIMEOFFSET NOT NULL,
message NVARCHAR(255) NOT NULL
);
And I have an odbc pdo connection $this->cn.
I can insert a row with
$sql = <<< EOSQL
INSERT INTO #dateTable (message, [timestamp]) VALUES (
'Solid string insert.',
'%s'
);
EOSQL;
$this->cn->exec(sprintf($sql);
However, if I paramaterize it like so:
$sql = <<< EOSQL
INSERT INTO #dateTable (message, [timestamp]) VALUES (
'Solid string insert.',
:timestamp
);
EOSQL;
$stmt = $this->cn->prepare($sql);
None of the following options work:
1.
$stmt->execute([date(DATE_ATOM)]);
2.
$timestamp = date(DATE_ATOM);
$stmt->bindParam(':timestamp', $timestamp);
$result = $stmt->execute();
3.
$stmt->bindValue(':timestamp', date(DATE_ATOM));
$result = $stmt->execute();
However, if I do PDO with the SqlSvr driver it works just fine.
Test script:
---------------
https://github.com/zippy1981/PhpSqlServerDateTime
Actual result:
--------------
PDOException: SQLSTATE[22018]: Invalid character value for cast specification: 206 [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Operand type clash: text is incompatible with datetimeoffset (SQLExecute[206] at ext\pdo_odbc\odbc_stmt.c:260)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 21:00:01 2025 UTC |
Indeed, at least the ODBC Driver for SQL Server and the SQL Server Native Client do not accept the 'T' in the date/time value[1]. date("Y-m-d H:i:sP") should work fine. I don't think we should work around that for PDO_ODBC; if it works with pdo_sqlsrv, fine – that extension appears to be better for interfacing with SQL Server anyway. [1] <https://docs.microsoft.com/en-us/sql/relational-databases/native-client-odbc-date-time/data-type-support-for-odbc-date-and-time-improvements#data-formats-strings-and-literals>