SQL Query to comparing the minutes in SQL 2008 R2 server - sql

What am trying to do is, I have table with Date field. Date field has entry in datetime format. Using that i have to display todays records after 5PM. Can anyone help me on this to get this query.
Thanks

After 5 Pm ?
Try this:
select *From Table where DATEPART(hh,yourdatefield)>=17 and and convert(date,yourdatefield,103)='2014-04-03'
this will the the records after 5pm .

Try this.
Select *
FROM tablename
WHERE datefield < '1/1/2014 17:00:00.000'

select * from table1
where
CONVERT(date,Datefield) = CONVERT(date,GETDATE()) and
CONVERT(time,Datefield) > convert(time,'17:00')

Related

SQL Server: how to select records with specific date from datetime column

I have a table with one column dateX formatted as datetime and containing standard dates.
How can I select all records from this table where this dateX equals a certain date, e.g. May 9, 2014 ?
I tried the following, but this returns nothing even if I have several records with this date.
SELECT *
FROM dbo.LogRequests
WHERE (CONVERT(VARCHAR(10), dateX, 101) = '09/05/14')
Edit: In the database the above example looks as follows, using SQL 2012: 2014-05-09 00:00:00.000
The easiest way is to convert to a date:
SELECT *
FROM dbo.LogRequests
WHERE cast(dateX as date) = '2014-05-09';
Often, such expressions preclude the use of an index. However, according to various sources on the web, the above is sargable (meaning it will use an index), such as this and this.
I would be inclined to use the following, just out of habit:
SELECT *
FROM dbo.LogRequests
WHERE dateX >= '2014-05-09' and dateX < '2014-05-10';
For Perfect DateTime Match in SQL Server
SELECT ID FROM [Table Name] WHERE (DateLog between '2017-02-16 **00:00:00.000**' and '2017-12-16 **23:59:00.999**') ORDER BY DateLog DESC
SELECT *
FROM LogRequests
WHERE cast(dateX as date) between '2014-05-09' and '2014-05-10';
This will select all the data between the 2 dates

How to query for today's date and 7 days before data?

I'm using sql server 2008. How to query out a data which is the date is today and 7 days before today ?
Try this way:
select * from tab
where DateCol between DateAdd(DD,-7,GETDATE() ) and GETDATE()
Query in Parado's answer is correct, if you want to use MySql too instead GETDATE() you must use (because you've tagged this question with Sql server and Mysql):
select * from tab
where DateCol between adddate(now(),-7) and now()

Select rows where date is between [this] and [that]

I have a table that has a column 'inspect_date'. This column contains dates in the format DD/MM/YYYY HH:MM:SS. I am trying to find the records that fall between April 2012 and September 2012. What can I put in my WHERE clause to be able to do this?
I'm using Microsoft SQL Server 2008.
select * from table
where inspect_date>= '04/01/2012 00:00:00.000'
and inspect_date< '10/01/2012 00:00:00.000'
WHERE inspect_date BETWEEN '2012-04-01' and '2012-09-30'
SELECT * FROM ....WHERE
inspect_date >= CAST('20120401' AS datetime) and
inspect_date < CAST('20121001' AS datetime)
for year selection you can use
WHERE DATEPART(YEAR,inspect_date)=2012
If you need it to be flexible, try taking the date apart like this:
WHERE MONTH(inspect_date) > 3 AND YEAR(inspect_date)= 2011
OR MONTH(inspect_date) <= 5 AND YEAR(inspect_date)= 2012

SQL to Return Dates for Selected Week Only

I'm writing a SQL query on a timesheet report. I need the report to return only the details for the week of the selected date.
E.g., if I pick 02/01/2012 (dd/MM/yyyy), then it should only return results between 02/01/2012 and 08/01/2012.
Thanks
SELECT
*
FROM
yourTable
WHERE
dateField >= #yourDate
AND dateField < #yourDate + 7
Some variations of SQL may have specific ways of adding 7 days to a datevalue. Such as...
- DateAdd(Day, 7, #date)
- DATE_ADD(#date, INTERVAL 7 DAYS)
- etc, etc
This option is both index friendly, and is resilient to database fields that have time parts as well as date parts.
You easiest is the equivalent of WEEK_OF_YEAR function in your SQL engine
But you can also use DATE_ADD
WHERE table.date BETWEEN target_date AND DATE_ADD(target_date,INTERVAL 7 DAY)
That depends on the database system you're using. MySQL has a function calles WEEK(), SQL Server can do something like this with the DATEPART() function:
MySQL:
SELECT
*
FROM table
WHERE WEEK(date_col) = WEEK('02/01/2012');
SQL SERVER:
SELECT
*
FROM table
WHERE DATEPART(WEEK, datecol) = DATEPART(WEEK,'02/01/2012');
SELECT * FROM table_name
WHERE date BETWEEN '1/02/2012' AND '1/08/2012';
you can replace the example date with your date and yourdate + 6.
Look here for example: http://www.roseindia.net/sql/sql-between-datetime.shtml

SQL date Statement

I need some help figuring out and SQL Statement.
I know what I want I just cant express it.
Im using php, so it doesnt need to be exclusivly SQL, its to act as a filter.
Pseudo code
$query="SELECT * FROM MyTable WHERE 'TIS' is not older than 2 days or empty = ''$ORDER"; }
TIS in the name of the column in my table were I store dates in this format 03-12-09 (d,m,y).
The $ORDER is for ordering the values based on values from other fields not dates.
Im looking at
SELECT *
FROM orders
WHERE day_of_order >
(SELECT DATEADD(day,-30, (SELECT MAX(day_of_order) FROM orders)) AS "-30 Days");
But i dont quite think im on the rigth track with this.
Thanks
Try the following:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, SYSDATE) > SYSDATE - INTERVAL '2' DAY
$ORDER
I don't know what database you're using - the above uses Oracle's method of dealing with time intervals. If you're using SQL Server the following should be close:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, GETDATE()) > DATEADD(Day, -2, GETDATE())
$ORDER
In MySQL try this:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, NOW()) > DATE_SUB(NOW(), INTERVAL 2 DAYS)
$ORDER
I hope this helps.
So, I was pretty lost in all this.
How did it got solved:
First I understood that the Statement I was using was not supported by MySql thanks to eligthment from Bob Jarvis.
_ Second In a comment by vincebowdren wich "strongly" adviced me to change the data type on that field to Date wich indeed I had not, it was a string.
It was pretty Dumb for me to try using SQL operations for Dates on a field that had String values.
So I just RTFM: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
and:
mysql> SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;
Then proceeded to change the field value to date.
and this is my perfectly working query:
$query="SELECT * FROM MyTable WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= TIS OR TIS = 0000-00-00 $ORDER "; }
I would like to thank the posters for their aid.