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

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

Related

Between & In operators- How can i give multiple conditions for a column in SQL

If i want to find # of suppliers who supplied products in 2010, 2011, 2012, 2013, all four years. how do i frame a query for that?
Between and In operators does not work.
Without knowing information about your table, if the year is stored in a column then you can do something like this
SELECT * FROM Table WHERE (year = '2010') OR (year = '2011') OR (year = '2012') OR (year = '2013');
if it's a type of DATETIME you can probably do
SELECT * FROM Table WHERE DATETIMEcolumn BETWEEN 'YYYY-MM-DD HH:MM:SS' AND 'YYYY-MM-DD HH:MM:SS'
being 'YYYY-MM-DD HH:MM:SS' the default DATETIME format of MySQL, we don't know what you are using but you get the idea.
Change the Date into Char and you can use IN:
SELECT COUNT(SUPPLIERS) FROM
(SELECT SUPPLIERS,Count(DISTINCT TO_CHAR(DATETIME_FIELD,'YYYY')) FROM TABLE_NAME
WHERE TO_CHAR(DATETIME_FIELD,'YYYY')
IN
('2010','2011','2012','2013')
GROUP BY SUPPLIERS
HAVING Count(DISTINCT TO_CHAR(DATETIME_FIELD,'YYYY'))=4);
There are many ways:
select * from TableName
where functionToExtractYear(DateColumn) in(2010, 2011, 2012, 2013)
select * from TableName
where functionToExtractYear(DateColumn) between 2010 and 2013
select * from TableName
where DateColumn >= '2010-01-01' and DateColumn <= '2014-01-01'
The last is preferable at least in Sql Server because it will use possible index on DateColumn.
You have not specified the engine you use so functionToExtractYear() may vary between different products. For Sql Server it is YEAR().

SQL Query to comparing the minutes in SQL 2008 R2 server

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')

Temporarily change format of a date column in SQL Query output

I am pulling data from SQL Server, and the date on our back end is formatted prtty ugly, like so:
2014-01-10 00:00:00.000
I was wondering if there is some sort of funciton that I can run in my query to display the date column differently in my output? I don't want to actually alter the way in which the Column is formatted on the back end, I just want to change how it is presented to the user in the results of a sigle query.
Thank you for your help!
Edit: I am using SQL server 2008 R2 as my RDBMS
SQL 2012: Has FORMAT():
SELECT FORMAT(Date_Field,'ddd, MM yyyy')
Here's a list of FORMAT() options
Prior to SQL 2012:
SELECT CONVERT(VARCHAR(12),Date_Field,109)
List of CONVERT() styles
If you just want to remove the time portion you can:
SELECT CAST(Date_Field AS DATE)
You can format datetime output using the CONVERT function. Review the MSDN documention and the W3Schools page for the Convert function for more details and examples.
Here is quick example of a few different formats available.
SELECT CONVERT(VARCHAR(19), GETDATE())
SELECT CONVERT(VARCHAR(10), GETDATE(),10)
SELECT CONVERT(VARCHAR(10), GETDATE(),110)
SELECT CONVERT(VARCHAR(11), GETDATE(),106)
SELECT CONVERT(VARCHAR(24), GETDATE(),113)
Results:
Feb 27 2014 11:54AM
02-27-14
02-27-2014
27 Feb 2014
27 Feb 2014 11:54:33:977

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()

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