Working with dates in SQL query using MDB-Tools - sql

I have been working on a project where I have to fetch data from MS-Access database. I am using mdb-sql tool for running sql queries into that database. But the problem occurs when I try to put where clause on a datetime column. It always throws a syntax error.
Please help me how can I use dates in where column.
I have tried using these queries -
SELECT * FROM table1 WHERE checkintime > '04/22/13 12:15:39'
SELECT * FROM table1 WHERE checkintime > #04/22/13 12:15:39#

mdb-sql displays human-readable dates in results, but requires timestamp values in queries -- calculated as the number of seconds sine 1/1/1970 (the beginning of the UNIX epoch).
So the correct query to extract all records with a date greater than 04/22/13 12:15:39 would be:
SELECT * FROM table1 WHERE checkintime > 1366650939
An easy way to calculate this from the command line is:
date --date='04/22/13 12:15:39' +"%s"

You can use
MS Access: Format Function (with Dates):
where Format([checkintime ],"yyyy-mm-dd hh:nn:ss")>='2013-04-22 12:15:39'

Related

Getting not valid month in Oracle sql

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.

SQL Count Where on a specific date... but the result is 0

Hi it is NOT my first time the i use this query:
SELECT COUNT(*) from dbo.SchDetail WHERE dbSchDate = '08-06-2020'
Query is working but he counts everytime 0.
But if i do a between "08-06-2020" and "07-06-2020".
I got the right result.
For mysql both query working fine.
But not on MSSQL.
I dont know what I do wrong.
Thanks for help
You are getting 0 because the WHERE clause filters out all rows. There are multiple reasons; for instance:
The table could be empty.
dbSchDate could be a datetime with a time component.
The constant may not be interpreted correctly as a date/time.
No values in the table might match.
I would suggest a proper date format, in YYYYMMDD format:
WHERE dbSchDate = '20200806'
You can also use:
WHERE dbSchDate >= '20200806'
dbSchDate < '20200807'
This version works even if there is a time component, as does:
WHERE CONVERT(DATE, dbSchDate) = '20200806'

cx_Oracle.DatabaseError: ORA-01843: not a valid month

select distinct sko.CONTENTId,
sko.HELPDESKID,
sko.SEGMENTID,
som.SUBMITTED_FOR_NAME,
sko.SUBMITTEDDATE,
to_date(sko.LASTMODIFIEDDATE, 'DD-MM-RR')
from sky_know_obj sko
join sky_object_mass som
on sko.CONTENTId = som.CONTENTId
where sko.LASTMODIFIEDDATE > date'2019-11-03'
and sko.LASTMODIFIEDDATE <= date'2019-12-03'
This is my oracle sql query. i am running it in python. when I run this in the Oracle SQL Developer then it is giving results but whenever I tried to execute it in the pycharm the following error is occuring:
cx_Oracle.DatabaseError: ORA-01843: not a valid month
when I run
select * from nls_session_parameters;
this in oracle sql developer then its showing
NLS_DATE_FORMAT = DD-MM-RR
As you apply TO_DATE to LASTMODIFIEDDATE column and it fails, it seems that not all values have valid month in that column which means that its datatype isn't date but varchar2.
SQL Developer doesn't return all rows, but the first 50 (or so). If your query scans the whole table, or rows that weren't displayed initially, then it'll fail. Try to navigate to the last row in returned record set in SQL Developer to see what happens.
On the other hand, if column's datatype is date, then don't to_date it; there's no point in doing that.
Also, you shouldn't rely on database's settings. Take control over your data and don't specify string when meaning date. Use date literal, e.g.
where lastmodifieddate > date '2019-11-03' -- instead of '03-11-19'

Sql Server Table date query showing incorrect result

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.

pgsql time diffrence?

How to write a query to find the time difference ?
time format is like this
2009-08-12 02:59:59
i want to compare
this time with
2009-08-12 02:59:10
how to check these two
i want to return some row having the time difference is 30sec
how to write a SQL statement ??
select date_part('second',date1) - date_part('second',date2)
In SQL you can do like this which give you output in seconds
SELECT * FROM your_table WHERE time1_column - time2_column = interval '30s'
Sorry this is the best I can do, given your description of the problem...
if both the times are columns in database table, you can directly use
relational operators (>, < and =)
Ex. if you have a table like
Test
(
id bigint,
starttime timestamp,
endtime timestamp
);
then you can have queries like
select * from test where starttime > end time
etc..
If you want to compare two dates in query, you can first convert text to time and then compare them
you can use: datetime function of pgsql
see: http://www.postgresql.org/docs/6.3/static/c10.htm
for more details