I'm trying to perform a query in Laravel 7 that gets the month part of a date field. I tried the following queries
$test = MyModal::selectRaw('month("date_col") as temp')->get();
$test = MyModal::select(DB::Raw('month("date_col") as temp'))->get();
$test = DB::table('my_table')->select('month("date_col") as temp')->get();
All variation of the same query. If I dump the query log, the query is correct for all 3
select month("date_col") as temp from "my_table"
If I run this on my DB I get the result as well. But I keep getting this error in laravel:
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such function: month (SQL: select month("date_col") as temp from "my_table")
Am I doing something wrong? And is there a way to do this query in laravel?
Edit: Sorry forgot to mention this is with Laravel's unit testing
I think the problem in the way you use Month function
you don't set quotations for the column name, it should be:
$test = MyModal::selectRaw('month(date_col) as temp')->get();
it should work
You can do something like this:
MyModal::select(DB::raw("MONTH(date_col) month"))->get();
Thanks to OMR's comment this solution finally worked
$test = MyModel::selectRaw('strftime("%m", "date_col") as temp')->get();
Update: It looks like what I had in the beginning worked fine and the unit test was the only part throwing this error. Since I was working with mysql while the test environment was using sqlite which apparently doesnt like the "month()" function which OMR did point out in his comment. So this solution works in the test but not in live. Couldn't figure out how to change the test environment to use in memory mysql instead of sqlite. If anyone knows how to please add an answer here.
Here is my error:
Error Number: 3037
Invalid GIS data provided to function st_geometryfromtext.
Here is my code:
SELECT ST_Within(ST_GEOMFROMTEXT('POINT(12.971201 077.652038)'),
ST_GEOMFROMTEXT('POLYGON((13.517837674890684 76.453857421875),(13.838079936422464 77.750244140625),(14.517837674890684 79.453857421875),(13.517837674890684 76.453857421875))')) As geoFenceStatus
Note that if you want to wrap each set of coordinates with their own () like you have done in your query, you need to be using mysql 5.7.9+ the earlier versions do not support it. But that is not all, there is another error. mysql polygons have to be closed
SELECT ST_GEOMFROMTEXT('POLYGON((13.517837674890684 76.453857421875,13.838079936422464 77.750244140625,14.517837674890684 79.453857421875,13.517837674890684 76.453857421875,13.517837674890684 76.453857421875))')
And also note that (( and )) is the correct syntax
I am getting oci_execute(): ORA-01843: not a valid month invalid. Why it is giving it?
INSERT INTO DETAIL (PPD_ID, PPD_ID,PPD_ENTRY_DATE, PPD_IGM_ID, PPD_DFM_ID, PPD_DRM_ID, PPD_HEIGHT, PPD_UOM_ID, PPD_FTM_ID, PPD_FRQ_ID, PPD_START_DATE_TIME, PPD_END_DATE_TIME, PPD_STATUS, PPD_STM_ID)
VALUES ('WS00318229',2440900,'29-12-2015',350,1,1,50,46,1,1,'29-12-2015','29-12-2015','PRESCRIBED',6)
When i am using the above query from php to insert record in to oracle database it is giving following error.
oci_execute(): ORA-01843: not a valid month invalid
But when i use the following query it is not giving any error and it is inserting record.How to solve this? This error was already is there but i am not clear about the answers.Any ideas why it is like this.
INSERT INTO DETAIL (PPD_ID, PPD_ID,PPD_ENTRY_DATE, PPD_IGM_ID, PPD_DFM_ID, PPD_DRM_ID, PPD_HEIGHT, PPD_UOM_ID, PPD_FTM_ID, PPD_FRQ_ID, PPD_START_DATE_TIME, PPD_END_DATE_TIME, PPD_STATUS, PPD_STM_ID)
VALUES ('WS00318229',2440900,'29-DEC-2015',350,1,1,50,46,1,1,'29-DEC-2015','29-DEC-2015','PRESCRIBED',6)
You are inserting strings into DATE columns. When you do that, oracle performs implicit conversion based on your NLS settings, specifically, nls_date_format. Evidently when you connect from php script and from some client you use (SQL*Plus, SQL Developer, what have you), your NLS settings differ.
You can check session parameters using following query.
SELECT * FROM nls_session_parameters;
For date presented in format '29-DEC-2015' nls_date_format should be 'DD-MON-YYYY'. If it has some other value, you can change it using following query.
alter session set nls_date_format='DD-MON-YYYY'
Or you can explicitly convert string to date using TO_DATE function and passing format as second parameter, but this will require your application code to be consistent in using only specified date format.
TO_DATE('29-DEC-2015', 'DD-MON-YYYY')
I am developing an application with code igniter and sometimes I got this error when I am calling a view that is loading data from the dabase:
Error Number: 0
SELECT `medidas_ludlum_5min`.* FROM (`medidas_ludlum_5min`) WHERE (FK_ludlum="190.26.88.131" and measurement >= "0.0" and fecha between "2014/04/01 11:49:03" and "2014/05/27 11:49:03")
Filename: C:\xampp-win32-1.7.5-VC9\xampp\htdocs\dashboard\system\database\DB_driver.php
Line Number: 330
Can some one help me to figure it out what is related to error 0? Or what can be happening?
Thanks.
You might want to look at the simple_query function. It seems that the usual query function has a mysql diver bug: http://ellislab.com/forums/viewthread/181751/#860516
If it returns false there might be something else wrong with the query.
Maybe an uneacaped value
I am having a bit of trouble writing a mysqli query that queries between dates.
The column in my mysql database is a datetime column.
The query I have written in php looks like this
$sessions = mysqli_query($db,"SELECT s.idSession, s.idUser,
FROM rempad.Session AS s WHERE s.idUser = 12 AND s.start BETWEEN '2013-04-28' AND '2013-05-28'");
I have tried escaping the dates like \'2013-04-28\'
I have also tried casting as datetime like: DATETIME(s.start) BETWEEN DATETIME('2013-04-28') AND ...
But that doesn't work.
I have tested the SQL syntax in Mysql workbench so I know that the query returns values, but I can't seem to get it right in php.
Any ideas? I can't find any examples online.
Thanks in advance
L
I tried it and it worked as:
query($db,"SELECT s.idSession, s.idUser
FROM rempad.Session s WHERE s.idUser = 12 AND s.start BETWEEN '2013-04-28' AND '2013-05-28'");
Removed a comma and 'as' after table name.