Passing date parameters to Oracle Query in SSRS - sql

I have an SSRS report that uses an Oracle datasource. I have a query;
select * from myTable t where
t.date between Date '2009-08-01' and Date '2009-08-04' + 1
This query works, both in Oracle and from SSRS. It gets all rows from myTable when the t.date column is between 08/01/2009 and 08/04/2009. The t.date column is of type Date()
Now, I want the dates to be parameters. So I changed the query to:
select * from myTable t where
t.date between Date :myDate and Date :myDate + 1
When I run this query in SSRS, it prompts me to enter a value for :myDate.
I tried 2009-08-01, '2009-08-01' both results in an oracle sql processing error: " Missing expression".
Any ideas?

If you insist on having one parameter (:myDate), using a with clause can make the process a bit cleaner. I'm not familar with the syntax you have in your example (i.e. the 'Date' like casting in the query), below is a SQL only implementation.
with
select TRUNC(TO_DATE(:mydate, 'yyyy-mm-dd')) p_date from dual as parameters
select t.*
from
myTable t,
parameters
where
trunc(t.date) between parameters.p_date and parameters.p_date + 1

Just because you have used :myDate twice doesn't mean that they are the same. They are placeholders, and since you have two placeholders you need two values.

This worked for me using SSRS:
SELECT t.*
FROM myTable t
WHERE t.date between :myDate AND to_date(:myDate + 1)

Have you tried making sure the parameter is a DateTime type (it is string by default) in the Report Parameters menu?

I had a similar problem with a much larger query, and I found that simply searching and replacing e.g. :mydate with TO_DATE(:mydate) was sufficient to solve the problem - the same date was then passed to every instance of TO_DATE(:mydate) in the query.
If you want a date range passed into the query, I would give the parameters different names e.g. :StartDate and :EndDate in the SQL and have separate parameters #StartDate and #EndDate in the report. This makes the SQL much clearer.

SSRS newer versions :parameter is not working, you need to use ? as below
below first two parameters date and third one text
select * from tasks where FINISH_DATE between ? and ?+1 and t2.TYPE = ?

Related

Getting not valid month in Oracle sql

I have a table called Transactions that has a column called trans_date. I am just trying to do a simple query in the SQL*Plus command window
The query is
SELECT * FROM transactions WHERE
trans_date BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
When I run this query I get not valid month and there is a little * under trans_date. Most of what I have read suggests the query is right but I am not sure what the problem is. The data type is varchar2(20).
Since trans_date is a varchar and you're trying to query whether it's between two dates, you need to convert it to a date too. Assuming it has the same format as the literals in your query:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyy') BETWEEN
to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
Seems like problem is columns data type, Try convert it to date,
SELECT * FROM transactions
WHERE to_date(trans_date,'mm/dd/yyyy') BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
You need to convert trans_date to a date. However, you can use date constants for the comparisons:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyyy') BETWEEN DATE '2021-09-11' AND DATE '2021-09-12';
You should fix your data model so the dates are stored correctly, using Oracle's built-in data type.

SQL number Wildcard issue

I'm trying to add a wildcard to my date select query so i only pull the day not time. I.e. 2021-03-11 17:54:30.123. I thought a number could be substituted for a #.
select AID, LocalCoAltIn,LocalCoAltOut,EventTime
from EXCDS.dbo.WKS_LOG_VIEW
where EventTime like '2021-03-11 ##:##:##:###';
My query is returning no values even though they are in the table. Thanks.
No! Don't use strings! One method is to convert to a date:
select AID, LocalCoAltIn,LocalCoAltOut,EventTime
from EXCDS.dbo.WKS_LOG_VIEW
where convert(date, EventTime) = '2021-03-11';
Another method is to use a range:
where EventTime >= '2021-03-11' and
EventTime < '2021-03-12'
The LIKE operator in most flavors of SQL only support _ and * wildcards (matching any one single, or multiple characters). Gordon has given you a better approach, but if you wanted to fix your current query on SQL Server you could try:
SELECT AID, LocalCoAltIn, LocalCoAltOut, EventTime
FROM EXCDS.dbo.WKS_LOG_VIEW
WHERE EventTime LIKE '2021-03-11 [0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9][0-9]';
SQL Server extended the LIKE operator to accept a few extra things, such as character classes. Here [0-9] inside LIKE would match any single digit.
Not sure that like operator would work for date as you want, but you still have few options.
Use DATEPART function to retrieve year\month\etc and compare it with exact value that you need
select AID, LocalCoAltIn,LocalCoAltOut,EventTime from EXCDS.dbo.WKS_LOG_VIEW where DATEPART(year,EventTime) = 2021 AND DATEPART(month,EventTime) = 3 AND DATEPART(day,EventTime = 11);
Or use Gordon Linoff suggestion if you dont care about exact date part and only need to compare entire date without time

Sql Server Table date query showing incorrect result

I have a Sql server table which contains below Date values(4th october)
Now Below query is not showing any result
select
*
from [dbo].[TB_AUDIT] TBA
where TBA.ActionDate >= '10/01/2018' and TBA.ActionDate <= '10/04/2018' which is not correct.
But If I write
select
*
from [dbo].[TB_AUDIT] TBA
where TBA.ActionDate >= '10/01/2018' and TBA.ActionDate <= '10/05/2018' it is returning me all results.
What I am doing wrong.
There are two problems with this query. The first, is that it's using a localized string. To me, it looks like it's asking for rows between January and April. The unambiguous date format is YYYYMMDD. YYYY-MM-DD by itself may not work in SQL server as it's still affected by the language. The ODBC date literal, {d'YYYY-MM-DD'} also works unambiguously.
Second, the date parameters have no time which defaults to 00:00. The stored dates though have a time element which means they are outside the search range, even if the date parameter was recognized.
The query should change to :
select
*
from [dbo].[TB_AUDIT] TBA
where
cast(TBA.ActionDate as date) between '20181001' and '20181004'
or
cast(TBA.ActionDate as date) between {d'2018-10-01'} and {d'2018-10-04'}
Normally, applying a function to a field prevents the server from using any indexes. SQL Server is smart enough though to convert this to a query that covers the entire date, essentially similar to
where
TBA.ActionDate >='2018:10:01T00:00' and TBA.ActionDate <'2018-10-05T00:00:00'
When you don't specify a time component for a DATETIME, SQL Server defaults it to midnight. So in your first query, you're asking for all results <='2018-10-04T00:00:00.000'. All of the data points in your table are greater than '2018-10-04T00:00:00.000', so nothing is returned.
You want
TBA.ActionDate >= '2018-10-01T00:00:00.000' and TBA.ActionDate < '2018-10-05T00:00:00.000'`
Use properly formatted dates!
select *
from [dbo].[TB_AUDIT] TBA
where TBA.ActionDate >= '2018-10-01' and TBA.ActionDate <= '2018-10-04'
YYYY-MM-DD isn't just a good idea. It is the ISO standard for date formats, recognized by most databases.
when you just filter by the date, it is with regard to the time as per the standard.

Select data in date format

I have a query in which I want to select data from a column where the data is a date. The problem is that the data is a mix of text and dates.
This bit of SQL only returns the longest text field:
SELECT MAX(field_value)
Where the date does occur, it is always in the format xx/xx/xxxx
I'm trying to select the most recent date.
I'm using MS SQL.
Can anyone help?
Try this using ISDATE and CONVERT:
SELECT MAX(CONVERT(DateTime, MaybeDate))
FROM (
SELECT MaybeDate
FROM MyTable
WHERE ISDATE(MaybeDate) = 1) T
You could also use MAX(CAST(MaybeDate AS DateTime)). I got in the (maybe bad?) habit of using CONVERT years ago and have stuck with it.
To do this without a conversion error:
select max(case when isdate(col) = 1 then cast(col as date) end) -- or use convert()
from . . .
The SQL statement does not specify the order of operations. So, even including a where clause in a subquery will not guarantee that only dates get converted. In fact, the SQL Server optimizer is "smart" enough to do the conversion when the data is brought in and then do the filtering afterwards.
The only operation that guarantees sequencing of operations is the case statement, and there are even exceptions to that.
Another solution would be using PATINDEX in WHERE clause.
SELECT PATINDEX('[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]', field_value)
Problem with this approach is you really are not sure if something is date (e.g. 99/99/9999 is not date).
And problem with IS_DATE is it depends on configuration (e.g. DATEFORMAT).
So, use an appropriate option.

Oracle query not displaying date properly

In Oracle, there is a column(dateColumn) in a table with column type = DATE and the value for a particular record is showing as '10/03/2010' when I do a select * from table.
Now, when I do:
SELECT *
FROM table
WHERE dateColumn < '01-JAN-11'
Nothing shows up. When I do:
SELECT *
FROM table
WHERE dateColumn > '01-JAN-11'
The record shows up.
Why is this behaving this way? "10/03/2010" is 10th MArch 2010 so clearly that is < 01 Jan 2011.
There is no definitive date format -- it can be different by region or even business. That's the reality without even considering SQL...
In Oracle, the means of evaluating a string as a date requires you to use the TO_DATE function:
SELECT *
FROM table
WHERE dateColumn > TO_DATE('01-JAN-11', 'DD-MON-YY')
You need to supply the format mask to instruct Oracle how to interpret the date you're supplying it.
The function is often different on other databases, but the experience is otherwise the same. Most databases accept 'YYYY-MM-DD' for implicit conversion of a string into a date. Explicit conversion is when you use a function, like TO_DATE to explicitly change the data type.
Try to use to_date:
select * from table where dateColumn < to_date('01-JAN-11', 'dd-MON-YY')