Numeric bug on access db - sql

I have this SQL:
SELECT *
FROM table
WHERE number >= '" + numberFrom + "'
AND number <= '" + numberTo + "'
ORDER BY number DESC
(number is "text" field)
numberFrom and numberTo are a range from "1080" to "2000",
but if i have one record with number "108" the SELECT find this record (even if we know that is out of the range).
How i can fix this?

You are doing alphabetic comparison where for that matter '9' > '19'.
If you want to compare numbers you should use numbers.

Related

Timestamp query in mysql

WHat is the issue with my DATE / Timestamp query?
Please point out.
I am filtering the data based on the date, filter as greater than a timestamp and less than another timestamp.
I tried using :
Select *
from TRANSACTION_LOG
where DATE_FORMAT(ORIGIN_TIMESTAMP,'%Y-%m-%dT%H:%i') > '" +
2018-01-19T11:11+ "'
&& DATE_FORMAT(ORIGIN_TIMESTAMP,'%Y-%m-%dT%H:%i') < '" +
2018-04-01T01:01 +"';
AND this one also -->>
Select *
from TRANSACTION_LOG
where DATE_FORMAT(ORIGIN_TIMESTAMP,'%Y-%m-%dT%H:%i') > '" +
2017-01-10T11:52:12.000Z + "'
&& DATE_FORMAT(ORIGIN_TIMESTAMP,'%Y-%m-%dT%H:%i') < '" +
2018-05-10T11:52:12.000Z +"';
neither of them work
What is the "+ ? MySQL should recognize ISO standard formats for date/times.
The following should work in MySQL:
Select *
from TRANSACTION_LOG
where ORIGIN_TIMESTAMP > '2018-01-19T11:11' and
ORIGIN_TIMESTAMP < '2018-04-01T01:01';

Combine Print by SQL with different datatype

Here is my SQL from ms-access:
SELECT WSID,
StartDate +' ' + StartTime + ' '+ Duration, + ' ' + Descp as WSP,
TID
FROM Workshop
I'm trying to print out expected result like this:
11/2/2016 1200 90 WORKSHOP 2
but since the datatype of startdate is date/time, StartTime is shorttext and duration is shorttext. Once i execute the SQL it get result like this:
Is this the issue of different datatype? or any solution for this? Thank you
In MS Access, & is used for string concatenation. Remember, + can be confused with addition of numbers. So, I would expect:
SELECT WSID,
(StartDate & ' ' & StartTime & ' '& Duration & ' ' & Descp) as WSP,
TID
FROM Workshop;
Note that I also removed the comma after Duration so get three columns in the result set rather than four.

Select a record with the largest sum of fields (Access 2010)

I would like to select a record from a table based on the field “labcode” specified by the user on a form. There could be multiple records associated with each “labcode” and I would like to select a record that has the highest sum of 10 corresponding fields in the “tblDSA". Fields are named as follows: “A1_MFI”, “A2_MFI”, “C1_MFI”, "C2_MFI", "DR1_MFI", "DR2_MFI"…)
All 10 fields are in 'text' format and sometimes contains a number, text or are left blank. I would only like to sum up records that contain a number in that field. Do I need to create a new field in “tblDSA” that holds the total score or should I avoid storing calculating values in the table?
Dim SQL As String
Dim db As DAO.Database
Dim tblDSA As DAO.Recordset
Set db = CurrentDb
Set tblDSA = db.OpenRecordset("tblDSA")
SQL = "SELECT * Nz((Val[A1_MFI])) + Nz((Val[A2_MFI])) + Nz((Val[B1_MFI])) + Nz((Val[B2_MFI])) + Nz((Val[C1_MFI])) + Nz((Val[C2_MFI])) + Nz((Val[DR1_MFI]))+ Nz((Val[DR2_MFI])) + Nz((Val[DQB1_MFI] + Nz((Val[DQB2_MFI]))as TotalScore FROM tblDSA WHERE [LABCODE] = " & Me.tbLabcode.Value & " ORDER BY TotalScore DESC "
Debug.Print SQL
Set rs = db.OpenRecordset(SQL)
The SQL above contains a syntax error (missing operator), therefore, I can't test it. I'm not sure what is missing?
Nz() is for skipping blank records and Val() is to convert each text field into value. Please let me know if this is a correct approach or I need to do something else? Thanks
Okay, after much back and forth, here is the final result that works for this particular problem:
SELECT TOP 1 *, (Nz(Val(IIf([A1_MFI] Is Null, 0, [A1_MFI]))) + Nz(Val(IIf([A2_MFI] Is Null, 0, [A2_MFI]))) + ...) AS TotalScore
FROM tblDSA
WHERE [LABCODE] = 57
ORDER BY (Nz(Val(IIf([A1_MFI] Is Null, 0, [A1_MFI]))) + Nz(Val(IIf([A2_MFI] Is Null, 0, [A2_MFI]))) + ...) DESC
I thought Access allowed field aliases in the ORDER BY, but it doesn't seem to do that any more, if it did at all.
It looks like you two things
you didn't have a comman after "SELECT *"
missing two brackets in one of your NZ statements
#PhillipXT pointed out the first - and by using his second suggestion, I think the SQL compiler would have pinpointed the missing brackets for you.
Try this with a copy / paste
SQL = "SELECT *, Nz((Val[A1_MFI])) + Nz((Val[A2_MFI])) + Nz((Val[B1_MFI])) + _
Nz((Val[B2_MFI])) + Nz((Val[C1_MFI])) + Nz((Val[C2_MFI])) + Nz((Val[DR1_MFI])) + _
Nz((Val[DR2_MFI])) + Nz((Val[DQB1_MFI])) + Nz((Val[DQB2_MFI])) AS TotalScore _
FROM tblDSA _
WHERE [LABCODE] = " & Me.tbLabcode.Value & _
" ORDER BY TotalScore DESC "

My C# SQL Select statement is not picking up any records in my table

I have 4 records in my SQL table that and I'm doing a SQL select statement to select records based on a certain criteria but I'm not picking any records up. Can someone please help?
Here is my SELECT statement:
string Sql = "";
Sql = #"SELECT * FROM " + _dtlName + "
WHERE Shipper_No = '" + sidNo + "'
AND Job_Code != '" + "R" + "'";
I have 3 records that have a Null for Job_Code and 1 record that has an R.
Based on that, I should pickup the 3 records with the NULL Job_Code but it returns 0 records.
I suspect the problem is that a comparison between any non-null value and a null value doesn't return a value of true or false, but null. So your query should probably be:
string sql = "SELECT * FROM " + _dtlName + " WHERE Shipper_No = #ShipperNo " +
"AND (Job_Code IS NULL OR Job_Code != 'R')";
(Note that I've extracted a parameter for Shipper_No - you shouldn't be including values directly in your SQL like that. Obviously you'll need to then set the parameter value in the SQL command.)
Also note that <> is more common in SQL to represent "not equal to", but I believe T-SQL allows either form.
Try,
Sql = #"SELECT * FROM " + _dtlName + " WHERE Shipper_No = '" + sidNo + "' AND Job_Code NOT IN ('R')";
I think your query should be more like this since you want to select the NULL values.
string Sql = String.Format("SELECT * " +
"FROM {0} " +
"WHERE Shipper_No = {1} AND " +
" Job_Code IS NULL",
_dtlName, sidNo)

Sorting null-data last in database query

i have an application where i order make a query to my database with an ORDER BY clause, it will order them in alphabetical order. i only have one small problem, it happens fairly often that one of the strings that the query is ordering by contains nothing (string="") when sorting in alphabetical order these get populated at the top of the list infront om a,b,c... i plain and simple dont want this. after a lot of googling i found on an oracle forum that i should change the SORT BY part of the query to "SORT BY xxx ASC NULLS LAST" this caused a fatal error when querying.
how shall i go by fixing this seemingly small issue?
here is my query statement as is today.
public Cursor fetchAllDatesByTag() {
return mdiktationsDb.rawQuery("SELECT " + KEY_DATEID + "," +" " + KEY_DATE + "," + " " + KEY_TIME + "," + " " + KEY_DICTTAG + "," + " " + KEY_DICTLISTIMAGE + " FROM " + DATABASE_TABLE + " ORDER BY " + KEY_DICTTAG + " ASC", null);
}
use a CASE equivalent in your ORDER BY
Like
ORDER BY CASE column WHEN NULL THEN 1 ELSE 0 END, column
so then it orders by the nulls first, then the actual column.
EDIT: And if you want to filter ""s (blank strings) or whatever else, you can employ this same method... assigning a numeric value to it and sorting before the alphabetizing.
EDIT2:
....+ " ORDER BY CASE " + KEY_DICTTAG + "WHEN NULL THEN 1 ELSE 0 END, " + KEY_DICTTAG + " ASC"
Solution one:
ORDER BY foo NULLS LAST
and
ORDER BY foo NULLS FIRST
But this seems to work only with numeric columns :(
Solution two:
ORDER BY IF(ISNULL(my_field),1,0),my_field
which will create a "fake"-column that just consist 0 or 1, depending on if my_field is null or not null, and sort on that. the fields that are not null will come first. in a second step, SQL will sort my_field, like before.
How about adding a WHERE field IS NOT NULL. This way you shouldn't get any null values back from your query.