VB.net SQL statement to query 1 yr old Timestamp - sql

I want to display in my gridview all data whose timestamp is not older than 1 years from current date.
My timestamp is formatted like so: 20110125-071830 or yyyymmdd-hhmmss
I have tried to reference :
Retrieve rows less than a day old:
Select * from table_name WHERE DATE(timestampVal) > DATE(NOW() - INTERVAL 1 YEAR);
but got missing expression flag
I also tried various others; however, my timestampVal is different simply by how it is formatted.
Like for example: https://community.oracle.com/thread/2207956
With coding: Select * from table_name WHERE timestampVal < sysdate - interval '1' year ;
but get error: literal does not match format string which means sysdate can't read how mine is formatted.
How do I query my timestamp to pull all that are a year or less old?
FYI: timestampVal is string type [varchar]

It looks like the following should work:
Select *
from table_name
WHERE TO_DATE(timestampVal, 'YYYYMMDD-HH24MISS') > sysdate - interval '1' year;
Also note that I reversed the comparison: you indicated that you want rows where timestampVal is not older than 1 year ago - so timestampVal should be greater (newer) than current time minus one year.
Give that a try.
Best of luck.

Related

timestamp string to timestamp in sql

Date data saved from stripe start_date as string timestamp like "1652789095".
Now I want to filter with this timestamp string form last 12 months.
what should I do ?
how can I filter with this timestamp string?
These are some examples - I'm sure there are plenty of options that would work.
convert to date
select *
from Table
where
to_timestamp(cast(start_date as int)::date > date_add(now(), interval -1 year);
work with unix timestamps
-- approx 1 year ago, by way of example
select *
from Table
where
start_date > '1621253095';
-- exactly one year ago, calculated dynamically
select *
from Table
where
start_date >
cast(unix_timestamp(date_add(now(), interval -1 year)) as varchar);
I'm not a MySQL guy really so forgive any syntax errors and fix up the sql as needed to work in MySQL.
Resources:
PostgreSQL: how to convert from Unix epoch to date?
https://www.postgresonline.com/article_pfriendly/3.html

PostgreSQL - subtract 'days' from a returned 'date' value without also returning timestamp

I'm querying a table to get some date, like so:
SELECT date - INTERVAL '10 day' AS date
FROM example_table
WHERE username = 'Bob'
LIMIT 1;
The date column in the example_table does not have a timestamp. All dates in the column are stored in the following manner:
YYYY-MM-DD
The query above will return a result like so:
2016-11-20 00:00:00.000000
It takes the date found, goes back 10 days, and returns that date. But I want it to return the date without adding the timestamp, like so:
2016-11-20
If I use INTERVAL it always seems to add a timestamp. Is there a way to only get the date?
Your query is fine (but can be simplified, as demonstrated by a_horse_with_no_name). What you are seeing is a display issue. You can format your date to a string in the relevant format using to_char():
SELECT to_char("date" - INTERVAL '10 day', 'yyyy-mm-dd') AS "date"
FROM example_table
WHERE username = 'Bob'
LIMIT 1;
Note: LIMIT without an ORDER BY does not make sense: if there is more than one record in the resultset, you actually get a random record out of them.
You can use the interval notation and convert back to a date:
SELECT (date - INTERVAL '10 day')::date AS date
You can subtract (or add) an integer from a date. That integer represents the number of days:
SELECT "date" - 10 AS "date"
FROM example_table
WHERE username = 'Bob'
LIMIT 1;

Oracle 11g and SQL Server EPOCH-3600 and EPOCH (Unix_timestamp)

I need to write a select query that gets data in between EPOCH(datetime)-3600 AND EPOCH(datetime).
select all incidents modified in specific date range between EPOCH(datetime)-3600 AND EPOCH(datetime)
So my query is:
SELECT *
FROM TABLENAME
WHERE COLUMN_NAME BETWEEN COLUMNNAME-3600 AND COLUMNNAME
Will this query get the data from 1 hour ago to the current time, in Unix TimeStamp format?
Given a column (COLUMN_NAME) where data is stored as a unix timestamp (eg number of seconds elapsed since January 1st, 1970), you are looking to filter records based on a DATE passed as parameter.
My understanding is that you need a way to convert a unix timestamp to a date. There is no such built-in function in Oracle, however you are allowed to add a number to a date, where the number represents the number of days to add.
Try :
SELECT *
FROM TABLENAME
WHERE
TO_DATE('01/01/1970', 'dd/mm/yyyy') + COLUMN_NAME/60/60/24 -- convert unix timestamp to date
BETWEEN :mydate - 3600 AND :mydate
;
Replace both :mydate with the actual DATE for which you want the search to be performed.

Match partial date in SQL query?

I am looking for a way to match a date, with the one related to records on my db.
I know how to match strings with the LIKE operator, but it doesn't work for dates; unless I search just for a number (say all records at 10, all records from the 21st and so on). As soon as I put minutes for example, the search with LIKE return no records.
How do you actually search for partial date? My objective is to find all records newer than a partial date; in the current day.
select * from CLIENTS where CLOSING like '%12:30%'
This won't match anything; but if I use just 12 or 30, it works....although it is not doing what I want.
Do I have to convert date in strings? I get a partial date from another process, and I would like to match all the records newer than the partial date.
Try this query
select * from CLIENTS where
TO_CHAR(CLOSING , 'dd-mm-yy hh24:mi:ss') like '%12:30%'
or
select * from CLIENTS where
TO_CHAR(CLOSING , 'hh24:mi') = '12:30'
If you want loans more recent than today at 12:30pm, you are best served to use date arithmetic rather than relying on string conversion
SELECT *
FROM clients
WHERE closing >= trunc(sysdate) + interval '12' hour + interval '30' minute;
or
SELECT *
FROM clients
WHERE closing >= trunc(sysdate) + 12.5/24
Here, trunc(sysdate) returns today at midnight. Then you can either add a fractional number of days (1/24 adds one hour so 12.5/24 takes you to 12:30pm) or you can add one or more intervals (you could use to_dsinterval as well to create the interval).

How to retrieve the records based on a date from oracle database

I have a table with date column in it. I need to fetch the records from it based on
the given date.
Currently when i used the query:
select * from workingemployee_data where created_date like '20-Jan-2012'
I am getting those records which have created_date on 20-Jan-2012
But i want to get the records those were created 10 days earlier to a given
date (i.e) 20-Jan-2012.
Please suggest me on this.
This gives all records between today and 10 days ago:
SELECT *
FROM workingemployee
WHERE created_date BETWEEN sysdate - INTERVAL '10' DAY
AND sysdate
This gives all records entered exactly 10 days ago:
SELECT *
FROM workingemployee
WHERE created_date = sysdate - INTERVAL '10' DAY
Replace sysdate with exact date if you want.
Why do you use like and not = ?
Assuming that created_date is of type DATE, it's bad practice to rely on implicit conversion according to NLS_DATE_FORMAT (this is what happens when you compare a date and a string)
dd-mon-yyyy isn't a good format for querying since it deffers according to NLS_LANGUAGE better use mm for months numbers
So, either use #mvp's answer or do something like this:
SELECT *
FROM workingemployee
WHERE trunc(created_date) = to_date('20-01-2013', 'dd-mm-yyyy') - 10
SELECT *
FROM workingemployee
WHERE created_date > sysdate - INTERVAL '10' DAY;