Skip to content

Wrong type serialised when using TIME bind value with FOR XML #1616

Open
@lukaseder

Description

@lukaseder

Driver version

9.2.1.jre11

SQL Server version

Microsoft SQL Server 2019 (RTM-CU6) (KB4563110) - 15.0.4053.23 (X64) 
Jul 25 2020 11:26:55 
Copyright (C) 2019 Microsoft Corporation
Express Edition (64-bit) on Linux (Ubuntu 18.04.4 LTS) <X64>

Client Operating System

Microsoft Windows [Version 10.0.19042.1083]

JAVA/JVM version

openjdk version "17-ea" 2021-09-14
OpenJDK Runtime Environment (build 17-ea+29-2576)
OpenJDK 64-Bit Server VM (build 17-ea+29-2576, mixed mode, sharing)

Table schema

N/A

Problem description

  1. Expected behaviour: A TIME bind value produces the correct serialisation when used with FOR XML
  2. Actual behaviour: The bind value is converted to a TIMESTAMP instead

Reproduction code

try (PreparedStatement s = connection.prepareStatement(
    """
    select ? [a]
    for xml raw, elements
    """
)) {
    s.setTime(1, Time.valueOf("10:15:00"));

    try (ResultSet rs = s.executeQuery()) {
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }
}

This produces:

<row><a>1970-01-01T10:15:00</a></row>

When it should produce

<row><a>10:15:00</a></row>

The correct behaviour can be achieved by using an explicit CAST(? AS TIME):

try (PreparedStatement s = connection.prepareStatement(
    """
    select cast(? as time) [a]
    for xml raw, elements
    """
)) {
    s.setTime(1, Time.valueOf("10:15:00"));

    try (ResultSet rs = s.executeQuery()) {
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BacklogThe topic in question has been recognized and added to development backlog

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions