Selecting entries whose `date_field < NOW()` - sql

When I try to run the following query, it returns nothing:
Item::where(\DB::raw('date_field < NOW()'))->get()
The reason for this is, that is null is appended to the generated MySQL query like this:
SELECT * FROM items WHERE date_field < NOW() is null;
Why does the is null part get appended to the above query?

This is a known issue in Laravel and has been reported on their GitHub page. Use whereRaw() instead and pass a string:
Item::whereRaw('date_field < NOW()')->get()

No idea why the not null part gets appended. But I found a workaround.
Try this
Item::whereNotNull(\DB::raw('date_field < NOW()'))->get()
Of course, you may use built-in features like Carbon
Item::where('date_field', '<', Carbon\Carbon::now())->get()

Use it this way..
Item::where('date_field','<','NOW()')->get()

SELECT * FROM items WHERE date_field < GETDATE();

Related

Big Query Concat String in FROM Clause

I have multiple tables like as follows in BigQuery:
PROJECT_NAME.DATA_SET_NAME.TABLENAME0
PROJECT_NAME.DATA_SET_NAME.TABLENAME1
PROJECT_NAME.DATA_SET_NAME.TABLENAME2
PROJECT_NAME.DATA_SET_NAME.TABLENAME3
PROJECT_NAME.DATA_SET_NAME.TABLENAME4
...
I want to empty some of these tables via a loop but don't know how to call the CONCAT in FROM clause:
DECLARE count INT64 DEFAULT 0;
WHILE count < 1000 Do
DELETE FROM CONCAT('PROJECT_NAME.DATA_SET_NAME.TABLENAME' , count ) WHERE TRUE;
SET count = count + 1;
END WHILE
But it's not working, it says I cannot use CONCAT in FROM part.
Anyone knows how should I do it?
Thanks
This is not currently possible in BigQuery, unless you script it outside BigQuery.
There's an open feature request, that you should subscribe to - to indicate interest and follow any new developments:
https://issuetracker.google.com/issues/142531516

# is not supporting to RIak Ts

while making query to Riak TS, my email contaning # symbol so it giving some problem like
SQL Lexer error <<"Unexpected token '#'.">>
then How we Resolve this problem.
Use below query.
SELECT SUM(steps),
registrationDate
FROM steps
WHERE
START >= 1482085800000
AND
START <= 1489775400000
AND userName = <<"hussain.shahzad250#gmail.com">>;
specially #Saranjith
Query will be now as given below
select start,end,steps from steps_record where start >= 1472495400000 and start <= 1490121000000 and userName = '<<"apolloprod120#gmail.com">>';
anyways Thanks #Saranjith, i tried with your given syntax in 2-3 ways after that i got this solved.

RoR Search conditions comparing two columns

What I basically want to do is the following:
a = Assignment.all(:conditions => { :end_date < :start_date })
I only want to select those records on which the end_date is before the start_date.
I actually don't want ending up writing a
for each, push to array if end_date is earlier than start_date.
How can I achieve this in a pretty 'Railsy'-way?
Thank you in advance.
Edit:
I think the problem is comparing the values of both columns. (Allmost) every query is comparing a cell-value to an input-value.
This is a shot in the dark, but maybe one in the right direction ?
Haven't figured out a solution.
It's been a while here but nevertheless, you can just use the ArelTable method:
t = Assignment.arel_table
Assignment.where(t[:end_date].lt(t[:start_date]))
The condition predicates documentation: http://www.rubydoc.info/github/rails/arel/Arel/Predications
The ArelTable documentation: http://www.rubydoc.info/github/rails/arel/Arel/Table
And a good guide: http://jpospisil.com/2014/06/16/the-definitive-guide-to-arel-the-sql-manager-for-ruby.html
a = Assignment.all(:conditions => ["end_date < ?", :start_date ])
Check this: http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-all
I am assuming that you are using ActiveRecord
try
a = Assignment.where('regioassignments.end_date < regioassignments.start_date')
use the tablename followed by the EXACT COLUMNNAMES in the database, since ActiveRecord recognizes this and uses this as SQL directly.
That means the columnname for end_date is probably assignment_expected_end_date from what I figured from one of your comments.
adapted from this answer

Alternative to relying on execution order of conditions in SQL 'where' clause

In languages such as JavaScript you can have 2 conditional statements and "protect" the second one with the first one. For instance:
if( scarryObject != null && scarryObject.scarryMethod() ) { ... }
// if scarryObject is null scarryMethod will not be called
I thought I would achieve the same in SQL like so:
where int_date > 19500101
and month(CONVERT(smalldatetime, ... int_date))
The problem here is that if int_date is some "bad" value like -1, 0, 1 the conversion will fail and the sp will stop with an error. I thought the first check int_date > 19500101 would get evaluated first and if false the second condition would be skipped.
It seems like it does not work like this... or? Is there any other way of doing this?
Thanks!
Your query is syntactically not correct, as the clausemonth(CONVERT.... is not a condition.
Let's assume you want to compare with a certain number, a possible way of expressing what you want would be
SELECT *
FROM myTable
WHERE case
when int_date > 19500101
then -1
else month(CONVERT(smalldatetime, ... int_date))
end = #YourMonth
You would 'protect' the evaluation of the 'month' and not the condition.
You could try splitting the query into two. Here is the concept:
SELECT *
FROM (
SELECT *
FROM myTable
WHERE int_date > 19500101
) t
WHERE month(CONVERT(smalldatetime, ... t.int_date))

Compare issue in yii

In almost in every wiki simple things are explained. I'm stuck in yii's CDbcriteria compare issue.
Only the exact "equal to" match is explained
for:
select * from users where status ='active'
This comparison is explained:
$criteria->compare('status','active');
But I can't find an example script which describes it with an operator based search. Like not equal to for the following query:
select * from users where status !='active'
How can I do this?
try some thing like this
$criteria->condition = " status<>'active'";
$criteria->compare('status',$this->status,true);
$criteria->addNotInCondition('status', array('active'));
if count(array('active')) === 1 sql will status != 'active', else status NOT IN ('active', 'smth else')
Try
$criteria->addCondition("NOT status = 'active'");
I had the below 3 conditions and it works like a charm:
$criteria->condition='employee_id<>:employee_id AND vehicle_id=:vehicle_id AND checked_in_on IS NULL';
$criteria->params=array(':employee_id'=>Yii::app()->session['activity']->employee_id,':vehicle_id'=>$model->id);
$checkedOutVehicleBySomeoneElse = Activity::model()->find($criteria);