The following query is working perfectly :
SELECT *
FROM MsUser
WHERE absensi_tanggal = DATE_FORMAT( STR_TO_DATE( "' .$query_array['datepicker'] . '", \'%m/%d/%Y\' ) , \'%Y-%m-%d\' )
However, this query is not working. I should miss something there :
SELECT *
FROM MsUser
WHERE absensi_tanggal BETWEEN DATE_FORMAT( STR_TO_DATE( "' .$query_array['datepicker1'] . '", \'%m/%d/%Y\' ) , \'%Y-%m-%d\' )
AND DATE_FORMAT( STR_TO_DATE( "' .$query_array['datepicker2'] . '", \'%m/%d/%Y\' ) , \'%Y-%m-%d\' )
The query above always return 0 result.
Any help is appreciated, Thanks :D
Since STR_TO_DATE returns a proper DATE type value that can be used with BETWEEN, have you tried simply:
SELECT *
FROM MsUser
WHERE absensi_tanggal
BETWEEN
STR_TO_DATE( "' .$query_array['datepicker1'] . '", \'%m/%d/%Y\' )
AND
STR_TO_DATE( "' .$query_array['datepicker2'] . '", \'%m/%d/%Y\' )
My guess would be that MySQL is choking on converting front a string to a date and then back to a string and then back again to a date to make the BETWEEN work.
Also, try this to make it a bit tidier:
"
SELECT *
FROM MsUser
WHERE absensi_tanggal
BETWEEN
STR_TO_DATE('{$query_array['datepicker1']}','%m/%d/%Y')
AND
STR_TO_DATE('{$query_array['datepicker2']}','%m/%d/%Y');
"
And just in case you aren't confirming that the value of datepicker1 is earlier than datepicker2, you can run the query like this to be sure (though I advise only for testing):
SELECT *
FROM MsUser
WHERE
(
absensi_tanggal
BETWEEN
STR_TO_DATE('{$query_array['datepicker1']}','%m/%d/%Y')
AND
STR_TO_DATE('{$query_array['datepicker2']}','%m/%d/%Y')
) OR (
absensi_tanggal
BETWEEN
STR_TO_DATE('{$query_array['datepicker2']}','%m/%d/%Y')
AND
STR_TO_DATE('{$query_array['datepicker1']}','%m/%d/%Y')
);
"
Related
I am getting the error "Incorrect parameters in the call to native function 'CONCAT': on the query below:
SELECT
*,
GROUP_CONCAT(
CASE
WHEN `REASONORINSTRUCTIONCODE` = 'R'
THEN CONCAT(
"name-",
`USERWHOENTEREDTHISLINE`,
",reason-",
RTRIM(
`REASONSORSHIPPINGINSTRUCTIONS`
)
END,
", "
)
) AS reason,
GROUP_CONCAT(
CASE
WHEN [ `REASONORINSTRUCTIONCODE` ] = 'S'
THEN CONCAT(
"name-",
`USERWHOENTEREDTHISLINE`,
",shipping instruction-",
RTRIM(
`REASONSORSHIPPINGINSTRUCTIONS`
)
END,
", "
)
) AS shipping instruction
FROM
TABLE
GROUP BY `PICKUP_NO`
You have several issues with your query. First, you're not closing your CONCAT with an end ), next your AS shipping instruction cannot contain a space. Next, you have [REASONORINSTRUCTIONCODE], remove the []
Take a look at the formatted query below:
SELECT
*,
GROUP_CONCAT(
CASE
WHEN `REASONORINSTRUCTIONCODE` = 'R'
THEN CONCAT(
"name-",
`USERWHOENTEREDTHISLINE`,
",reason-",
RTRIM(
`REASONSORSHIPPINGINSTRUCTIONS`
))
END,
", "
)
AS reason,
GROUP_CONCAT(
CASE
WHEN `REASONORINSTRUCTIONCODE` = 'S'
THEN CONCAT(
"name-",
`USERWHOENTEREDTHISLINE`,
",shipping instruction-",
RTRIM(
`REASONSORSHIPPINGINSTRUCTIONS`
))
END,
", "
)
AS shipping_instruction
FROM
`TABLE`
GROUP BY `PICKUP_NO`
I am looking around for a solution but have not succeeded so far.
My MySQL table ($people_table) holds birthdays in a column "birthdatetr" of type "date", and the data is stored in the format "YYYY-MM-DD" (example: 1789-04-02)
Depending on the current date, I specify a range of interest.
I am calculating the current week with:
$lastmonday = date('m-d', strtotime('last monday'));
$nextsunday = date('m-d', strtotime('next sunday'));
I would like to Select all rows, where the birthday is within this range. (The application is website with genealogical data, which displays all birthdays that happened in the same week so and so many years ago)
I managed to get it somehow done by calculating the day of the year:
$lastmonday_doy = date('z', strtotime('last monday'));
$nextsunday_doy = date('z', strtotime('next sunday'));
SELECT * FROM $people_table WHERE DAYOFYEAR(birthdatetr) BETWEEN $lastmonday_doy AND $nextsunday_doy;
The idea was to bypass any trouble with the month wrap. But this still does not work when the year wraps. Also, I was concerned whether this is very expensive, because the table may hold >5000 rows in the future.
Finally, the DAYOFYEAR() approach does not take leap years into account.
At the moment, there is another implementation roughly working, but I don't understand it and I cannot change it for that reason. It looks very complex:
$query = "SELECT birthdatetr, YEAR(birthdatetr) AS BirthYear, birthdatetr + INTERVAL YEAR('" . $datetouse . "') - YEAR( birthdatetr ) + ( (birthdatetr + INTERVAL YEAR('" . $datetouse . "') - YEAR( birthdatetr ) YEAR) < '" . $datetouse . "') YEAR as nextbirthday
FROM $people_table
WHERE DATEDIFF( birthdatetr + INTERVAL YEAR('" . $datetouse . "') - YEAR( birthdatetr ) + ( ( birthdatetr + INTERVAL YEAR('" . $datetouse . "') - YEAR(birthdatetr) YEAR) < '" . $datetouse . "') YEAR, '" . $datetouse . "') <= $futuredays ORDER BY nextbirthday, birthdatetr";
Here, the current date is $datetouse, and the $futuredays specifies the length of the range. (in this example, $futureday=7; )
Is there a light-weight way to SELECT desired data?
Thanks!
For the one week requirement could be simpler to replace the
WHERE DAYOFYEAR(birthdatetr) BETWEEN $lastmonday_doy AND $nextsunday_doy
with
WHERE DAYOFYEAR(birthdatetr) in (7 day numbers for the 7 weekdays)
and pass 7 parameters to the query (monday, tuesday... sunday)
Ok, here is the final SQL statement. Thanks to StanislavL:
SELECT birthdatetr as datedisplay FROM tng_people
WHERE DATE_FORMAT(birthdatetr, '%m-%d') IN ('05-12', '05-13', '05-14', '05-15', '05-16', '05-17', '05-18')
UNION ALL
SELECT deathdatetr as datedisplay FROM tng_people
WHERE DATE_FORMAT(deathdatetr, '%m-%d') IN ('05-12', '05-13', '05-14', '05-15', '05-16', '05-17', '05-18')
UNION ALL
SELECT marrdatetr as datedisplay FROM tng_families
WHERE DATE_FORMAT(marrdatetr, '%m-%d') IN ('05-12', '05-13', '05-14', '05-15', '05-16', '05-17', '05-18')
ORDER BY DAYOFWEEK(datedisplay)
I'm developing an application using vb.net2008 with database as msaccess. I've designed a table in database having columns IN_TIME and OUT_TIME in format HH:MM:SS . I want a result as TOT_TIME which will display difference between these two columns. I've written a query for this, now i'm facing a problem that i dont want to display result if IN_TIME is less than 8:00:00 AM else display result. I have used CASE WHEN but it didnt worked, help me out.
I've tried this so far
SELECT *
(CASE WHEN LATE_LIMIT > '" + date3 + "'
THEN ROUND(([PM_OUT]-[OVERIME_LIMIT]),2)
ELSE 'N' )AS OverTime
FROM DTR_REC
You are missing a comma and an END case:
SELECT *
,(CASE WHEN LATE_LIMIT > '" + date3 + "'
THEN ROUND(([PM_OUT]-[OVERIME_LIMIT]),2)
ELSE 'N' END)AS OverTime
FROM DTR_REC
But I'm not sure if Access supports CASE statements. If not does this work:
SELECT *
,IIF(LATE_LIMIT > '" + date3 + "', ROUND(([PM_OUT]-[OVERIME_LIMIT]),2),'N') AS OverTime
FROM DTR_REC
Could this be done with one query?
table day:
dID
enable
holiday
fewMore
table segment:
d_ID //foregn key to day->dID
startTime
endTime
fewMore
query:
SELECT dID, enabled,holiday,
TIME_TO_SEC(dHaurs) as wsTimeHHmm,
DATE_FORMAT(dDate, '%d') as dateForComparing,
DATE_FORMAT(start, '%H:%i') as startTime,
DATE_FORMAT(end, '%H:%i') as endTime
FROM day d
LEFT JOIN segment ws
ON ws.d_ID = d.dID
WHERE dDate BETWEEN '" . $startDate . "' AND '" . $endDate . "'
ORDER by dateForComparing
result I am getting:
dID:10, start time:08:00, end time:09:00, holiday:0, enabled:1
dID:10, start time:09:00, end time:10:00, holiday:0, enabled:1
dID:11, start time:08:00, end time:10:00, holiday:1, enabled:1
but I would like to group all segments with same d_ID.
dID:10, array((start time:08:00, end time:09:00),(start time:09:00, end time:10:00)), holiday:0, enabled:1
I tried with GROUP_CONCAT(wsID,start, end) ... GROUP BY dID but I am not sure is that right way and also I am not able to use function DATE_FORMAT(start, '%H:%i') as startTime
could this be done with mysql only?
UPDATE:
right now, I am creating array from result:
$returnData [] =array(array(dID =>"dID",
endTime => $end_time,
startTime=> $start_time,...
),
array( enabled=> $enabled,
holiday =>$holiday
)
);
echo json_encode($returnData);
You can't return arrays in MySQL, but you could manipulate the output like this:
dID:10, start time:08:00, end time:10:00, holiday:0, enabled:1
I have the query below which works great.
SELECT `Despatch`.`DespatchNumber` , `Despatch`.`Product` ,
`Stock`.`ProductNumber` , `Stock`.`Description` ,
`Stock`.`ProductGroup` , `Stock`.`Size` , `Stock`.`IPICODE` , `Stock`.`Fit` ,
`Stock`.`62`
FROM Stock , Despatch
WHERE `Despatch`.`DespatchNumber` = '" . $Despatch . "'
AND `Despatch`.`Product` = `Stock`.`ProductCode`
My problem is i want to expand it that im using the value of $Despatch to search another column in the same table using an OR function. So the query will look into Despatch Number and if it does not find one matching then it looks into order number as below. It doesnt work and my mysql server just hangs trying to run this query.
SELECT `Despatch`.`DespatchNumber` , `Despatch`.`Product` ,
`Stock`.`ProductNumber` , `Stock`.`Description` , `Stock`.`ProductGroup` , `Stock`.`Size` , `Stock`.`IPICODE` , `Stock`.`Fit` , `Stock`.`62`
FROM Stock , Despatch
WHERE `Despatch`.`DespatchNumber` = '" . $Despatch . "'
OR `Despatch`.`OrderNumber` = '" . $Despatch . "'
AND `Despatch`.`Product` = `Stock`.`ProductCode`
You need parentheses around your OR clause:
WHERE (`Despatch`.`DespatchNumber` = '" . $Despatch . "'
OR `Despatch`.`OrderNumber` = '" . $Despatch . "')
AND `Despatch`.`Product` = `Stock`.`ProductCode`
I'd also very strongly recommend you to explicitly use the JOIN keyword when you perform joins. Not only is this best practice, but it also would have prevented the error occuring in this case.
SELECT -- ...columns...
FROM Stock AS S
JOIN Despatch AS D
ON D.Product = S.ProductCode
WHERE D.DespatchNumber = 'DN001234'
OR D.OrderNumber = 'DN001234'