Can someone help point out to me what is wrong with this no brainer WHERE clause? - sql

This is very simple but somehow I'm doing something wrong with this query on my database.
I have this query below:
SELECT login FROM accounts WHERE login = "loginname";
When I execute this query the result I get is this:
column "loginname" does not exist
This is a no brainer, why is this query not working properly? I have a login column and I know that this user exists because I've found this person with the rails console. Why is the login criteria referring to itself as a column?

Try with single quotes '' if you are trying to match a string
SELECT login FROM accounts WHERE login = 'loginname';
Check the documentation
There is a second kind of identifier: the delimited identifier or
quoted identifier. It is formed by enclosing an arbitrary sequence of
characters in double-quotes ("). A delimited identifier is always an
identifier, never a key word. So "select" could be used to refer to a
column or table named "select", whereas an unquoted select would be
taken as a key word and would therefore provoke a parse error when
used where a table or column name is expected.

Double quotes (") are used to refer to object names, in a case sensitive way. In this case, "loginname" is interpreted as a column name, and the query fails, since there is no such column. In order to refer to a string literal, you should use single quotes ('):
SELECT login FROM accounts WHERE login = 'loginname';
-- Here ---------------------------------^---------^

It seems that the " " are the problem if you believe the documentation. Single quotes are required for string values.

From the PostgreSQL Documentation:
There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named "select", whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected. The example can be written with quoted identifiers like this:
UPDATE "my_table" SET "a" = 5;
Quoted identifiers can contain any character, except the character with code zero. (To include a double quote, write two double quotes.) This allows constructing table or column names that would otherwise not be possible, such as ones containing spaces or ampersands. The length limitation still applies.
So in your query "loginname" is the same as having loginname without quotes - it's attempting to refer to a column with that name. To make it a literal string, use single-quotes instead.

Related

What does a reserved keyword mean in SQL when it's not applicable to the current statement?

What exactly does it mean when a keyword is reserved in SQL? For example, if we use the word GRANT which is not applicable outside of admin statements. What exactly does it mean then that the keyword is reserved, for example can I do any of the following:
Use the word as a database, schema, table, or column name?
Use the word as a function, procedure, trigger, or index name?
Use the word as an alias, for example within a SELECT statement?
Use the word as a key in a JSON (or STRUCT/RECORD) key?
Reserved word have to be escaped if really want to use them outside their scope.
It is usually dioscouraged as you need to add all the time the escape characters.
Single quotes don't escape the reserved the but make them to strings, so the databse will not produce an error
double quotes can be escape characters like in postgres or strings like in MySQL. in both cases you wouldn't get an error message
You get an error message because the parser checks for keywords like the re4served words and chekc if the syntax is correct, which is never the case when you use reserved words.

Select text column in Postgres SQL [duplicate]

I am new to PostgresSQL.I tried
select * from employee where employee_name="elina";
But that results error as follows:
ERROR: column "elina" does not exist.
Then I tried by replacing double quotes with single quotes as follows:
select * from employee where employee_name='elina';
It result fine..So what is the difference between single quotes and double quotes in postgresql.If we can't use double quotes in postgres query,then if any other use for this double quotes in postgreSQL?
Double quotes are for names of tables or fields. Sometimes You can omit them. The single quotes are for string constants. This is the SQL standard. In the verbose form, your query looks like this:
select * from "employee" where "employee_name"='elina';
As explained in the PostgreSQL manual:
A string constant in SQL is an arbitrary sequence of characters bounded by single quotes ('), for example 'This is a string'. To include a single-quote character within a string constant, write two adjacent single quotes, e.g., 'Dianne''s horse'. Note that this is not the same as a double-quote character (").
Elsewhere on the same page:
There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named "select", whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected.
TL;DR: Single quotes for string constants, double quotes for table/column names.
Well single quotes are used for string literals and double quotes are used for escaping DB objects like table name / column name etc.
Specifically, double quotes are used for escaping a column/table name if it's resemble to any reserve/key word. Though every RDBMS have their own way of escaping the same (like backtique in MySQL or square bracket in SQL Server) but using double quotes is ANSI standard.

Snowflake: SELECT "COLUMN" with double quotes

I have a customer that their Snowflake instance require the columns name to have double quotes. Example: SELECT "CategoryName" from "lk_category" ;
Does anyone know how to turn this feature off?
The Snowflake documentation does explain a bit about identifiers.
The key point is that quoted identifiers must be referred to exactly as they are defined. So, the following are different:
"ABC"
"abc"
"Abc"
Unquoted identifiers are case-insensitive, so the following are the same:
ABC
abc
Abc
As a bonus, these are also equivalent to:
"ABC"
because Snowflake resolves unquoted identifiers using the upper case.
If the database has already been created with quoted identifiers . . . well, you can go about changing it.
Snowflake has identified this as a problem. You can turn off the quoted behavior by setting the QUOTED_IDENTIFIERS_IGNORE_CASE parameter.
For your question:
SELECT """CategoryName""" FROM lk_category
…is the answer. I had a similar issue with double pivot tables.
Please check it using:
Describe View/Table Table/View_Name
We will get the list of Column Names, If a field name with double quote Ex:"Name" is present.
Then to select it we should use extra quotes as escape characters.

Table or column name cannot start with numeric?

I tried to create table named 15909434_user with syntax like below:
CREATE TABLE 15909434_user ( ... )
It would produced error of course. Then, after I tried to have a bit research with google, I found a good article here that describe:
When you create an object in PostgreSQL, you give that object a name. Every table has a name, every column has a name, and so on. PostgreSQL uses a single data type to define all object names: the name type.
A value of type name is a string of 63 or fewer characters. A name must start with a letter or an underscore; the rest of the string can contain letters, digits, and underscores.
...
If you find that you need to create an object that does not meet these rules, you can enclose the name in double quotes. Wrapping a name in quotes creates a quoted identifier. For example, you could create a table whose name is "3.14159"—the double quotes are required, but are not actually a part of the name (that is, they are not stored and do not count against the 63-character limit). ...
Okay, now I know how to solve this by use this syntax (putting double quote on table name):
CREATE TABLE "15909434_user" ( ... )
You can create table or column name such as "15909434_user" and also user_15909434, but cannot create table or column name begin with numeric without use of double quotes.
So then, I am curious about the reason behind that (except it is a convention). Why this convention applied? Is it to avoid something like syntax limitation or other reason?
Thanks in advance for your attention!
It comes from the original sql standards, which through several layers of indirection eventually get to an identifier start block, which is one of several things, but primarily it is "a simple latin letter". There are other things too that can be used, but if you want to see all the details, go to http://en.wikipedia.org/wiki/SQL-92 and follow the links to the actual standard ( page 85 )
Having non numeric identifier introducers makes writing a parser to decode sql for execution easier and quicker, but a quoted form is fine too.
Edit: Why is it easier for the parser?
The problem for a parser is more in the SELECT-list clause than the FROM clause. The select-list is the list of expressions that are selected from the tables, and this is very flexible, allowing simple column names and numeric expressions. Consider the following:
SELECT 2e2 + 3.4 FROM ...
If table names, and column names could start with numerics, is 2e2 a column name or a valid number (e format is typically permitted in numeric literals) and is 3.4 the table "3" and column "4" or is it the numeric value 3.4 ?
Having the rule that identifiers start with simple latin letters (and some other specific things) means that a parser that sees 2e2 can quickly discern this will be a numeric expression, same deal with 3.4
While it would be possible to devise a scheme to allow numeric leading characters, this might lead to even more obscure rules (opinion), so this rule is a nice solution. If you allowed digits first, then it would always need quoting, which is arguably not as 'clean'.
Disclaimer, I've simplified the above slightly, ignoring corelation names to keep it short. I'm not totally familiar with postgres, but have double checked the above answer against Oracle RDB documentation and sql spec
I'd imagine it's to do with the grammar.
SELECT 24*DAY_NUMBER as X from MY_TABLE
is fine, but ambiguous if 24 was allowed as a column name.
Adding quotes means you're explicitly referring to an identifier not a constant. So in order to use it, you'd always have to escape it anyway.

SQL Server query to create database is giving me an error when I include numeric characters in the database name

I am having trouble running a simple sql query in Microsoft SQL Server 2005 to create a database and im not sure why.
When I run this query
CREATE DATABASE 4444-virtual2
I receive this error
Incorrect syntax near '4444'.
Is there anything in particular that I must specify if I am creating a database table with numeric values in the name? Or am I forgetting something?
Database names need to start with a letter, underscore, at sign or number sign:
The first character must be one of the
following:
A letter as defined by the Unicode
Standard 3.2. The Unicode definition
of letters includes Latin characters
from a through z, from A through Z,
and also letter characters from other
languages.
The underscore (_), at sign (#), or
number sign (#).
Certain symbols at the beginning of an
identifier have special meaning in SQL
Server. A regular identifier that
starts with the at sign always denotes
a local variable or parameter and
cannot be used as the name of any
other type of object. An identifier
that starts with a number sign denotes
a temporary table or procedure. An
identifier that starts with double
number signs (##) denotes a global
temporary object. Although the number
sign or double number sign characters
can be used to begin the names of
other types of objects, we do not
recommend this practice.
Some Transact-SQL functions have names
that start with double at signs (##).
To avoid confusion with these
functions, you should not use names
that start with ##.
Unless you want to delimit every use of the name with double quotes "4444-virtual2" or brackets [4444-virtual2].
You can still create a database with that name, but you need to put it in quotes or brackets. e.g. this works:
CREATE DATABASE [4444-virtual2]
or this:
CREATE DATABASE "4444-virtual2"