Can't compare a values in access - sql

Here is the code, the field Автомобили.Описание is instance of Short Text in a result table of a Автомобили query
SELECT Автомобили.НомVIN, Автомобили.ФИО, Автомобили.РегНомер, Автомобили.Описание
FROM Автомобили
WHERE (((Year(Now()) - CInt([Автомобили].[Описание]) = 40)))
ORDER BY Автомобили.ФИО, Автомобили.Описание;
The thing is that if I want to just compare values in WHERE clause only with '=' it seems fine, everything works properly. But when I try to compare them by '<' or '>' or '<=' or '>=" the Access throws an error "Datatype mismatch in criteria expression". What is wring with it?

Year() returns an Integer, so your criteria examples are correct and perfectly valid. So, it is something else.
Try to leave out the ordering or sort on the numeric value:
SELECT
Автомобили.НомVIN,
Автомобили.ФИО,
Автомобили.РегНомер,
Автомобили.Описание
FROM
Автомобили
WHERE
Year(Date()) - CInt([Автомобили].[Описание]) = 40
ORDER BY
Автомобили.ФИО,
CInt(Автомобили.Описание);

Related

SQL overlap statement

I'm trying to do overlap in SQL (using postgres) but receive an syntax error. And i do not know why I'm getting error and whats wrong with my SQL Statement
Error text
[42601] ERROR: syntax error at or near "[" Position: 296
SQL Statement
SELECT *,
ARRAY_AGG("reserve"."state_id" ) AS "states" FROM "orders_order"
JOIN "reserve" ON ("order"."id" = "reserve"."order_id")
GROUP BY "order"."id"
HAVING (NOT (ARRAY_AGG("reserve"."state_id" )&& ['test']::varchar(255)[]))
As documented in the manual there are two ways to specify an array:
First with the array keyword:
array['test']
or as a string enclosed with curly braces:
'{test}'::text[]
In the second case the casting might or might not be needed depending on the context on where it's used.
There is no need to cast it to a varchar(255)[] array. text[] will do just fine - especially when comparing it to the result of an array_agg() which returns text[] as well.

Treat TO_NUMBER() invalid format errors as NULL

I have a string column which usually contains integers in two formats... zero-padded, and not:
5
05
I want to sort based on these values numerically. To do that I do something like:
SELECT * FROM things ORDER BY TO_NUMBER(num, '0000');
This works fine, but sometimes there is invalid data, like abc, or !## in this column. Postgres becomes unhappy with me:
ERROR: invalid input syntax for type numeric: " "
What I'd like to do is treat invalid values/failures of TO_NUMBER() as NULL so that they are sorted accordingly. Is this possible? Or, some other alternative?
If you are using PostgreSQL, you can use this query:
SELECT * FROM things ORDER BY
TO_NUMBER((case when num ~ '^[0-9\.]+$' THEN num else '0' end),'0000');

SQL query problem in WHERE clause, this returns all that start with

I've written the following SQL query to return all sites having "id" equal to 2.
SELECT * FROM `sites` WHERE id = '2'
And it works well. The problem is that even if I add some characters after "2" like this :
SELECT * FROM `sites` WHERE id = '2etyupp-7852-trG78'
It returns the same results as above.
How to avoid this ? that's to say return none on the second query ?
Thanks
The reason is that you are mixing types:
where id = '2'
------^ number
-----------^ string
What is a SQL engine supposed to do? Well, the standard approach is to convert the string to a number. So this is run as:
where id = 2
What happens when the string is not a number? In most databases, you get a type conversion error. However, MySQL does implicit conversion, converting the leading digits to a number. Hence, your second string just comes 2.
From this, I hope you learn not to mix data types. Compare numbers to numbers. Compare strings to strings.

create a query with combobob values in access

I have the following query in access where i want to compare a text column value to a combo value
SELECT NAME.ID, NAME.V_name, NAME.Gender, NAME.V_center, NAME.BoxNo, NAME.SerinBox
FROM NAME
WHERE ((cint([Forms]![Name]![Combo75].value) >= 2020-cint(left([name.id])))
and (cint([Forms]![Name]![Combo77].value) <= 2020-cint(left([name.id]))));
getting error in SQL statement, couldn't figure it out.
Image of error
The error is pretty clear:
Wrong number of arguments . . .
The code in question is:
left([name.id])
The left() function takes two arguments. The second is how many characters to return.

Postgresql ERROR: operator does not exist: date ~~ unknown

When I do this query, I have no problems:
SELECT a.value, b.label AS campo, 'date' AS tipo
FROM contenido.field_value_textarea a
JOIN estructura.field b ON a.field=b.id
WHERE a.value LIKE '%aaa%'
contenido.field_value_textarea is character varying(2000)
But if I try to select from:
contenido.field_value_fecha which type is date I got this error message:
ERROR: operator does not exist: date ~~ unknown
What I'm trying to do is searching between different tables, each query select FROM it's table. Some tables use text values, textarea values, integer values, and it works, but when the value is date all fails. What can I do?
EDIT: By the way, my date values are like this: 2009-05-01
The ~~ operator is actually the LIKE operator.
You are trying to use an expression that looks like:
contenido.field_value_fecha.value LIKE '%aaaa%'
That is, you're trying to compare a date with a string (which, without the adequate context, is considered to be of type 'unknown'), and decide if the date looks like something.
If you actually want to do such a comparison, you need to convert the date to a string, which can be done by means of:
contenido.field_value_fecha.value::text LIKE '%aaaa%'
or (using standard SQL):
CAST(contenido.field_value_fecha.value AS text) LIKE '%aaaa%'
This will be syntactically correct... Whether it is meaningful or not, is a different part of the story.