Selecting a column with period in the column name SQL Server - sql

I am linked to a Proficy Historian that allows periods in the column names. Because the data is stored in a non DBMS format I can not use openquery to get the data because there is no set schema to the tables. So I must use four part name syntax to get the data. This example works:
SELECT * FROM iHist...[SELECT * FROM ihTrend]
but this fails with Incorrect syntax near '.'.
SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07][0].F_CV.Value] FROM ihTrend]
where SERVER.pid_astatus[07][0].F_CV.Value is the name of the column
This fails as well with Incorrect syntax near the keyword 'from'.
SELECT * FROM
iHist...[SELECT [SERVER.pid_astatus[[07]][[0]].F_CV.Value] from ihTrend]`
Any ideas on how I can make SQL Server see this as a column?
EDIT:
Martins suggestion of the right brackets to escape the brackets work only on the outside of the sql call
SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM iHist...[SELECT * FROM ihTrend]
However it does not work inside Incorrect syntax near the keyword 'from'.
SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM ihTrend]
EDIT
SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value]] FROM ihTrend]
I had to escape the column escape :)

You only need to escape these ]
[pid_astatus[07]][0]].F_CV.Value]
This works for me
CREATE TABLE #t(
[pid_astatus[07]][0]].F_CV.Value] int
)
SELECT [pid_astatus[07]][0]].F_CV.Value]
FROM #t

(Edited to reflect new knowledge, if you like this vote for Martin Smith's answer instead!)
Escape the ] by doubling them:
SELECT * FROM
iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] from ihTrend]
Based on your comment, try:
SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM iHist...ihTrend

Related

SQL server select query with parenthesis

I created a table in sql server
called user,
but when i tried to query the table
it only works like this approch (with parenthesis):
Select * from [user]
when i try:
select * from user
it dosent seems to work 'incorrect synax near the keyword 'user'
why i need to add those parenthesis i know i need to add Parenthesis if there is a space between like colum name "first name"
why it is not working?
You can write SELECT statement without square brackets for database tables, columns. When you use square brackets, then you explicitly says to SQL Server engine to select data from table user:
Select * from [user]
But when you write select * from user, then SQL engine thinks not about table user, but about reserved keyword user. The correct query to see database user name looks like this:
select user

"Syntax error at or near ' , '" while trying to SELECT INTO

The query for selecting multiple values and assigning to multiple variables in a single SELECT query leads to an error. My Postgres version is 9.5.
The query is:
SELECT INTO region_id ,doc_type,tax_amt fk_bint_supplier_tax_region_id,chr_supporting_document_type,
dbl_base_currency_client_net-dbl_base_currency_market_fare-dbl_base_currency_cc_charge_collected+
dbl_base_currency_vat_in+dbl_base_currency_cc_charge_collected+(19*(dbl_base_currency_tax))*5/10
FROM tbl_sales_details WHERE chr_document_status='N' AND vchr_document_no='INV/47922/01/18'
AND vchr_supporting_document_no='5111143004'
The error is:
ERROR: syntax error at or near ","
LINE 1: SELECT INTO region_id ,doc_type,tax_amt fk_bint_supplier_ta...
^
********** Error **********
ERROR: syntax error at or near ","
SQL state: 42601
SELECT INTO in PL/pgSQL has a different meaning from
SELECT INTO in SQL. The latter is generally discouraged. The manual:
CREATE TABLE AS is functionally similar to SELECT INTO. CREATE TABLE AS
is the recommended syntax, since this form of SELECT INTO is not
available in ECPG or PL/pgSQL, because they interpret the INTO clause
differently. Furthermore, CREATE TABLE AS offers a superset of the
functionality provided by SELECT INTO.
The error message indicates you tried to run the statement as plain SQL.
There's nothing wrong with your placement of the INTO clause when used in PL/pgSQL like you tagged. You also stated that it's for:
assigning to multiple variables
That, too, only makes sense inside procedural language code as there are no variable assignments in plain SQL.
Related:
SELECT INTO with more than one attribution
You put the into after the column list:
SELECT region_id, doc_type,tax_amt fk_bint_supplier_tax_region_id, chr_supporting_document_type,
(dbl_base_currency_client_net - dbl_base_currency_market_fare -
dbl_base_currency_cc_charge_collected +
dbl_base_currency_vat_in + dbl_base_currency_cc_charge_collected + 19 * dbl_base_currency_tax
) * 5/10
INTO . . .
FROM tbl_sales_details
WHERE chr_document_status = 'N' AND
vchr_document_no = 'INV/47922/01/18' AND
vchr_supporting_document_no = '5111143004';
I don't know what the variable names are, but the go after the INTO and there must be one for each expression in the SELECT.

Using CAST or CONVERT in CONTAINS - SQL Server

I'm trying to use the CAST or CONVERT functions in a SQL query like this :
SELECT *
FROM [SQL_BMS].[dbo].[GEID]
WHERE CONTAINS(CONVERT(nvarchar(max), NUID), 'titre')
or
SELECT *
FROM [SQL_BMS].[dbo].[GEID]
WHERE CONTAINS(CAST(NUID as nvarchar(max)), 'titre')
but I'm getting errors:
Incorrect syntax near the keyword 'CONVERT'
or
Incorrect syntax near the keyword 'as'
So how can I cast my NUID column from Int to nvarchar?
You need to create a view with SCHEMABINDING, with some ID and concatenated columns.
CREATE VIEW dbo.[view_GEID]
WITH SCHEMABINDING
AS
SELECT NUID,
Col1+Col2+CAST(Col3 as nvarchar(50))... as ConcatColumn
FROM dbo.GEID
Then create full-text index on that view. Then you can use CONTAINS for search purpose.
SELECT *
FROM [SQL_BMS].[dbo].[view_GEID]
WHERE CONTAINS(ConcatColumn, 'titre')
Another way is to concatenate all columns you need and use LIKE, but it could lead to loss of performance.
SELECT *
FROM [SQL_BMS].[dbo].[GEID]
WHERE Col1+Col2+CAST(Col3 as nvarchar(50))... LIKE '%titre%'
Try this:
SELECT *
FROM [SQL_BMS].[dbo].[GEID]
WHERE CONTAINS(CONVERT(nvarchar(max),NUID), 'titre')

How do I deal with SQL tablenames with hyphen (-) when writing raw queries? i.e project-users

I have a table called project-users and want to write a SQL query like SELECT * FROM project-users I get this error ERROR: syntax error at or near "-".
I cannot change the table name at this point.
According to http://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html, you should use double quotes.
In your case, for PostgreSQL the query should be:
SELECT * FROM "project-users";
It is good practice to avoid the use of characters that need escaping or that contain spaces in identifiers.

Squirrel SQL Error Code 102

I'm very confused - I opened up my database in Squirrel SQL to do some SQL and I type this in my SQL entry window:
select * from *
And for whatever reason it gives me this output:
Error: Incorrect syntax near '*'.
SQLState: 42000
ErrorCode: 102
Does anyone know what this means? It's hard to do a google search because Google treats the asterisk as a special character.
I think that second * is invalid, that should be a table name:
SELECT * FROM MyTable
You need to specify the table name.
SELECT * FROM myTable
If however you knew that and you are actually looking for a way to SELECT from all your tables i'm afraid you'll have to type them one by one like this :
SELECT * FROM myTable1
UNION ALL
SELECT * FROM myTable2
UNION ALL
SELECT * FROM myTable3
...
Also remember that if you have to Select like I did on the second example, there is a problem with the way your data is organized in you database. If it's the case, you should take a look at this article on Data Normalization.