Hi I want to find the oldest date from a string date column in format 20180209 00:00:00.
I am using the following query to get the string column in date format
select from_unixtime(unix_timestamp(acc_last_change_date, 'yyyyMMddHHmmss')) from ACCOUNTS
but the result is returned as null.
Could you help me on same.
Just use min():
select min(acc_last_change_date)
from accounts;
Your string is in a suitable format for using min().
If you want the entire row, you can use:
select a.*
from accounts a
order by a.acc_last_change_date
limit 1;
Related
I would like to know how to know how to filter a table by a specific column, when this column contains a specific subtring.
Here's an example of my table:
I would like to obtain those rows where the column tsBegin contains 2020-08-04, maybe with something like:
SELECT * FROM mytable
where '2020-08-04' in tsBegin
Date operations:
where date(tsBegin) = date '2020-08-04'
A column named tsBegin should not be a string column, so you just want the date.
If tsBegin is a string, I would suggest that you convert it to a timestamp.
Use date functions, and half-open intervals:
select *
from mytable
where tsbegin >= date '2020-08-04'
and tsbegin < date_add(date '2020-08-04', interval 1 day)
Although a bit lenghtier to type, direct filtering against literal values is usually much faster than applying a date function on the column being filtered, as in where date(tsbegin) = date '2020-08-04'
In Table have a Date field of type date and I get 2 date from and to as String.
I want to filter the recodrs that exist between these 2 dates.
What will be the best way to select
The best is to transform the string dates into dates using your DBMS provided transformation functions,
Using Oracle for instance, i would write :
where
your_date_field
between to_date(from,format_of_from) and to_date(to,format_of_to)
I wanted to know how to perform the SQL SELECT operation on only a particular range of characters.
For example,I've got an SQL query:
SELECT date,score from feedback GROUP BY date
Now this date is of format yyyy/mm/dd.
So I wanted to strip the days or cut out the days from it and make it yyyy/mm, thereby selecting only 0-7 characters from the date.
I've searched everywhere for the answer but could not find anything.Could I maybe do something like this?
SELECT date(7),score from feedback GROUP BY date(7)
In MSQL you use LEFT other use substring
SELECT LEFT('abcdefg',2);
--
ab
So in your case
SELECT LEFT(date,7), score
FROM feedback
GROUP BY LEFT(date,7)
But again things will be much easier if you use a date field instead a text field.
Assuming that your date field is an actual date field (or even a datetime field) the following solution would work:
select left(convert(date ,getdate()),7) as Year_Month
Changing getdate() to your date field, it would look like:
select left(convert(date , feedback.date),7) as Year_Month
Both queries return the following:
2016-01
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 dont have proper timestamp in table; is it possible to delete 1 day old logs even now?
I have a column name as SESSION_IN which is basically a VARCHAR datatype, and the value will be like
2013-10-15 02:10:27.883;1591537355
is there any way to trim the number after ; and is it possible to compare with "sysdate" identifier?
This SP should compare all the session IDs with current datetime and it should delete if it is older then 1 day.
You can igonre time part and convert date into required format somthing like this
SYSDATE - to_date('date_col','YYYY-DD-MM')
then you can perform operations.
Use the Substring function to extract the datetime portion from the record, then use convert to datetime to cast it to datetime, and then finally use datediff to check if it was inserted yesterday. Use all these caluses in a
DELETE FROM table
WHERE ___ query
For Oracle you could use something like this:
SELECT
TRUNC(to_timestamp(SUBSTR('2013-10-15 02:10:27.883;1591537355',1,
(
SELECT
instr('2013-10-15 02:10:27.883;1591537355', ';')-1
FROM
dual
)
), 'YYYY-MM-DD HH:MI:SS.FF'))
FROM
dual;
Which gives you just the date portion of your input string. Just subtract the amount of days you want to log at the end.
Hope following query helps you:
Select Convert(Datetime,Substring('2013-10-15 02:10:27.883;1591537355',1,23)), DateDiff(dd,Convert(Datetime,Substring('2013-10-15 02:10:27.883;1591537355',1,23)),Getdate())