Below query is giving me two different result for two different oracle database. ( database version I am not aware of )
I want to ask like TO_DATE function varies from database version ?
select * from ACC WHERE CHANGE = TO_DATE('01/02/2015','MM/DD/YY');
Note that I am facing issue in only = but < and > working fine.
In 1 db : this query is giving 1 record
In 2 db : this query is giving 0 record.
In DB : CHANGE value is 02-JAN-14
As far as I know, to_date() hasn't changed. More likely is that you have a time component on your column change, which prevents = from working. Try this:
select *
from ACC
where trunc(CHANGE) = TO_DATE('01/02/2015','MM/DD/YY');
Or, how I would prefer to write this:
where change >= DATE '2015-01-02' and
change < (DATE '2015-01-02') + 1
Using the date keyword, you can use ISO standard formats. This version also allows the use of indexes on the change column.
if they are on the same server is strange. If you are on two different servers verify that the date format of the server is the same.
You can see it in the settings under NLS
Related
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'
I've searched but been unable to figure this out. I used to use MS Access many many years ago but have switched mostly to PHP and MySQL to do my work. But when I"m at work, I have to use those resources.
Trying to build a basic query from an ODBC connection to a SQL Server. One of the columns is a date field (SQL server field type: datetime).
When I build the query and enter Date() for my Where clause should be it yields no results.
SELECT dbo_Order.OrderStatusID,
dbo_Order.FillerOrderNumber,
dbo_Order.RelevantClinicalInfo,
dbo_Order.ReasonForStudy,
dbo_Order.ProcedureDescList,
dbo_Order.PlacerFld2 AS Modality,
dbo_Order.ScheduleDate,
dbo_Order.ExplorerStatus,
dbo_Order.SiteID
FROM dbo_Order
WHERE (
(
(dbo_Order.PlacerFld2) = "CRFL"
OR (dbo_Order.PlacerFld2) = "CT"
OR (dbo_Order.PlacerFld2) = "SAMR"
)
AND ((dbo_Order.ScheduleDate) = DATE ())
AND ((dbo_Order.ExplorerStatus) = "SCHEDULED")
AND ((dbo_Order.SiteID) = 1)
);
I've tried also doing something like Date: Format([ScheduleDate], "dd/mm/yyyy") but this also returns no results.
MS Access 2016
SQL Server 2008
If your ScheduleDate include any time values, Date() returns 1/1/2018 00:00 as a default so if your Scheduled date is 1/1/2018 04:34:00 it won't match.
Try using DateValue([ScheduleDate]) to return just the date
I would prefer:
dbo_Order.ScheduleDate >= Date() AND dbo_Order.ScheduleDate < Date() + 1
DateValue([ScheduleDate])has to be computed on every row and can't use an index (if any) what can affect query performance on huge data. See Access query won't work when dates have times In addition DateValue can't handle NULL values.
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.
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.
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'