I am wondering if it's possible for us to use parenthesis to include multiple criteria after WHERE. For example, I am looking for data from multiple dates, and original code looks like this,
SELECT * FROM MyDB
WHERE Date = '2016-06-30' OR Date = '2016-09-30' OR Date = '2016-12-31'
This code will be extremely long if I will need to get data from more time periods, or column name is long and complex. I tried to change the code into the following format and apparently it's not correct,
SELECT * FROM MyDB
WHERE Date = ('2016-06-30', '2016-09-30', '2016-12-31')
I am wondering if there is a way we can code in the method I described above so that it can get me data from all dates (or other criteria)?
Thanks in advance!
Using IN
SELECT * FROM MyDB
WHERE Date IN ('2016-06-30', '2016-09-30', '2016-12-31')
Related
I have a table called Transactions that has a column called trans_date. I am just trying to do a simple query in the SQL*Plus command window
The query is
SELECT * FROM transactions WHERE
trans_date BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
When I run this query I get not valid month and there is a little * under trans_date. Most of what I have read suggests the query is right but I am not sure what the problem is. The data type is varchar2(20).
Since trans_date is a varchar and you're trying to query whether it's between two dates, you need to convert it to a date too. Assuming it has the same format as the literals in your query:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyy') BETWEEN
to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
Seems like problem is columns data type, Try convert it to date,
SELECT * FROM transactions
WHERE to_date(trans_date,'mm/dd/yyyy') BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
You need to convert trans_date to a date. However, you can use date constants for the comparisons:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyyy') BETWEEN DATE '2021-09-11' AND DATE '2021-09-12';
You should fix your data model so the dates are stored correctly, using Oracle's built-in data type.
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.
I see that a table has the data value as 18-May-2012. But when I query looking for the same date using the below query, no results are available.
Select Submit_Dt From Siebel.S_Order_Dtl
where submit_dt = '18-May-2012'
Could you help me sort this issue?
You need to convert string date into date with TO_DATE() function.
Also you need to take into account that your date might contain hours/minutes/seconds. In order to handle this you need to truncate submit_dt column.
In your case it would look like this:
Select Submit_Dt From Siebel.S_Order_Dtl
where TRUNC(submit_dt) = TO_DATE('18-May-2012','dd-MON-yyyy')
Try to convert the date to date format using to_date as below
Select Submit_Dt From Siebel.S_Order_Dtl
where submit_dt = to_date('18-May-2012','DD-MON-YYYY')
I was wondering if someone could help me out.
I need to return database results based on a date range, im using Classic ASP and MsSQL
My script gives me dates formatted as follows:
6/18/2014
The dates are saved in the database in the following format
12/24/2014 7:03:00 AM
What im wanting to do is something as follows:
SELECT * FROM table WHERE paid >= 6/18/2014 AND =< 6/28/2014
When i run that, im getting weird results as the dates arent formatted the same.
Can someone help me out.
Cheers,
you should put those two dates between single quotes like..
SELECT * FROM table WHERE paid BETWEEN '6/18/2014' and '6/28/2014'
EDIT:
you can use DATE_FORMAT(date,format) function to display date/time data in different formats.
here's some reference
http://www.w3schools.com/sql/func_date_format.asp
if its not typo
SELECT * FROM table WHERE paid BETWEEN '6/18/2014' AND '6/28/2014'
EDIT:-my database is storing it in the format yyyy-mm-dd
SELECT *
FROM table
WHERE DATE >= '2014-05-18'
AND DATE <= '2014-06-28'
and its working correctly
here is a reference
change the format according to your own database it will work
I want to get record from DB based on given date value. I used Timestamp as coulmn type. I used query as
SELECT *
FROM table1
WHERE LAST_UPDT_S = to_date('23-AUG-12')
Getting empty table. where as records were present in the table.
Please, sort out me from this issue.
Thanks in advance.
May be your table contains dates with time part so try to use:
SELECT *
FROM table1
WHERE Trunc(LAST_UPDT_S) = to_date('23-AUG-12')
Try this,
SELECT *
FROM table1
WHERE LAST_UPDT_S = to_date('23-AUG-12', 'DD-MON-YY')
SEE: TO_DATE Manual
Generally it's better to use a time range - this opens the possibility of using an index if one exists:
SELECT *
FROM table1
WHERE LAST_UPDT_S >= DATE '2012-08-23'
AND LAST_UPDT_S < DATE '2012-08-24'
(Also, it's better to not use TO_DATE without specifying the date format, otherwise it will use whatever the current session's NLS setting happens to be.)