Using CAST or CONVERT in CONTAINS - SQL Server - sql

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')

Related

sql conversion of data types cast and convert

I have two columns in my table: AmountIncl_LC, AmountExcl_LC which are nvarchar data type.
Now O want to change data type of these two columns and sum two columns into a one column result us.
select cast(AmountIncl_LC as int)int [AmountIncl_LC]
from dbo.table2
I used the above query and it is throwing me an error saying
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'AmountIncl_LC'.
Try this:
select cast(AmountIncl_LC as numeric(16,8))+cast(AmountExcl_LC as numeric(16,8)) from dbo.table2
try this
select cast(AmountIncl_LC as int)+cast(AmountExcl_LC as int) from dbo.table2
The question doesn't contain all the details. The DB seems to hold ints in decimal format stored as Nvarchar, which in the first place isn't a great approach. You should try cast:
select try_cast(AmountIncl_LC as decimal)+try_cast(AmountExcl_LC as decimal) from dbo.table2
CAST function could be helpful to change the data type while Querying, and use the
addition operator "+" to add both columns. Further, you can use the "AS" command to create a new column ;
As far as my understanding, the only error in your code is variable mismatch and Syntax error;
select CAST(AmountIncl_LC AS int) + CAST(AmountExcl_LC AS int) as New_Column from dbo.table2;

"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.

Create Table from View - Oracle SQL SQL Error: ORA-01723: zero-length columns are not allowed

I need to create a table from view with statement like
CREATE TABLE NEW_TABLE AS
SELECT *
from VIEW
It is giving error message as below. It is not possible to create table from view (with Select * statement)?
Error report -
SQL Error: ORA-01723: zero-length columns are not allowed
01723. 00000 - "zero-length columns are not allowed
I have this problem when NULL is specified for a column, but the type is not specified. Arrgh!
You will need to look at the code. This often happens when I use:
select '' as x
Because I think '' should have the right type.
In any case, the solution is simple:
select cast(NULL as varchar2(255)),
cast(NULL as number)
or whatever the type is.
You'll need to either change the view definition, or use a query with a subquery with explicit casts.

How to select result with empty set

I have sql query:
select * from table where id in (1,2)
1,2 are parameters which I add there dynamically. But what If I have empty set:
select * from table where id in ()
then this query call exception:
ERROR at line 1:
ORA-00936: missing expression
how should I create sql with empty set
You can always add null to your set, so when the real set is empty, you get no syntax error:
select * from table where id in (null,1,2)
vs
select * from table where id in (null)
Your generator would be simpler, too, because you can print a , in front of every item that you add, without checking if it's the first one or not.
Of course since you are generating your SQL, the standard precautions against SQL Injection apply: do not let user input anywhere near the SQL that you generate.
Try this
select * from table where id in (null)

Selecting a column with period in the column name SQL Server

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