Parsing SQL Server number literal with underscore

Viewed 1075

I wonder why it works and why it does not return an error:

SELECT 2015_11

Result:

╔══════╗
║ _11  ║
╠══════╣
║ 2015 ║
╚══════╝

Second case:

SELECT 2.1_a

╔═════╗
║ _a  ║
╠═════╣
║ 2.1 ║
╚═════╝

Checking metadata:

SELECT  name, system_type_name
FROM sys.dm_exec_describe_first_result_set(
N'SELECT 2015_11', NULL, 0) 
UNION ALL
SELECT  name, system_type_name
FROM sys.dm_exec_describe_first_result_set(
N'SELECT 3.2_a', NULL, 0) 

╔══════╦══════════════════╗
║ name ║ system_type_name ║
╠══════╬══════════════════╣
║ _11  ║ int              ║
║ _a   ║ numeric(2,1)     ║
╚══════╩══════════════════╝

While identifier that starts with letter behaves as I think it should:

SELECT a_11
-- Invalid column name 'a_11'.

LiveDemo

2 Answers
Related