Variable names in SQL Server - sql

Can I declare name of SQL Server variable in a table with spaces?
create table test(
record name, float, not null
.....
The above query when executed gives me an error. Is there any way to declare the variable as
variable name with a space..??

Yes, escape these names using []:
[record name] ....
These names are called Delimited identifiers:
Are enclosed in double quotation marks (") or brackets ([ ]).
But it is not recommended, use legal names instead or regular identifiers.

Try using square brackets:
create table test(
[record name] float not null)

use [square brackets] around your column names with spaces and you should be fine.
It would be advisable in the long term to avoid spaces all together if you can, it will save you hours of stress in the future.

Related

code convert unique chars to '?' in postgreSQL queries

I have an automation code that runs a bunch of queries into Postgresql DB.
one of my queries is :
CREATE TABLE 行 (CustomerName int, City varchar(255),Country varchar(255))
when running it into the DB, I got this response:
Query response from db:
CREATE TABLE ? (CustomerName int, City varchar(255),Country varchar(255));
ERROR: syntax error at or near "?"
LINE 1: CREATE TABLE ? (CustomerName int, City varchar(255),Country ...
^
postgres=#
it seems that it converts the unique char to '?'.
any suggestion why this could happen?
I'm sure that before the query is executed the letters are encoded correctly.
(when running this query manually everything goes well)
I would avoid using keyword characters, or international characters.
If you really need to do that you can try to use " double quote
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.
SQL-SYNTAX-IDENTIFIERS
CREATE TABLE "行" (CustomerName int, City varchar(255),Country varchar(255))

How to SELECT COL_"NAME" in Snowflake ? (COLUMN name containing double quote)

I know this is very bad naming practice, but I am not the owner of the table...
I need to run :
SELECT COL_"NAME"
FROM TABLE
where COL_"NAME" is the name of the column, containing double quotes.
I tried :
SELECT COL_""NAME""
FROM TABLE
SELECT COL_\"NAME\"
FROM TABLE
But nothing works
Identifier Requirements:
To use the double quote character inside a quoted identifier, use two quotes.
To access column: COL_"NAME"
SELECT "COL_""NAME"""
FROM TABLE;

Hive- issue with Create Table with column have space

Will need some advice. In HIVE DB is it possible to create table with column have space as below
CREATE TABLE TEST2("Kod ASS" String)
get an error as below
Error: Error while compiling statement: FAILED: ParseException line 1:19 cannot recognize input near '"Kod ASS"' 'String' ')' in column specification
SQLState: 42000
ErrorCode: 40000
show manual about column names:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL
In Hive 0.12 and earlier, only alphanumeric and underscore characters are allowed in table and column names.
In Hive 0.13 and later, column names can contain any Unicode character (see HIVE-6013). Any column name that is specified within
backticks (`) is treated literally. Within a backtick string, use
double backticks (``) to represent a backtick character. Backtick
quotation also enables the use of reserved keywords for table and
column identifiers.
To revert to pre-0.13.0 behavior and restrict column names to alphanumeric and underscore characters, set the configuration property
hive.support.quoted.identifiers to none. In this configuration,
backticked names are interpreted as regular expressions. For details,
see Supporting Quoted Identifiers in Column Names.
CREATE TABLE DB_Name.Table_name (
First name VARCHAR(64), Last name VARCHAR(64), Location id VARCHAR(64) , age INT, gpa DECIMAL(3,2)) CLUSTERED BY (age) INTO 2 BUCKETS STORED AS ORC;
OR
CREATE TABLE TEST2(Kod ASS String) STORED AS TEXTFILE;
You can use and put column name inside.
I hope both worked for you.

Square bracket in table/column name is not supported?

Is square bracket in table name, column name or datatype is not supported in postgresql ?
I am getting below error while running the query in pgadmin:
CREATE TABLE [Test];
ERROR: syntax error at or near "["
SQL state: 42601
In PostgreSQL you can use double quotes :
CREATE TABLE "Test";
Same for columns, square brackets are used in SQL-Server.
If you mean the table name would be [Test] with brackets included then you would use "[Test]".
Create table "[Test]" ...;
If you meant it as an identifier, you could simply use without brackets or double quotes as Test.
Create table Test ...;
This way, you could refer to it as Test or test or tESt without double quotes in subsequent queries, ie:
select * from test;
If you use "Test" then postgreSQL would treat it as case sensitive and you would always use "Test".
Create table "Test" ...;
If you were trying to emphasize the identifier name by using square brackets, then sagi's answer is correct. On the other hand, if you really want to use square brackets in your table name, postgresql supports this as "[Test]". In this case your table name will include square brackets. You can get additional info from postgresql documentation

[] brackets in sql statements

What do the brackets do in a sql statement?
For example, in the statement:
insert into table1 ([columnname1], columnname2) values (val1, val2)
Also, what does it do if the table name is in brackets?
The [] marks the delimitation of a identifier, so if you have a column whose name contains spaces like Order Qty you need to enclose it with [] like:
select [Order qty] from [Client sales]
They are also to escape reserved keywords used as identifiers
This is Microsoft SQL Server nonstandard syntax for "delimited identifiers." SQL supports delimiters for identifiers to allow table names, column names, or other metadata objects to contain the following:
SQL reserved words: "Order"
Words containing spaces: "Order qty"
Words containing punctuation:
"Order-qty"
Words containing international
characters
Column names that are
case-sensitive: "Order" vs. "order"
Microsoft SQL Server uses the square brackets, but this is not the syntax standard SQL uses for delimited identifiers. Standardly, double-quotes should be used for delimiters.
In Microsoft SQL Server, you can enable a mode to use standard double-quotes for delimiters as follows:
SET QUOTED_IDENTIFIER ON;
They are meant to escape reserved keywords or invalid column identifiers.
CREATE TABLE test
(
[select] varchar(15)
)
INSERT INTO test VALUES('abc')
SELECT [select] FROM test
They allow you to use keywords (such as date) in the name of the column, table, etc...
Since this is a bad practice to begin with, they are generally not included. The only place you should see them being used is by people starting out with sql queries that don't know any better. Other than that they just clutter up your query.
Anything inside the brackets is considered a single identifier (e.g. [test machine]. This can be used to enclose names with spaces or to escape reserve words (e.g. [order], [select], [group]).
if you use any column name which is same as any reserved keyword in sql, in that case you can put the column name in square bracket to distinguish between your custom column name and existing reserved keyword.
When having table names or filenames with spaces or dashes (-) etc... you can receive "Systax error in FROM clause".
Use [] brackets to solve this.
See: https://msdn.microsoft.com/en-us/library/ms175874.aspx
They are simply delimiters that allow you to put special characters (like spaces) in the column or table name
e.g.
insert into [Table One] ([Column Name 1], columnname2) values (val1, val2)