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
Related
I have searched through all the posts I could find regarding the "Cannot set the command text for dataset" error I am getting, and everything that users suggest, still results in this error for me.
I am working in SSRS 2008R2, and I am connecting via ODBC to a MYSQL connection.
="SELECT case when tranHour between '00' and '03'
then DATE_format(DATE_SUB(trandate,INTERVAL 1 DAY), '%Y-%m-%d')
else DATE_format(trandate, '%Y-%m-%d')
end as WorkDay,
case when tranHour between '03' and '15' then 'AM'
else 'PM'
end as Shift,
'Station1' AS Station,
count(*) as 'Scans',
ROUND(SUM(elapsed_seconds)/60/60,2) as 'Hours',
ROUND(count(*)/(SUM(elapsed_seconds)/60/60),2) as 'Scans/Hour',
season
FROM data_lite.master_lite_134_301
where SEASON IN ('" + join(Parameters!ReportParameter1.Value,"','") + "')
group by WorkDay, Shift
ORDER BY DATE_format(WorkDay, '%Y-%m-%d') DESC"
Every time I get this error...
An error occurred during local report processing.
Cannot set the command text for dataset 'AMPM'
Error during processing of the CommandText expression of dataset 'AMPM'
If I replace the Parameter with an actual value, it runs fine.
So with where SEASON = '15F' , it runs,
with where SEASON IN ('" + join(Parameters!ReportParameter1.Value,"','") + "') , it gets this error
I had the similar issue. I had everything correct my tables, fields and query as well. What resolved my issue was checking "multiple values" box in parameters.
Go to your parameters --> double click on it --> If you have default value set it in default value tab --> In general tab check multiple values option.
Build your report again/ delete the .rdl.data file from the folder and then check your report it should work perfectly fine.
change your CASE to
SELECT case when ( tranHour between '03' and '15') then 'AM'
else 'PM'
end CASE as Shift;
so
="SELECT case when tranHour between '00' and '03'
then DATE_format(DATE_SUB(trandate,INTERVAL 1 DAY), '%Y-%m-%d')
else DATE_format(trandate, '%Y-%m-%d')
end as WorkDay,
case when ( tranHour between '03' and '15') then 'AM'
else 'PM'
end CASE as Shift,
'Station1' AS Station,
count(*) as 'Scans',
ROUND(SUM(elapsed_seconds)/60/60,2) as 'Hours',
ROUND(count(*)/(SUM(elapsed_seconds)/60/60),2) as 'Scans/Hour',
season
FROM data_lite.master_lite_134_301
where SEASON IN ('" + join(Parameters!ReportParameter1.Value,"','") + "')
group by WorkDay, Shift
ORDER BY DATE_format(WorkDay, '%Y-%m-%d') DESC"
You have to map the Query parameter with the corresponding Report parameter.
In Dataset properties go to Parameters tab, map the name of the query parameter with name of the report parameter.
Now in your query you can use the parameter in this way - only if it is a multivalue parameter.
SELECT case when tranHour between '00' and '03'
then DATE_format(DATE_SUB(trandate,INTERVAL 1 DAY), '%Y-%m-%d')
else DATE_format(trandate, '%Y-%m-%d')
end as WorkDay,
case when tranHour between '03' and '15' then 'AM'
else 'PM'
end as Shift,
'Station1' AS Station,
count(*) as 'Scans',
ROUND(SUM(elapsed_seconds)/60/60,2) as 'Hours',
ROUND(count(*)/(SUM(elapsed_seconds)/60/60),2) as 'Scans/Hour',
season
FROM data_lite.master_lite_134_301
where SEASON IN (#QueryYearParameter)
group by WorkDay, Shift
ORDER BY DATE_format(WorkDay, '%Y-%m-%d') DESC
Note Parameter Name property and the parameter I use in the query is
the same.
Let me know if this helps you.
SSRS is funny about its spaces at the end of lines in the expressions. I had the same issue and SSRS was combining the last word of the line with the first word of the next line. Just add a space in at the end of every line.
OR
Wrap your JOIN func in CSTR(). so... CSTR(join(Parameters!ReportParameter1.Value,"','"))
Hi I need to do something like this as a part of a bigger script:
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('(SUM(CASE WHEN ColumnA = "' ,ColumnA, ' "THEN 1 ELSE 0 end))/(select total
from hd_totals where ColumnA = "' ,ColumnA, ' ") AS "' ,ColumnA, ' "'))
FROM table inner join.....
The problem is that I can't do the / operation because it gives an error, even if the select output is a single value.
I don't even know how to use aliases or to store in a variable because it's a rather complex (to me) environment I'm creating here...
What about this : use CAST to have DECIMAL type, and use coalesce to prevent null values?
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('COALESCE(CAST((SUM(CASE WHEN ColumnA = "' ,ColumnA, ' "THEN 1 ELSE 0 end)) AS DECIMAL(10,4)),0)/COALESCE(CAST((select total
from hd_totals where ColumnA = "' ,ColumnA, ' ") AS DECIMAL(10,4)),0.01) AS "' ,ColumnA, ' "'))
FROM table inner join.....
Note : there is an arbitrary constant 0.01 to prevent dividing by 0.
More important note : If I was you I would also add a CASE WHEN to check that you don't divide by 0.
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've written following code:
Dim date1 As Date
Dim date2 As Date
date1 = Convert.ToDateTime(DatePickerFromDate.Text)
date2 = Convert.ToDateTime(DatePickerToDate.Text)
Dim cnd As New OleDbCommand("SELECT * FROM Sales WHERE Invoice_Date BETWEEN " + date1 + " AND " + date2 + "", om)
om.Open()
Dim da As OleDbDataReader = cnd.ExecuteReader
While da.Read()
ComboBox1.Items.Add(da(0))
End While
da.Close()
om.Close()
I want to retrieve data between two dates that are been taken from two datepickers.
I tried BETWEEN, also i tried >= =< but result was empty though database contains data. Please help where I'm getting wrong
Your code is probably generating an error. When doing this type of querying, you should store the query string after substitution and print it out. You seem to be missing delimiters around the dates. So this may work in your specific case.
New OleDbCommand("SELECT * FROM Sales WHERE Invoice_Date BETWEEN '" + date1 + "' AND '" + date2 + "'", om)
However, you then need to be careful about the format of the dates. The application layer and the database might use different formats. If you are substituting directly into the query string, then use the format YYYY-MM-DD -- it is the ISO standard date format and generally understood.
Even better is to learn how to parameterize queries so you can actually pass in the date values as date parameters.
If you're using MS Access, this should be the syntax...
SELECT * FROM Sales WHERE Invoice_Date>=#" + date1 + "# and Invoice_Date<=#" + date2 + "#"
If you're using MS SQL Server or MySQL, then do something like this...
SELECT * FROM Sales WHERE Invoice_Date>='" + date1 + "' and Invoice_Date<='" + date2 + "'"
My problem is likely all about date formatting in a SELECT.
In an asp file I open an ADO Recordset wanting to retrieve rows of a MS SQL table that fall between date1 (08/15/2013) and date2 (08/22/2013) (i.e., the previous 7 days from today's date.)
The SELECT does retrieve the appropriate 2013 rows but also retrieves rows going back to 08/15/2012.
Here is the SELECT:
oRS.Source = "SELECT * FROM aTable WHERE entry_Date BETWEEN '" & resultLowerDate & "' AND '" & resultCurrentDate & "' AND entry_Status <> 'INACTIVE'"
resultLowerDate = 08/15/2013 and resultCurrentDate = 08/22/2013.
The table is set up as follows with resultCurrentDate = "08/22/2013":
entry_Status entry_Date (varchar) LastName FirstName SELECT Result
INITIAL 08/15/2012 Smith Jim YES
INACTIVE 08/21/2012 Green Tom no
INITIAL 08/22/2013 Jones Mary yes
FOLLOWUP 08/22/2013 Jones Mary yes
FOLLOWUP 08/22/2013 Brown Sally yes
FOLLOWUP 08/22/2013 Smith Jim yes
Any thoughts as to why the INITIAL 08/15/2012 row gets selected along with the other rows that meet the SELECT query?
STOP STORING DATES IN VARCHAR COLUMNS! And STOP CONCATENATING STRINGS, USE PROPER PARAMETERS.
Sorry to yell, but we are getting multiple questions a day where people use the wrong data type for some unknown and probably silly reason, and these are the problems it leads to.
The problem here is that you are comparing strings. Try:
"... WHERE CONVERT(DATETIME, entry_date, 101)" & _
" >= CONVERT(DATETIME, '" & resultLowerDate & "', 101)" & _
" AND CONVERT(DATETIME, entry_date, 101)" & _
" < DATEADD(DAY, 1, CONVERT(DATETIME, '" & resultCurrentDate & "', 101))"
Or better yet, set resultLowerDate and resultUpperDate to YYYYMMDD format, then you can say:
"... WHERE CONVERT(DATETIME, entry_date, 101) >= '" & resultLowerDate & "'" & _
" AND CONVERT(DATETIME, entry_date, 101) < DATEADD(DAY, 1, '" & resultCurrentDate & "'"
Note that I use an open-ended range (>= and <) instead of BETWEEN, just in case some time slips into your VARCHAR column.
Also note that this query could fail because garbage got into your column. Which it can, because you chose the wrong data type. My real suggestion is to fix the table and use a DATE or DATETIME column.
The fact that your entry_Date column is a varchar field and not an actual date is the problem. If you cast it to a datetime during the select, you'll get the results you expect.
select *
from aTable
where cast(entry_Date as datetime) between '08/15/2013' and '08/22/2013'
Sql Fiddle link