The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,152 other subscribers

Archive for the ‘SQL Server’ Category

SQL comma bullet point formatting: because AS is optional

Posted by jpluimers on 2022/11/03

Do you see the error below?

(note: OCR via [Wayback/Archive.is] Best Free OCR API, Online OCR, Searchable PDF – Fresh 2021 OCR Software)

SELECT
  license date
  expiration_date,
  renewal_due_date
FROM license
WHERE expiration_date IS NULL
AND processing != 'Automatic'
AND edition != 'Other'
expiration_date renewal_ due date
1 2016-04-08 09:50:00 [NULL]
2 2013-11-14 11:15:00 [NULL]
3 2014-11-20 14:51:00 [NULL]
4 2017-07-21 16:00:00 [NULL]
5 2018-12-17 14:37:46 2020-12-17 14:37:46

All of the expiration_date columns have values, which is contrary to the WHERE clause. This is because the table itself contains an expiration_date column, and the SELECT part aliases license_date into expiration_date.

The result is that you see rows that have expiration_date being NULL, but license_date having a value.

So I totally agree with [Archive.is] Mathias Magnusson on Twitter: “That is one reason I’m a form believer in comma bullet point in my SQL. Problems like that has bit me far too often due to som issue with commas.… “

This is how the SQL should have looked:

SELECT
  license date
, expiration_date
, renewal_due_date
FROM license
WHERE expiration_date IS NULL
AND processing != 'Automatic'
AND edition != 'Other'

Yes indeed: an alias of a column without the AS keyword is allowed in quite a few SQL dialects (they differ even more widely in SQL extensions like SQL/PSM, T-SQL, PL/SQL, SQL_PL, or ABAB).

Aliases are for output, cannot be used in WHERE (but can in ORDER BY).

You can see what happens (and how hard this can become on one line) with these two dbfiddle queries running on Microsoft SQL Server 2019 dialect (though it works similar in other dialects):

  1. select a b, c from (values (9, 1, 2), (8, 3, 4), (7, 5, 6)) t(a, b, c) order by b
    I saved it as [Wayback/Archive.is] SQL Server 2019 | dbfiddle: select a b, c from (values (9, 1, 2), (8, 3, 4), (7, 5, 6)) t(a, b, c) order by b
    Resulting in

    b c
    7 6
    8 4
    9 2

    It shows only the column names a and b, but note the table itself is aliased to t above as well.

  2. select a, b, c from (values (9, 1, 2), (8, 3, 4), (7, 5, 6)) t(a, b, c) order by b
    I saved it as [Wayback/Archive.is] SQL Server 2019 | dbfiddle: select a, b, c from (values (9, 1, 2), (8, 3, 4), (7, 5, 6)) t(a, b, c) order by b
    Resulting in

    a b c
    9 1 2
    8 3 4
    7 5 6

    It shows only the column names a, b and c, but note the table itself is aliased to t above as well.

I really wish various SQL dialects would force the SQL syntax to be (together with a hint that the alias would overwrite an existing field):

SELECT
  license date AS expiration_date
, renewal_due_date
FROM license
WHERE expiration_date IS NULL
AND processing != 'Automatic'
AND edition != 'Other'

That is not going to happen, so the second best is to wish for tooling to hint/warn about it, and provide better syntax highlighting for it. That seems work in progress by now:

Read the rest of this entry »

Posted in Conventions, Database Development, Development, OracleDB, PL/SQL, SQL, SQL Server, T-SQL | Leave a Comment »

Database fiddle sites

Posted by jpluimers on 2022/10/27

I knew there was JSFiddle for live playing around with JavaScript and more in your browser, so I wondered if there was a similar site for databases and SQL queries.

There are, so here are a few database fiddle sites: SQL playgrounds where you can live play with SQL queries (sometimes even without an underlying example database).

All via [Wayback/Archive.is] database fiddle – Google Search:

Read the rest of this entry »

Posted in Conference Topics, Conferences, Database Development, DB2, Development, Event, Firebird, JavaScript/ECMAScript, JSFiddle, MariaDB, MySQL, OracleDB, PL/SQL, PostgreSQL, Scripting, Software Development, SQL, SQL Server, SQLite, T-SQL | Leave a Comment »

Case sensitivity for SQL identifiers · ontop/ontop Wiki · GitHub

Posted by jpluimers on 2021/07/08

For my link archive: [WayBack] Case sensitivity for SQL identifiers · ontop/ontop Wiki · GitHub:

  • Oracle and H2 changes unquoted identifiers to uppercase.
    Although technically possible, Oracle explicitly recommends to not use lowercase identifers. We do not support H2 with the setting DATABASE_TO_UPPER=FALSE, if this setting is enabled all queries with names and tables in lowercase must be quoted.
  • DB2 Names are not case sensitive.
    For example, the table names CUSTOMER and Customer are the same, but object names are converted to uppercase when they are entered. If a name is enclosed in quotation marks, the name becomes case sensitive. The schema name is case-sensitive, and must be specified in uppercase characters.
  • Postgres changes unquoted identifiers (both columns and alias names) to lowercase.
  • Mysql does not change the case of unquoted tables and schemas.
    It changes in lowercase the unquoted columns. Mysql tables are stored as files in the operating system the server runs on. This means that database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix or Linux. The backtick ` is used for enclosing identifiers such as table and column names.
  • Mssqlserver All connection string property names are case-insensitive.
    For example, Password is the same as password. Identifiers of objects in a database, such as tables, views, and column names, are assigned the default collation of the database. For example, two tables with names that differ only in case can be created in a database that has case-sensitive collation, but cannot be created in a database that has case-insensitive collation. Default SQL Server is not case sensitive. SELECT * FROM SomeTable is the same as SeLeCT * frOM soMetaBLe. Delimited identifiers are enclosed in double quotation marks (“) or brackets ([]). Identifiers that comply with the rules for the format of identifiers may or may not be delimited.

–jeroen

Posted in Database Development, DB2, Development, MySQL, OracleDB, PostgreSQL, SQL Server | Leave a Comment »

SQL Server, [] brackets, keywords and special characters

Posted by jpluimers on 2021/07/06

A few links for my archive:

  • [WayBack] sql server – What is the use of the square brackets [] in sql statements? – Stack Overflow answer by Michael Haren:

    The brackets are required if you use keywords or special chars in the column names or identifiers. You could name a column [First Name] (with a space)–but then you’d need to use brackets every time you referred to that column.

    The newer tools add them everywhere just in case or for consistency.

     

  • [WayBack] tsql – What characters are valid in an SQL Server database name? – Stack Overflow answer by Scott Munro:

    Delimited names – surrounded by square brackets or double quotes (if QUOTED_IDENTIFIER is set to ON) – can contain basically anything other than the delimiters themselves. It is even possible to use the delimiters within the name with some escape logic. Note though that it is only the closing escape character that must be escaped. In the first example below, the single instance of the opening escape character in the name does not need to be escaped whereas the closing escape character does have to be escaped (by replacing the single instance with two). I guess the logic here is that whatever code that is parsing these statements is looking for a closing escape character and has is not interested in nested opening escape characters.

    • [Test[Test] -> Test[Test
    • [Test]]Test] -> Test]Test

    The following is a description of the rules surrounding non delimited (nonquoted) identifier names in SQL Server 2012. It is an extract from the document Guide to Migrating from MySQL to SQL Server 2012.

    Schema Object Names

    In SQL Server 2012, an object name can be up to 128 characters long.

    Nonquoted identifier names must follow these rules:

    • The first character must be alphanumeric, an underscore (_), an at sign (@), or a number sign (#).
    • Subsequent characters can include alphanumeric characters, an underscore, an at (@) sign, a number sign, or a dollar sign.
    • The identifier must not be a Transact-SQL reserved word. Guide to Migrating from MySQL to SQL Server 2012 8
    • Embedded spaces or special characters are not allowed.

    Identifiers that start with @ or a number sign have special meanings. Identifiers starting with @ are local variable names. Those that start with a number sign are temporary table names.

    To quote an identifier name in Transact-SQL, you must use square brackets ([]).

  • [WayBack] Database Identifiers – SQL Server | Microsoft Docs:
    1. Classes of Identifiers
    2. Rules for Regular Identifiers
    3. See Also

–jeroen

 

 

Posted in Database Development, Development, SQL, SQL Server | Leave a Comment »

In case you cannot do BIGINT yet: A How-to Guide for Resolving Database Record Limitations | Distillery

Posted by jpluimers on 2021/06/30

Sometimes your development stack is not fully ready to handle signed 64-bit BIGINT values across all layers, which is when [WayBack] A How-to Guide for Resolving Database Record Limitations | Distillery comes in handy.

For data type ranges: [WayBack] int, bigint, smallint, and tinyint (Transact-SQL) – SQL Server | Microsoft Docs

–jeroen

Posted in Database Development, Development, Software Development, SQL Server | Leave a Comment »

 
%d bloggers like this: