Afternoon all, unsure if this exactly the right spot for this but here goes anyway:
I noticed an issue where my smalldatetime fields were being rounded down instead of up when converting from datetime.
The circumstance which this happens under is when running an update across a linked server while updating other columns that require some form of conversion or contain a variable. Straight away I will say this is in SQL 2005 and I don't know if this bug persists today, so what I am really asking is, does this still happen in later versions?
Table Definition:
CREATE TABLE [aDatabase].[dbo].aTempTable(
[record_id] INT NOT NULL,
[record_datetime] SMALLDATETIME NULL,
[record_text] VARCHAR(200) NULL
)
INSERT INTO aTempTable ([record_id],[record_datetime]) VALUES (1, getdate())
The following sql results in the correct smalldatetime value appearing in the table:
UPDATE [LINKED_SERVER].[aDatabase].[dbo].aTempTable
SET [record_text] = 'Literal Text'
, [record_datetime] = '2014-07-24 17:15:38.330'
WHERE [record_id] = 1
The correct value is '2014-07-25 17:16:00' as 38 seconds should round up to the next minute.
When running the following sql, the incorrectly rounded down version of the date is stored:
DECLARE @SOMETEXT VARCHAR(20)
SET @SOMETEXT = 'Dynamic Text'
UPDATE [LINKED_SERVER].[aDatabase].[dbo].aTempTable
SET [record_text] = @SOMETEXT
, [record_datetime] = '2014-07-24 17:15:38.330'
WHERE [record_id] = 1
This query results in '2014-07-25 17:15:00' being stored, incorrectly.
The difference I see between the two queries is that the first causes a Remote Query to be executed by SQL and the second a Remote Update
Query 1:
|--Remote Query(SOURCE:(LINKED_SERVER), QUERY:(UPDATE "aDatabase"."dbo"."aTempTable" set "record_text" = 'Literal Text',"record_datetime" = convert(datetime, '2014-07-24T17:15:38.330') WHERE "record_id"=(1)))
Query 2:
|--Remote Update(SOURCE:(####), OBJECT:("aDatabase"."dbo"."aTempTable"), SET:([LINKED_SERVER].[aDatabase].[dbo].[aTempTable].[record_text] = [Expr1003],[LINKED_SERVER].[aDatabase].[dbo].[aTempTable].[record_datetime] = [Expr1004]))
|--Compute Scalar(DEFINE:([Expr1003]=CONVERT_IMPLICIT(varchar(200),[@SOMETEXT],0), [Expr1004]='2014-07-24 17:15:38.330'))
|--Table Spool
|--Filter(WHERE:([LINKED_SERVER].[aDatabase].[dbo].[aTempTable].[record_id]=(1)))
|--Remote Scan(SOURCE:(####), OBJECT:("aDatabase"."dbo"."aTempTable"))
Anyone care to weigh in on this? As it is SQL 2005 I am just going to perform a workaround in my query that will drop the seconds before saving the field (as in this instance I actually wanted to round down - I just didn't expect it to! And don't wish
to rely on a bug/undocumented behaviour)
A big if, but IF I get the chance I will check this on a more recent version of SQL server.
Regards, Dave