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';
Related
I am checking my database to see if there are any overlapping appointments. It works for ADDing appointments, but when I go to UPDATE my SQL counts the record I am updating as an overlapping appointment when I try to change it. Here is the SQL I am using in the UPDATE statement.
"SELECT * FROM appointments " +
"WHERE ('" + start + "' BETWEEN Start and End OR '" + end + "'
BETWEEN Start AND End OR '" + start + "' > Start AND '" + end + "' < End AND
Appointment_ID <> " + id + ")";
This still pulls the record with the appointment id that I'm giving it. Anyone have any suggestions on how to exclude any appointments that have the id I'm updating but still meeting the other criteria.
currently, you are only checking for the id of the appointment to update when the third clause you have chained with ORs triggers because of the order of operations for OR and AND. Your query fetches a result when the start date is overlapping with the appointment OR your end date is is overlapping with the appointment OR it has a different ID and the dates are encapsulating the appointment.
Also, you shouldn't need to have brackets around your whole WHERE clause but you can use the opening bracket and move the closing bracket to make sure that the condition with the id is always evaluated like so:
"SELECT * FROM appointments " +
"WHERE ('" + start + "' BETWEEN Start and End OR '" + end + "'
BETWEEN Start AND End OR '" + start + "' > Start AND '" + end + "' < End) AND
Appointment_ID <> " + id;
Could you help me please? I have tried to solve my problem for about 6 hours.
I have one table and there are 4 columns.
Columns:
Nio ,checked,date,time
I need to get data from the first and second columns, where I have to compare it by date and time.
I wanted to use the operator IN but excel gives me the error
Incorrect syntax near the keyword
Thanks for help.
strQueryQ7DrLeftElox =
"((Select nio,checked
FROM q7_dr_left_elox_incoming_inspection
where date >= '" & sDateFrom & "' AND date <= '" & sDateTo & "') IN (Select nio,checked from q7_dr_left_elox_incoming_inspection WHERE time = '19:55:06'))"
Both select statements query the same table, so simplify it by adding one more condition in the 1st query's WHERE clause:
SELECT nio,checked
FROM q7_dr_left_elox_incoming_inspection
WHERE date >= '" & sDateFrom & "' AND date <= '" & sDateTo & "'
AND time = '19:55:06'
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.
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)
I'm trying to sort customers billings and i need to sort them by different time periods.
What I've been trying is:
(select billing_date from [transaktions]
between '" + start + "' and '" +stop+"' where konto_nr = #konto_nr")
also
(select billing_date from [transaktions] where konto_nr = #konto_nr" between '" + start + "' and '" +stop+"')
start = the starting period of the date
stop = the ending of the period
The error message I'm getting is
Incorrect syntax near the keyword
'between'.
First of all : you should never concatenate together your SQL statement! That's a big big open door for SQL injection attacks....
Second: you need to put your BETWEEN clause into a WHERE clause:
SELECT billing_date
FROM dbo.[transaktions]
WHERE Billing_Date BETWEEN #Start AND #EndDate
AND konto_nr = #konto_nr
Your syntax should be something like
where Transaktions.Billing_Date between StartDate and EndDate
of the obvious respective columns and variable names you are working with. Yes, you referred to the "billing_date" as a selected column, but the WHERE can be testing OTHER columns of criteria so you have to explicitly identify it there too.