How to fix this Syntax error in query expression? - database

I am working on Standard Access. I wrote this code:
SELECT *
FROM room
WHERE price < 40 AND
type IN ('Double", "Single")
ORDER BY price;
when I run it, it's telling me this message
Syntax error in string in query expression price < 40 AND type IN
('Double", "Single") ORDER BY price;

String literals in SQL are denoted by single quotes, not double quotes:
SELECT * FROM room WHERE price < 40 AND type IN ('Double', 'Single') ORDER BY price

You cannot mix single- and double-quotes. Also, type is a reserved word.
So try:
SELECT *
FROM room
WHERE price < 40 AND [type] IN ("Double", "Single")
ORDER BY price;

Related

Integer function in snowsql

I am trying to run this query in snowflakes, but I keep having the error below. Can someone please tell me what I am doing wrong. I am trying to get the integer value for the ages.
SELECT DISTINCT Target_quikvalf.MPOLICY
FROM Target_quikvalf
WHERE (((Target_quikvalf.MPOLICY) Like '%N')
AND ("DATE" (Target_quikvalf.MISSUE) Between '5/1/2020' And '5/31/2020')
AND (as_integer((Target_quikvalf.MAGE) / 5)*5))
Error Message:
SQL compilation error: invalid type [FLOAT] for parameter 'AS_INTEGER(variantValue...)'
The placement of where you are applying the function is the issue. Here we are converting the data type before applying the mathematical calculations.
You'll need to cast the FLOAT to INT can cast it like so:
SELECT DISTINCT Target_quikvalf.MPOLICY
FROM Target_quikvalf
WHERE (((Target_quikvalf.MPOLICY) Like '%N')
AND ("DATE" (Target_quikvalf.MISSUE) Between '5/1/2020' AND '5/31/2020')
AND ((cast(Target_quikvalf.MAGE as int) / 5) * 5);
As an alternative, we can try converting the FLOAT to a number using TO_NUMBER. If Target_quikvalf.MAGE column was a variant data type, then you'd be able to run it like so (in the example below, we are forcing the converted float number to not have a tenths place and can support ages below 1000 since people can be over 100 years old):
SELECT DISTINCT Target_quikvalf.MPOLICY
FROM Target_quikvalf
WHERE (((Target_quikvalf.MPOLICY) Like '%N')
AND ("DATE" (Target_quikvalf.MISSUE) Between '5/1/2020' AND '5/31/2020')
AND ((to_number(Target_quikvalf.MAGE, 3, 0) / 5) * 5);
SELECT as_integer(5.1);
generates
SQL compilation error: invalid type [NUMBER(2,1)] for parameter 'AS_INTEGER(variantValue...)'
so don't use a function that is intended to "parsing variant data" to truncate you number, instead
cast
SELECT 5.1::int;
gives:
5.1::INT
5
given you are not getting the value scaled as you expect, let break it down:
SELECT 68 as age
, age/5 as float_scale
, float_scale::int as cast_age
, round(float_scale,0) as round_age
, trunc(float_scale,0) as trunc_age
, trunc_age * 5 as scaled_back;
gives:
AGE FLOAT_SCALE CAST_AGE ROUND_AGE TRUNC_AGE SCALED_BACK
68 13.600000 14 14 13 65
casting, and rounding appear to not be what you want, thus TRUNC is the key here
thus your code should be
(TRUNC(Target_quikvalf.MAGE / 5) *5)

how to remove double quotes from an Array

I had created a table in hive with ORC format and loaded data into the table. I have use collect_set to eliminate the duplicates as follows to insert the data. However, i see double quotes in the array. Is there anyway to remove those double quotes?
This is an sample data iim getting from table a and inserting into the table b using:
insert into table b
select a.name as name, collect_set(b.sub) as subjects from a group by a.name;
my table be would be like this:
name | subjects
john | ["Eng", "Math", "Phy"]
Sarah | ["Math", "Chem"]
I want to get ride of the double quote in the array to look like this:
name | subjects
john | [Eng, Math, Phy]
Sarah | [Math, Chem]
Is there anyway to do this using hql?
Array is an object and to be displayed it needs to be transformed to string.
When you select array, it is being transformed(serialized) to string. Hive displays array as comma separated values, double quoted, in square brackets.
Consider this example:
select array('Eng', 'Math', 'Phy');
Returns:
["Eng","Math","Phy"]
What I'm trying to say is that there is no double-quotes " in the initial data most probably, it is being serialized to String with double-quotes when you select it directly without explicit conversion to string.
If this is the real reason of double quotes in the select result, then the solution is to transform array to string explicitly:
select concat('[',concat_ws(',',array('Eng', 'Math', 'Phy')),']');
Returns:
[Eng,Math,Phy]
Is it what you expected?
If not and you really need to remove double-quotes from column value, then regexp_replace will do.
Example of array containing double quotes in the values:
select concat('[',concat_ws(',',array('"Eng"', '"Math"', '"Phy"')),']');
Returns:
["Eng","Math","Phy"]
In such case you can apply regexp_replace when loading your table
regexp_replace(string, '["]', '') --this will remove double-qutes
Your insert statement will look like this:
insert into table b select a.name as name, collect_set(regexp_replace(sub, '["]', '')) as subjects from a group by a.name;

how to convert the in date when len of int is greater than 17 in SQL

The int value is '1192060800000000' then following select statement works properly ie. length 16
SELECT DATEADD(day,clinicaldate/1000000/3600/24,'01-01-1960') FROM document
Refered - https://www.qvera.com/kb/index.php/743/can-change-clinicaldate-from-centricity-document-into-date
If the length is greater than or equal to '18' and having (-) minus sign at the beginning of the int (-58635680520000000)
then it gives the following error-
'Adding a value to a 'datetime' column caused an overflow'
I would like to know how I can ge the result for 18 digit int or how can we exclude such records in the query?
cast(clinicaldate as varchar(20)), use substring() to extract the section you actually want to work with, then do your math.

How do I do decimal arithmetic on two varchars and return result to an aliased column?

I have two fields of type varchar that contain numeric values or blank strings, the latter of which I have filtered out to avoid Divide by Zero errors.
I am attempting to determine the percentage value that num2 represents in relation to num1, i.e. (Num_2 * 1 / Num_1). Relatively simple math.
The problem I am having is that I cannot seem to do the math and then cast it to a decimal value. I keep receiving Arithmetic overflow error converting int to data type numeric errors.
Can someone help me out with the casting issue?
You didn't interpret the error correctly.
It is not about casting the result of your math to float, it is about implicit type casting before the equation is evaluated.
You have in your table some values that cannot be converted to numeric, because they are not valid numbers or numbers out of range. It is enough that one row contains invalid data to make fail the whole query.
perhaps you're looking for something similar to this?
declare #table table (
[numerator] [sysname]
, [denominator] [sysname]);
insert into #table
([numerator],[denominator])
values (N'1',N'2'),
(N'9999999999',N'88888888888');
select case
when isnumeric([numerator]) = 1
and isnumeric ([denominator]) = 1
then
cast([numerator] as [float]) / [denominator]
else
null
end
from #table;
Is this what you're looking for?
select cast('25.5' as decimal(15, 8)) / cast('100.0' as decimal(15, 8))
The example above will return this:
0.25500000000000000000000
In this case, I'm converting the operand types before they get used in the division.
Remember to replace the literals in my query by your field names.
you said that can be number or blank string.
son try something like this:
SELECT
(CASE WHEN NUM_2 = '' THEN 0 ELSE CAST(NUM_2 AS NUMERIC(15,4)) END)
/
(CASE WHEN NUM_1 = '' THEN 1 ELSE CAST(NUM_1 AS NUMERIC(15,4)) END)
you test if string is blank. if it is, you use 0 (or 1, to avoid division by zero)

Oracle select column with special name

I have a view containg a column named "Started At". How do i select this in a SELECT statement ?
Thank you!
Try with
SELECT "Started At"
FROM your_table
You can break the Oracle Database schema object name rules (no reserved words, start with A-Z, length 30 char, etc..) by enclosing the name within double quotes. To later access the object you MUST enclose the name by double quotes.
To the point:
me#XE> create table t ("x" int);
Table created.
me#XE> select x from t;
select x from t
*
ERROR at line 1:
ORA-06553: PLS-306: wrong number or types of arguments in call to 'OGC_X'
me#XE> select "x" from t;
no rows selected
me#XE> create view v as select * from t;
View created.
me#XE> select x from v;
select x from v
*
ERROR at line 1:
ORA-06553: PLS-306: wrong number or types of arguments in call to 'OGC_X'
me#XE> select "x" from v;
no rows selected
me#XE>

Resources