Problems with relational operators in a varchar field in sqlite - sql

I have this syntax sql who should pick up all the records in the table where the 'stock_ quantity' (Varchar Column) column is less than the 'minimum_amount' (Varchar Column) column:
SELECT * FROM fazerem_xius WHERE stock_quantity < minimum_amount ORDER
BY name_products ASC
My syntax have a problem, if the column 'stock_quantity' stores the value 10, and column 'minimum_amount' stores the value 3, This record has been returned by the database, why is this happening and how to solve this type of problem?

Convert the string to a number before comparing. For instance with a mathematical operation
SELECT * FROM fazerem_xius
WHERE stock_quantity * 1 < minimum_amount * 1
ORDER BY name_products ASC

It's because string values are compared alphabetically. When you compare the string 10 to the string 3, SQLite checks the first characters first. 1 is before 3 in the alphabet, that's why the result of '10' < '3' is true.
Use CAST() to cast them to numbers:
SELECT * FROM fazerem_xius WHERE CAST(stock_quantity AS INTEGER) < CAST(minimum_amount AS INTEGER) ORDER BY name_products ASC

Related

How to SELECT records from a temp table applying a specific condition?

I have dumped a file into a temp table and trying to pull records where the score is <= 100. The column has values for example, 3, 4.50, 10.02, 99.88,99,99, 100, 100.2, 100, 116, 116.44 etc. I need only 3, 4.50, 10.02,99.88,99,99,100. When I tried the below, it does not give the proper result. Could anyone advise?
SELECT CAST(sum AS numeric ),* FROM temp WHERE sum >= '100' ;
There are two issues here. First, you're using the >= operator instead of the <= operator. Second, sum seems to be a string column, so the comparison is performed lexicographically. You could apply the same casting you've applied in the select list and compare the value to a number literal 100, not a string literal '100':
SELECT CAST(sum AS numeric ),* FROM temp WHERE CAST(sum AS numeric) >= 100;

How to use between condition with numbers for string columns in Oracle

I need to check the between condition for two numbers in the tables but the column in oracle DB has string values .
I tried the following query , nothing helped me
select * from sys.employee_infor where to_number(emp_number) between 1200 and 2400;
select * from sys.employee_infor where (emp_number) >= to_char(1200) and (emp_number) <= to_char(2400);
select * from sys.employee_infor where to_number(emp_number) between '1200' and '2400';
Received error as :
ORA-01722: invalid number
My Emp_number column be like as,
The safer solution is to use CASE:
SELECT *
FROM sys.employee_infor
WHERE CASE WHEN NOT REGEXP_LIKE(emp_number, '\D') THEN TO_NUMBER(emp_number) END BETWEEN 1200 AND 2400
It is likely that you have data in the employee number column that cannot be converted to a number.
Assuming that the employee number is an integer value, the following query will show you the offending rows :
select * from sys.employee_infor where regexp_like(emp_number, '[^0-9]');
You can use that where clause in your query to ignore badly formatted data :
select * from (
select * from sys.employee_infor where not regexp_like(emp_number, '[^0-9]')
) where to_number(emp_number) between 1200 and 2400
PS : as you are looking to compare numbers, not strings.
Another solution is to use the DEFAULT ... ON CONVERSION ERROR option of function TO_NUMBER(), which is available starting Oracle 12c R2. With this option conversion errors are trapped and a default value is returned instead of throwing an error :
select *
from sys.employee_infor
where to_number(emp_number default 0 on conversion error) between 1200 and 2400
Under many circumstances, this will work:
where emp_number >= '1200' and emp_number <= '2400'
or:
where emp_number >= '1200' and emp_number <= '2400' and
length(emp_number) = 4
This is not exactly the same, because it is using string comparisons and not numeric comparisons. On the other hand, it can take advantage of indexes.

order clause for varchar ranges

I currently have a column in db with few ranges storaged as varchar, such as:
0-499
1000-1199
500-999
How do I order these ranges like the following:
0-499
500-999
1000-1199
Thanks in advance
If you want to be tricky, you can do:
order by cast(replace(col, '-', '.') as decimal(30, 15))
This replaces the hyphen with a decimal point, converts to a numeric value, and uses that for sorting. This should work in just about any database.
This is not perfect, because it does not really order by the second number of the range correctly. But the first number would need to exactly match (and for some reason, that seems unlikely to me base on your sample data).
Order by the characters before the hyphen, converted to an integer.
You can useorder by clause with left() function :
order by cast(left(n, charindex('-', n)-1) as int);
However, the preceding order by cluase has int conversation, if you have decimal value before hyphen then use decimal instead
if these are the only values:
order by
case varcharcol when '0-100' then 1
when '500-1000' then 2
when '1000-1199' then 3
end
create table #temp1(id int,range varchar(50))
insert into #temp1(id,range)
values (1,'0-499'),(2,'1000-1199'),(3,'500-999')
select * from #temp1 order by cast(replace(range, '-', '.') as decimal(30, 15))
id range
1 0-499
3 500-999
2 1000-1199
select * from #temp1 order by cast (substring(range,0,charindex('-',range)) as int)
id range
1 0-499
3 500-999
2 1000-1199

SQL Server: how to add case statement to select

I am using the following select to query a date from a database table.
The input (ms) for this query results from an xml string and the stored procedure then loops through all the single values in the xml to return a certain number (integer) for each of them.
This works fine so far.
Is there a way that I can return a placeholder number (like 99999) if the input (ms) is empty / nothing ?
Currently the below returns 0 in such a case which I cannot use to identify this as 0 can also be a valid result in other cases.
My stored procedure so far:
SELECT ms as date,
type,
(
SELECT COUNT(calendar_dt)
FROM Calendar
WHERE day_of_week NOT IN (1, 7)
AND calendar_dt > GETDATE()
AND calendar_dt <= ms
) as bDays
FROM #dates
FOR XML PATH('ms'), ELEMENTS, TYPE, ROOT('ranks')
Many thanks in advance for any help with this, Tim.
If the column "ms" is actually NULL or populated, just use ISNULL.
http://technet.microsoft.com/en-us/library/ms184325.aspx
SELECT ISNULL(ms, 99999) AS date
However, if that column can contain an empty string, which is not the same as NULL, then also use NULLIF.
http://technet.microsoft.com/en-us/library/ms177562.aspx
SELECT ISNULL(NULLIF(ms,''), 99999) AS date

Teradata - Invalid Date supplied for FIELD

I'm trying to query a table that has a varchar(100) "VALUE" column. This column can hold anything from a letter, a number or, in this case, a date.
The date will always be entered in the table as 'YYYY-mm-dd'. However, when I run the following query:
select * from myTable
where VALUE = '2009-12-11' (Date, Format 'yyyy-mm-dd')
I receive the following error:
Invalid date supplied for myTable.VALUE.
Example of the value table:
(1,'122')
(2,'red')
(3,'2009-12-11')
Any ideas as to what might be causing this?
Thanks!
if the data type is declared as varchar, it should just treat it like a string.
try not specifying anything about the date format, like
select * from myTable
where VALUE = '2009-12-11'
If you run an explain on the query, you can see that it's casting value to date before comparing against your supplied value. If you have another column that accurately records the type of what's in VALUE, you can add that to the where clause and you will no longer get the error (see below). Otherwise, go with Beth's recommendation.
select * from myTable
where VALUE = '2009-12-11' (Date, Format 'yyyy-mm-dd')
and VALUE_TYPE = 'DATE';
Teradata internal date calculation is (year - 1900) * 10000 + (month * 100) + day.
So if date is 02/11/2009 (2nd November 2010) then
=(2009-1900) * 10000 + (11 * 100) + 2
=109 * 10000 + 1100 + 2
=1090000 + 1100 + 2
=1090000
1100
2
----------
1091102
----------
So 2nd november 2009 is stored in Teradata as 1091102.
You can extract it in required format by casting (as u have it in varchar). Hope this helps.
Is it possible that VALUE is a reserved word in Teradata?
If so, you need to put that into double quotes:
select *
from myTable
where "VALUE" = '2009-12-11'