I'm trying to search in an database for records with a specific date. I've tried to search in the following ways:
SELECT *
FROM TABLE_1
WHERE CAL_DATE=01/01/2015
and
SELECT *
FROM TABLE_1
WHERE CAL_DATE='01/01/2015'
I'm working with an Access database, and in the table, the dates are showing in the same format (01/01/2015). Is there something I'm missing in the SQL statement?
Any of the options below should work:
Format the date directly in your query.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=#01/01/2015#;
The DateValue function will convert a string to a date.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=DateValue('01/01/2015');
The CDate function will convert a value to a date.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=CDate('01/01/2015');
The DateSerial function will return a date given the year, month, and day.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=DateSerial(2015, 1, 1);
See the following page for more information on the above functions: techonthenet.com
Try using CDATE function on your filter:
WHERE CAL_DATE = CDATE('01/01/2015')
This will ensure that your input is of date datatype, not a string.
SELECT * from Table1 WHERE (CDATE(ColumnDate) BETWEEN #03/26/2015# AND #03/19/2015#)
if this works .. vote for it.. we use above query for searching records from 26th march to 19 the march.. change the dates accordingly..
SELECT * from Table1 WHERE (CDATE(ColumnDate) BETWEEN #03/26/2015# AND #03/19/2015#)
if this works .. vote for it.. we use above query for searching records from 26th march to 19 the march.. change the dates accordingly..
Related
I want to know the difference betweeen 2 dates.
I need something like this:
"sysdate minus the datevalue of a other table"
Currently I have something like this, but it's not working
SELECT something FROM myTable1 WHERE mydate >= sysdate-(SELECT latestExport FROM myTable2 WHERE id=1);
The 2nd select statement prints the date of the latest export. Example "27-JUL-21".
thanks for your help.
It can not work. Because the result of
sysdate-(SELECT latestExport FROM myTable2 WHERE id=1)
is a decimal value. For example 5121.2 days difference between SYSDATE and latestExport and you can not compare your Date value mydate with a decimal. This is the logic you used:
Is "25-10-2020" >= 5121.2 ?
You need to think again about the result you want to get from this query.
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
I have a set of dates that are in the format DD-MMM-YYYY. I need to be able to compare dates by using only the DD-MMM part of the date, since the year isn't important.
How would I achieve this?
I have tried reading up on the DATEPART function (edit: which evidently wouldn't work) but I can only theoretically get that to return either the DD or the MMM parts, not both of them at once.
Edit: added oracle tag. Sorry.
Example of date field: 01-MAR-1994
If your column is of type DATE then it doesn't have a format.
If I understand you right, then you want to view the mon-dd part only, so you need to convert it with TO_CHAR function,
i.e.:
select to_char(your_date_column, 'mon-dd') from your_table
Convert your dates using the following format, it will only month and the date part. You have to replace getdate() with you date fields.:
select convert(varchar(5),getdate(),110)
Assuming that you are using SQL Server or Oracle since you attempted using DATEPART, you can just get the day and month using the DAY() and MONTH() functions. Assuming, again, that the dates you are comparing are in two different tables, it would look similar to this:
SELECT MONTH(t1.date), DAY(t2.date)
FROM table AS t1
INNER JOIN table2 AS t2
ON t1.key = t2.key
WHERE MONTH(t1.date) = MONTH(t2.date)
AND DAY(t1.date) = DAY(t2.date)
EDIT: If you are just comparing rows in the same table, you only need a very simple query.
SQLFiddle
select id, TO_CHAR(most_recent, 'mon-dd')
from (
select id, MAX(date1) AS most_recent
from table1
group by id
)
You can also combine month and day into one integer:
EXTRACT(MONTH FROM datecol) * 100 + EXTRACT(DAY FROM datecol) AS MonthDay
Then it's easier to sort and compare.
select FORMAT(yourcoulmn_name, 'dd/MM') from table
This should do the trick
`select CONVERT(varchar(7),datetime_column,100) from your_table`
date_default_timezone_set("Asia/Kolkata");
$m = date("m");//Month
$d = date("d");//Day
$sql = "SELECT * FROM contactdata WHERE MONTH(date) = '$m' AND DAY(date) = '$d' ";
only checks day and month and returns today, day and month from database
SELECT LEFT(REPLACE(CONVERT(varchar(10),GETDATE()-1,3),'/',''),4)
WOuld this work for you?
FROMAT(DATETIME, 'dd-MMM') = FROMAT(DATETIME, 'dd-MMM') use any format you want
I have to select all rows from database by just passing date. For example to get all rows that have date 10/23/2012
In sqlite db I store this in DATE column:
01/01/1900 11:00:00 AM
I have tried to get by using date() but I get nothing for date:
select itemId, date(dateColumn) from items
So all I need is to compare only dates but can't find how to do this in sqlite.
Firstly, format your dates to the ISO-8601 standard. Wrap it in Date() to ensure it gets processed as a DATE. Finally, construct your range so that it will include everything from 12:00am onwards until just before 12:00am the next day.
select itemId, dateColumn
from items
where dateColumn >= date('2012-10-23')
AND dateColumn < date('2012-10-23', '+1 day')
SQLite columns are not typed. However, if you compare the column to a DATE as shown, it is sufficient to coerced the column data into dates (null if not coercible) and the comparison will work properly.
Example on SQLFiddle:
create table items (
itemid, datecolumn);
insert into items select
1,'abc' union all select
2,null union all select
3,'10/23/2012 12:23' union all select
4,'10/23/2012' union all select
5,'2012-10-23 12:23' union all select
6,'2012-10-23' union all select
7,'2012-10-24 12:23' union all select
8,'2012-10-24' union all select
9,date('2012-10-24 12:23') union all select
10,date('2012-10-24');
Results:
itemid datecolumn
5 2012-10-23 12:23
6 2012-10-23
Note that although rows 3 and 4 appear to be dates, they are not, because they do not conform to ISO-8601 formatting which is the only format recognized by SQLite.
In SQLite, there is no datatype DATE, it's stored as strings. Therefor, the Strings have to match exactly to be equal.
Since we don't want that, you'll want to "cast" the values from the date-column to pseudo-date and also cast your argument to a pseudo-date, so they can be compared:
SELECT itemId FROM items WHERE date(dateColumn) = date("2012-10-22");
Note that the date-command takes dates formated as YYYY-MM-DD, as further explained in an answer to this older question. The question also shows that you can use the BETWEEN x AND y-command to get dates, matching a range.
SELECT itemId,dateColumn FROM items WHERE dateColumn=date('YYYY-MM-DD HH:MM:SS');
SQLite Reference
select itemId,dateColumn from items
where dateColumn = #date
I have an sqlite table with Date of Birth. I would like to execute a query to select those records where the age is more than 30.
I have tried the following but it doesn't work:
select * from mytable where dob > '1/Jan/1980'
select * from mytable where dob > '1980-01-01'
Some thing like this could be used:
select dateofbirth from customer Where DateofBirth BETWEEN date('1004-01-01') AND date('1980-12-31');
select dateofbirth from customer where date(dateofbirth)>date('1980-12-01');
select * from customer where date(dateofbirth) < date('now','-30 years');
If you are using Sqlite V3.7.12 or greater
Dont use date(dateofbirth) just use dateofbirth. So your query would look like this:
select * from customer where dateofbirth < date('now','-30 years');
Using the magic docs at the sqlite website:
select * from mytable where dob < date('now','-30 years');
Try writing using the date format 'YYYY-MM-DD'
select * from mytable where dob > '1980-01-01'
A more programic way would be something like this:
select * from mytable where datediff(dob, now()) > 30
You'll Need to find specific syntax for sqlite.
select * from mytable where date(dob) > date('1980-01-10')
QLite3 has some cool new date functions. Per the docs site you can use date(), time(), datetime(), julianday(), unixepoch(), or strftime() depending on how your column data is formatted.
If you use strftime(), like my suggestion below, then you have to make sure that your column data is formatted the same way as your strftime string.
You would probably want something like:
SELECT * FROM mytable WHERE dob < strftime("%m/%d/%Y", 'now', '-30 year');
Note that you might have to change the format string here to match your own.
And here's some code that I use personally to give you a better idea of how powerful it is. It lets me get all the orders from the previous 3 months, not including this month.
SELECT * FROM orders WHERE SHIPPEDDATE > strftime('%m/%d/%Y', 'now', 'start of month', '-3 month');
The modifiers are very powerful with sqlite. The first string inside strftime() is the format, the 2nd string is when you want the date to start. 'Start of month' puts the day to 1, and '-3 month' goes back 3 months. So if I ran that today (08/03/2022), the date it uses would be 05/01/2022.