Home > Software design >  SELECT column name starting with numeral
SELECT column name starting with numeral

Time:11-15

This one is out of morbid curiosity. I have a very badly named table here:

CREATE TABLE badtable (
    id INT PRIMARY KEY,
    "customer name" VARCHAR(63),
    "order" VARCHAR(12),
    "1st" date,
    "last-date" date
);

I am trying to show when you might desperately need delimited column names. However, the following is not an error:

SELECT
    "customer name",
    "order",
    1st,    --  no delimiter
    "last-date"
FROM badtable;

Instead it happily gives me a column called st.

This works on both PostgreSQL and Microsoft SQL Server, so it’s not limited to a quirk of one of them.

How is the 1st column name being interpreted?

CodePudding user response:

In some situations whitespace is not required as long as the DBMS is able to read the expression unambiguously.

select 1st

selects a 1. What follows is the alias name. Hence the same as

select 1 st

or

select 1 as st
  • Related