SQL query is not executing.. -ORACLE take long time and no results - sql

select count(*)
from siebel.s_srv_req
where trunc(created)>='25-Sep-2017'
and trunc(created)<='25-Sep-2017'
and X_MBL_AREA_LIC is not null
and sr_cat_type_cd <> 'Trouble Ticket';
I'm trying to execute above sql in my oracle db and its not giving results.
If i execute this as below it works fine. Created is an indexed column.
select count(*)
from siebel.s_srv_req
where trunc(created)>='25-Sep-2017'
and trunc(created)<='25-Sep-2017'
when i see the execution plan it gives below error
What should i need to be done to work this fine. This is working fine in my test environment

If you use TRUNC, the index may not be used by optimizer. If you are sure that you don't have data in the table for dates beyond '25-Sep-2017',
you may use
WHERE created >= DATE '2017-09-25'
Otherwise use,
WHERE created >= DATE '2017-09-25' AND created < DATE '2017-09-26'
or if you are using SYSDATE,
created >= TRUNC(SYSDATE) and created < TRUNC(SYSDATE) + 1

Please check if X_MBL_AREA_LIC and sr_cat_type_cd fields are indexed.
Also if you can change the values of sr_cat_type_cd column to integer values it could work faster. Now you make a string comparison for every record.

Related

SQL Count Where on a specific date... but the result is 0

Hi it is NOT my first time the i use this query:
SELECT COUNT(*) from dbo.SchDetail WHERE dbSchDate = '08-06-2020'
Query is working but he counts everytime 0.
But if i do a between "08-06-2020" and "07-06-2020".
I got the right result.
For mysql both query working fine.
But not on MSSQL.
I dont know what I do wrong.
Thanks for help
You are getting 0 because the WHERE clause filters out all rows. There are multiple reasons; for instance:
The table could be empty.
dbSchDate could be a datetime with a time component.
The constant may not be interpreted correctly as a date/time.
No values in the table might match.
I would suggest a proper date format, in YYYYMMDD format:
WHERE dbSchDate = '20200806'
You can also use:
WHERE dbSchDate >= '20200806'
dbSchDate < '20200807'
This version works even if there is a time component, as does:
WHERE CONVERT(DATE, dbSchDate) = '20200806'

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.

XCODE - SQL Dates

I am trying to update my SQL DB when the the date has expired (i.e is less than now)
I have this lovely bit of SQL code
Update Notifications
SET Active = 'N'
where CAST(SetDate AS DATE) <= CAST('2012-08-23 11:19:00 +0000' AS DATE)
But it updates all the records (even if the date is not less than now)
I have also tried
Update Notifications
SET Active = 'N'
where CAST(SetDate AS DATE) < CAST('2012-08-23 11:19:00 +0000' AS DATE)
But this dosent affect any rows.
I guess I have something a little confused??
Any help???
Thanks
I cannot see anything odd about the code, except for this line:
CAST('2012-08-23 11:19:00 +0000' AS DATE)
If you are using CAST() to change it to the date, then there is no need to pass in the time portion of the value.
You did not provide the full table schema but one thing to consider is using a bit field for the Y/N values.
Here is a SQL Fiddle with the code working
Update Notifications
SET Active = 'N'
where CAST(SetDate AS DATE) <= CAST('2012-08-23' AS DATE)
One thing you have confused is types in SQL. I don't know what database you are using, but it probably has types for boolean (or int) and date. Storing booleans and dates as strings is very inefficient. For example, even if your example did work it would have to scan the entire database and cast each value in order to compare it.
The clue to the error is the < vs <=. This hints at the values being equal. If the cast fails on both sides, and returns the default value of zero, then then will be equal.
You should change your schema to use the correct types, then your query will work.

Passing date parameters to Oracle Query in SSRS

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 = ?

Sql Shorthand For Dates

Is there a way to write a query equivalent to
select * from log_table where dt >= 'nov-27-2009' and dt < 'nov-28-2009';
but where you could specify only 1 date and say you want the results for that entire day until the next one.
I'm just making this up, but something of the form:
select * from log_table where dt = 'nov-27-2009':+1;
I do not believe there is one method that is portable to all RDBMSes.
A check in one of my references (SQL Cookbook) shows that no one RDBMS solves the problem quite the same way. I would recommend checking out Chapter 8 of that book, which covers all of the different methods for DB2, Oracle, PostgreSQL, MySQL.
I've had to deal with this issue in SQLite, though, and SQL Cookbook doesn't address that RDBMS, so I'll mention a bit about it here. SQLite doesn't have a date/time data type; you have to create your own by storing all date/time data as TEXT and ensure that your application enforces its formatting. SQLite does have a set of date/time conversion functions that allow you to add nominal date/times while maintaining the data as strings. If you need to add two time durations (HH:MM:SS) to each other, though, based upon data that you've stored in text columns that you are treating as date/time data, you'll have to write your own functions (search for "Defining SQLite User Functions") and attach them to the database at runtime via a call to sqlite3_create_function(). If you want an example of some user functions that add time values, let me know.
For MS SQL Server, check out DATEPART.
/* dy = Day of Year */
select * from log_table where datepart(dy, dt) = datepart(dy, '2009-nov-27');
With SQL Server, you could
Select * From table
Where dt >= DateAdd(day, DateDiff(day, 0, #ParamDate), 0)
And dt < DateAdd(day, DateDiff(day, 0, #ParamDate), 1)
As long as you are dealing with the date data type for the respective data type, the following will work:
t.date_column + 1
...will add one day to the given date. But I have yet to find a db that allows for implicit data type conversion into a date.
SELECT '12-10-2009' + 1
...will fail on SQL Server because SQL Server only performs the implicit conversion when comparing to a datetime data type column. So you need to use:
SELECT CONVERT(DATETIME, '12-10-2009') + 1
For Oracle, you'd have to use the TO_DATE function; MySQL would use something like STR_TO_DATE, etc.
Have a column that just has the date part (time is 00:00:00.000) and then you can add a where clause: WHERE dt = '2009-11-27'