I'm trying to pull records from a table that are from the last two years. The field is stored as a datetime data type in SQL Server 2008. The query I attempted is:
Where ChangeWho<>N'RMADMIN'
And ChangeWho<>N'dbo'
And ChangeWhen < dateadd(year,-2,getdate())
I do not want any records where the ChangeWho is 'RMADMIN' or 'dbo' but I also only want records that have been changed within the last two years, from today's date.
Looking at my record set after the query has run, I see records from 2012 and before so it something apparently isn't correct with my ChangeWhen statement.
Any advice on how to correct it?
Thanks,
Try
Where ChangeWho<>N'RMADMIN'
And ChangeWho<>N'dbo'
And ChangeWhen > dateadd(year,-2,getdate())
Related
The table doesn't have any date time column. I want to if there is any inbuilt keyword which can does that.
I want to know all commits done after a particular date.
If flashback is enabled on the database you can get records on the table in an around a particular date range in Oracle.(It purely depends on if its enabled and for how long the flashback needs to be kept)
You can query to see the data in the table as of 3 days back as follows
select *
from table as of timestamp sysdate-3
I'm trying to apply 'curdate()' functionality to a select statement from DB2. I'm used to MySQL but I'm still trying to get the hang of a lot of the DB2 functionality and how to essentially marry the two.
My query is complete except for one line. I'm trying to select based on a ship date, which is the column EXTD1H and I need to check it against today or curdate(). The problem is that column in DB2 is an integer format, not a date format, and I don't have the option of changing it. In prior inserts to mysql, I've been able to put it into Y-m-d format and I know I can trim the year using LEFT(EXTD1H, 4) but I have no idea how to modify my select so that I can say WHERE EXTD1H is today so that I'm only selecting records for this date.
Here's the query:
select
invnoz as ORDER,
fstatz as STATUS
from gportafl
/*where EXTD1H is curdate, hypothetically*/
AND FSTATZ <> 'S'
limit 20;
As you can see, I have a commented line where my issue is. I'm sure it's simple I just can't seem to find in the documentation exactly what I'm looking for, which is to be able to use that INT column to verify that selected records are from today.
UPDATE:
All values from the column are in YYYYMMDD format i.e.
20180202
but it should be 2018-02-02
It's best not to do operations on the columns, so the indexes are used.
You can typecast the current date to fit your data as follows:
WHERE extd1h = INTEGER(VARCHAR_FORMAT(CURRENT DATE,'YYYYMMDD'))
I will like to query a MySQL database table that has a timestamp field. The query is a SELECT statement that retrieves, as part of the columns, the month value in the timestamp. At present, I have no idea what to write.
The main reason I'm doing this in MySQL is that I would want to sort the result by that month value.
Perhaps if it's not possible, a PHP solution will be highly welcome.
(It's been a long time I asked any question on SO, so please forgive the formatting. Hope to catch up on it soon.)
I am trying to write a query to extract records updated in the last 2 hours in SQL Server 2008.
Could anyone help me write this?
select * from table where table.date1>=dateadd(hh,-2,getdate())
dateadd() function lets you subtract hours from getdate() letting you choose records updated past 2 hours
First, you have to design the table so you have a field where the time of the last change will be stored
Then, whenever you update a row, update the value in the 'last update' field. After that, you can use a script such as suggested by Vijaykumar
The downside of this method is that when a single record was changed more than once in the specified time period, you will be notified only about the time of the last update.
Another solution for tracking the updates is to read the database online transaction log file, but you'll need a third party tool for that
I need a column in a MySql datatable which shows the difference between now and a timestamp. I have the following column within a view but I need a corresponding column in a datatable (InnoDB)
time_format(timediff(`myTable`.`anyTimestamp`, now()), '%H:%i')) AS `timeDifference`
I may also use a procedure but this one should be executed at least every 3 minutes and I wonder how this procedure would influence overall perfomance of the datatable since there are > 1000000 datasets stored in it.
Any help is appreciated!
Do you really need a column, or do you in fact just need the time difference to be caluclated in your SQL statement.
See : SQL time difference between two dates result in hh:mm:ss