Skip to main content
added 266 characters in body
Source Link
Tom V
  • 15.7k
  • 7
  • 64
  • 87

That is just documented behavior. I don't think anyone messed with the settings.

See data type precedence on MSDN.

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.

As noted in the comments the empty string gets converted to 0 in any numeric type and to 1900-01-01 00:00:00.000 when converted to a date.

EDIT: I think your real problem is that your design is so that you have to join on fields of a different data type. The only way to get around this is to have a convert on your join clause which will hurt query performance. The main problem is probably with the schema design

EDIT: There was a lot of discussion in the comments that have been moved to chat. However illogical it may seem, converting an empty string to other data types produces arbitrary values.

This code:

SELECT CONVERT(int, '')
SELECT CONVERT(float, '')
SELECT CONVERT(date, '')
SELECT CONVERT(datetime, '')

Produces this output:

0
0
1900-01-01
1900-01-01 00:00:00.000

You could expect then that this behavior is consistent between other preceding datatypes and expect that converting 0 to a date would produce the same arbitrary value but it doesn't.

SELECT CONVERT(date, 0)

Produces

Explicit conversion from data type int to date is not allowed.

Because it's not a supported conversion

while

SELECT CONVERT(datetime, 0)

Returns

January, 01 1900 00:00:00

So yes, it's weird and arbitrary, but actually documented and explainable.

That is just documented behavior. I don't think anyone messed with the settings.

See data type precedence on MSDN.

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.

As noted in the comments the empty string gets converted to 0 in any numeric type and to 1900-01-01 00:00:00.000 when converted to a date.

EDIT: I think your real problem is that your design is so that you have to join on fields of a different data type. The only way to get around this is to have a convert on your join clause which will hurt query performance. The main problem is probably with the schema design

EDIT: There was a lot of discussion in the comments that have been moved to chat. However illogical it may seem, converting an empty string to other data types produces arbitrary values.

This code:

SELECT CONVERT(int, '')
SELECT CONVERT(float, '')
SELECT CONVERT(date, '')
SELECT CONVERT(datetime, '')

Produces this output:

0
0
1900-01-01
1900-01-01 00:00:00.000

You could expect then that this behavior is consistent between other preceding datatypes and expect that converting 0 to a date would produce the same arbitrary value but it doesn't.

SELECT CONVERT(date, 0)

Produces

Explicit conversion from data type int to date is not allowed.

That is just documented behavior. I don't think anyone messed with the settings.

See data type precedence on MSDN.

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.

As noted in the comments the empty string gets converted to 0 in any numeric type and to 1900-01-01 00:00:00.000 when converted to a date.

EDIT: I think your real problem is that your design is so that you have to join on fields of a different data type. The only way to get around this is to have a convert on your join clause which will hurt query performance. The main problem is probably with the schema design

EDIT: There was a lot of discussion in the comments that have been moved to chat. However illogical it may seem, converting an empty string to other data types produces arbitrary values.

This code:

SELECT CONVERT(int, '')
SELECT CONVERT(float, '')
SELECT CONVERT(date, '')
SELECT CONVERT(datetime, '')

Produces this output:

0
0
1900-01-01
1900-01-01 00:00:00.000

You could expect then that this behavior is consistent between other preceding datatypes and expect that converting 0 to a date would produce the same arbitrary value but it doesn't.

SELECT CONVERT(date, 0)

Produces

Explicit conversion from data type int to date is not allowed.

Because it's not a supported conversion

while

SELECT CONVERT(datetime, 0)

Returns

January, 01 1900 00:00:00

So yes, it's weird and arbitrary, but actually documented and explainable.

added 714 characters in body
Source Link
Tom V
  • 15.7k
  • 7
  • 64
  • 87

That is just documented behavior. I don't think anyone messed with the settings.

See data type precedence on MSDN.

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.

As noted in the comments the empty string gets converted to 0 in any numeric type and to 1900-01-01 00:00:00.000 when converted to a date.

EDIT: I think your real problem is that your design is so that you have to join on fields of a different data type. The only way to get around this is to have a convert on your join clause which will hurt query performance. The main problem is probably with the schema design

EDIT: There was a lot of discussion in the comments that have been moved to chat. However illogical it may seem, converting an empty string to other data types produces arbitrary values.

This code:

SELECT CONVERT(int, '')
SELECT CONVERT(float, '')
SELECT CONVERT(date, '')
SELECT CONVERT(datetime, '')

Produces this output:

0
0
1900-01-01
1900-01-01 00:00:00.000

You could expect then that this behavior is consistent between other preceding datatypes and expect that converting 0 to a date would produce the same arbitrary value but it doesn't.

SELECT CONVERT(date, 0)

Produces

Explicit conversion from data type int to date is not allowed.

That is just documented behavior. I don't think anyone messed with the settings.

See data type precedence on MSDN.

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.

As noted in the comments the empty string gets converted to 0 in any numeric type and to 1900-01-01 00:00:00.000 when converted to a date.

EDIT: I think your real problem is that your design is so that you have to join on fields of a different data type. The only way to get around this is to have a convert on your join clause which will hurt query performance. The main problem is probably with the schema design

That is just documented behavior. I don't think anyone messed with the settings.

See data type precedence on MSDN.

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.

As noted in the comments the empty string gets converted to 0 in any numeric type and to 1900-01-01 00:00:00.000 when converted to a date.

EDIT: I think your real problem is that your design is so that you have to join on fields of a different data type. The only way to get around this is to have a convert on your join clause which will hurt query performance. The main problem is probably with the schema design

EDIT: There was a lot of discussion in the comments that have been moved to chat. However illogical it may seem, converting an empty string to other data types produces arbitrary values.

This code:

SELECT CONVERT(int, '')
SELECT CONVERT(float, '')
SELECT CONVERT(date, '')
SELECT CONVERT(datetime, '')

Produces this output:

0
0
1900-01-01
1900-01-01 00:00:00.000

You could expect then that this behavior is consistent between other preceding datatypes and expect that converting 0 to a date would produce the same arbitrary value but it doesn't.

SELECT CONVERT(date, 0)

Produces

Explicit conversion from data type int to date is not allowed.

included blurb from article to make answer more complete
Source Link
Tom V
  • 15.7k
  • 7
  • 64
  • 87

That is just documented behavior. I don't think anyone messed with the settings.

See data type precedence on MSDN.

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.

As noted in the comments the empty string gets converted to 0 in any numeric type and to 1900-01-01 00:00:00.000 when converted to a date.

EDIT: I think your real problem is that your design is so that you have to join on fields of a different data type. The only way to get around this is to have a convert on your join clause which will hurt query performance. The main problem is probably with the schema design

That is just documented behavior. I don't think anyone messed with the settings.

See data type precedence on MSDN.

As noted in the comments the empty string gets converted to 0 in any numeric type and to 1900-01-01 00:00:00.000 when converted to a date.

EDIT: I think your real problem is that your design is so that you have to join on fields of a different data type. The only way to get around this is to have a convert on your join clause which will hurt query performance. The main problem is probably with the schema design

That is just documented behavior. I don't think anyone messed with the settings.

See data type precedence on MSDN.

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.

As noted in the comments the empty string gets converted to 0 in any numeric type and to 1900-01-01 00:00:00.000 when converted to a date.

EDIT: I think your real problem is that your design is so that you have to join on fields of a different data type. The only way to get around this is to have a convert on your join clause which will hurt query performance. The main problem is probably with the schema design

Mod Moved Comments To Chat
in response to ypercube's comment who has a point
Source Link
Tom V
  • 15.7k
  • 7
  • 64
  • 87
Loading
added 279 characters in body
Source Link
Tom V
  • 15.7k
  • 7
  • 64
  • 87
Loading
Source Link
Tom V
  • 15.7k
  • 7
  • 64
  • 87
Loading