Search data between two timestamp in oracle - date

I am trying to search data between two dates, but it retrieving old data where not in the range i gave? kindly advice me how to re-write the below query logic
select *
from Users
where TO_CHAR(Users.lastloggedindate,'dd-MON-yy HH.MI.SS') between '17-JAN-17 07.16.55' and
'18-JAN-17 09.20.22';

Try using to_date() instead of to_char(), to convert the two points of the range into dates against which your logged in date can be compared:
select *
from Users
where Users.lastloggedindate between to_date('2017-17-01 07:16:55', 'YYYY-DD-MM HH24:MI:SS') and
to_date('2017-18-01 09:20:22', 'YYYY-DD-MM HH24:MI:SS');

Related

To filter sales based on max date value minus 6 months

I'm trying to filter the data based on the date filter. The date column in my table is in date9. format(30JUN2017).
I want to filter the date column by subtracting 6 months from the existing date, which will be 31DEC2017.
e.g: date<31DEC2017
Can anyhow suggest on how to do this, I have tried using intx function and other options as well.
Thanks for your help.
Best Regards,
AJ
When manipulating dates in SAS using their formatted values you should enclose them in ""d, i.e. in your example it should be where date<"31DEC2017"d. If your date is stored in date9 format as a string, you can make it into a date like so: where input(date,date9.)<"31DEC2017"d
Now your question seems to differ slightly from its title. According to the latter, you want to filter on max_date - 6 months, whatever that max date might be in your dataset. This could be done so:
proc sql;
select *
from t
where intnx('month',date,6)<(select max(date) from t)
;
quit;
If however, for some strange reason, by "max date" you mean the current date, the above query simply becomes this:
proc sql;
select *
from t
where intnx('month',date,6)<date()
;
quit;

Cast varchar as date select distinct top 100

I am trying to fix a query that has come to light in SSRS after the new year. We have an input that comes from another application. It grabs a date and stores it as varchar. The SSRS report then fetches the top 100 'dates' but when 2017 dates have come around, this are not in the top 100.
The existing query is as follows
SELECT DISTINCT TOP (100)
FROM DenverTempData
ORDER by BY Date DESC
The date is stored as VARCHAR. So obviously this query doesn't grab a value such as 01012017 as being a top 100 (over values likes 12312016). I thought maybe I can simply change the datatype on this column to datetime. But the information comes from a flat file and is converted, so it's a little more difficult that that. So I'm hoping to do a select of the distinct top 100 while converting the date column to datetime or just date and grabbing the last 100 dates.
Can someone help with the query syntax? I'm thinking a cast to convert varchar to date, but how do I format with distinct top 100? I'm simply looking to retrieve the last 100 dates in chronological order from a column that is stored as varchar but contains a string representing a date.
Hopefully that makes sense
It is always a bad idea to store a date as string. This is highly culture specific!
You can cast your formatted string-date to a real date like this:
DECLARE #DateMMDDYYYY VARCHAR(100)='12312016';
SELECT CONVERT(DATE,STUFF(STUFF(#DateMMDDYYYY,5,0,'-'),3,0,'-'),110)
After the conversion your sorting (and therefore the TOP 100) should work as expected.
My strong advise: Try to store your dates in a column of a real date type to avoid such hassel!
SELECT DISTINCT TOP 100 (CAST(VarcharColumn as Date) as DateColumn)
FROM TABLE
Order by DateColumn desc

Obtain date without timestamp in DB2

Please pardon my ignorance if I have missed any documentation/solution for the same. But I searched the web and could not find an answer.
I have a simple question. In the DB2 table,I have a column of type date and the with data of format 04/25/2013 12:00:00AM . When I query the DB2 database, I want to obtain just the date and not the timestamp i.e to obtain "04/25/2013" and not "04/25/2013 12:00:00AM". I tried DATE(column name) and just gave back the complete value including the time stamp.
This looks like a TIMESTAMP and not a DATE column. If it is indeed a TIMESTAMP column try this:
select varchar_format(current timestamp, 'MM/DD/YYYY') from sysibm.sysdummy1 ;
Just replace the current timestamp in the above example with your column and sysibm.sysdummy1 with your table.
The good thing about varchar_format is that it lets you easily format the timestamp. Just change the 'MM/DD/YYYY' part to 'YYYY.MM.DD' to get a format like '2017.08.18'.

convert and filter datetime with mysql

Iam struggling to get all dates from now (all dates from today or the future).
Unfortunately my dates are in this format: '%d%m%Y %H:i' (08.06.2013 12:00)
I tried a couple of things, the last was:
SELECT * FROM `events` WHERE DATE_FORMAT(SUBSTRING(enddate,1,9),'%Y%m%d') >= DATE_FORMAT(CURDATE(),'%Y%m%d')
I thought this would convert into: (when date = 27.08.2013)
SELECT * FROM `events` WHERE 20130827 >= 20130608
But its now working... I get too much results :-/
Any help would be appreciated...
Thanks!
You should try using STR_TO_DATE (reference).
SELECT * FROM `events` WHERE STR_TO_DATE(enddate, '%Y.%m.%d') >= CURDATE();
STR_TO_DATE parses the string according to given format ('%Y.%m.%d' in your case) and returns a date string that can be used with MySQL. Once converted, you can freely compare it against other date values or functions like CURDATE. You should read the documentation for more details.
In the longer run, you will be better off using actual DATE and DATETIME columns.

getting date from timestamp in PostgreSQL

I have a PostgreSQL timestamp as
2009-12-22 11:01:46
I need to change this to date as
2009-12-22
So that I can compare the dates in postgreSQL
How can I achieve this transformation?
Cast it to date.
SELECT yourtimestamp::date;
If you need to extract other kinds of stuff, you might want to use EXTRACT or date_trunc
Both links are to the same page, were you'll find more date/time-related functions.
You can either use one of the Postgres date functions, such as date_trunc, or you could just cast it, like this:
SELECT timestamp '2009-12-22 11:01:46'::date
>>> 2009-12-22
SELECT [timestamp column] ::date;
Along in select query you can search on the basis of date
WHERE [timestamp column] :: date = '2022-05-30'