how to query by date in oracle - sql

i have field date like this
type data = varchar
date
2019.01.01
2019.02.02
01.04.2016
i want to get data date >= 2019.01.01
i write query like this select * from mydb date >= '2019.01.01
the results are all data. i want the result just
2019.01.01
2019.02.02
anyone help me ?

If the column is stored as a date:
select t1.date -- use the column name (and if it is called "date", use the table name too)
from t1 -- the table name
where t1.date >= to_date('20190101','YYYYMMDD') -- Oracle dates can be funny

With case to check the format of the date:
select * from mydb
where
case locate('.', date)
when 3 then to_date(date,'DD.MM.YYYY')
else to_date(date,'YYYY.MM.DD')
end >= to_date('2019.01.01','YYYY.MM.DD')
This covers the case where there only exist dates in 2 formats:
DD.MM.YYYY and YYYY.MM.DD

Related

SSIS Expression to get julian date from yesterday

Can anyone help me with SSIS Expression
I have this query in the expression:
Select period, * from table
I want to add a where clause to get period = yesterday
But, period column is in julian date format.
at the end i want the same result like this query
Select period, * from table
Where getdate() - 1
Thank you
you only can use you table date time column to compare with the getdate
Select period,
dataDate, *
from table
Where dataDate = getdate() - 1

Extracting data from only the year

I have data in a table in SQL with dates, but how do I select only those that happen in 2021. (The dates look like 31-oct-2020) in the table. The dates are the actual date variable, not just text.
You should avoid storing your dates as text, but rather should use a proper date column. That being said, you may check the right 4 characters of the date string:
SELECT *
FROM yourTable
WHERE RIGHT(date_col, 4) = '2021';
If the column be an actual date type then use:
SELECT *
FROM yourTable
WHERE date_col >= '2021-01-01' AND date_col < '2022-01-01';
I suspect that your DB is Oracle after checking out your previous post. Then you can use
SELECT *
FROM yourTable
WHERE EXTRACT(year FROM dt) = 2021
or
SELECT *
FROM yourTable
WHERE TRUNC(dt,'YYYY') = date'2021-01-01'
or
SELECT *
FROM yourTable
WHERE dt BETWEEN date'2021-01-01' AND date'2021-12-31'
You can benefit the index if there's one on the date column(namely dt) by using the last SELECT statement
If using MSSQL, you can leverage the YEAR(...) to extract the year from a date.
Replace and , with the table name and date column name respectively.
select * from <tablename> where year(<datecolumn>) = 2021

How to query on DATE on DATETIME Column in SQL Server?

Here is my table having column(completed_on) with datetime field.
However I need to filter results based on date only.
SELECT * FROM myTable
WHERE completed_on
-- Pass Date Range in condition
Please tyr this
SELECT * FROM myTable
WHERE completed_on BETWEEN 'date1' and 'Date2'
-- Pass Date Range in condition
Another option is-
SELECT * FROM myTable
WHERE completed_on >= '20190701' --Sample start Date
and completed_on < '20190722'
-- This will make sure dates will be inclued less then
-- the given date. You have to pass this with
-- understanding what you want. If you want
-- date 7 to be included, please set the end date to 8

Select records after specific date in SQL

I have a column in my database called "begin_date".
I am trying to select records where the begin_date are greater than a specific date. I put
select * from Table_Name
where begin_date >= '1/1/2014'
However, it returns error message "String to date conversion error".
I am not sure how to modify the query to make it work?
Thanks!
Try using ISO (8601) standard date formats:
select *
from Table_Name
where begin_date >= '2014-01-01';
select *
from table
where data >= '01/01/2014'

Selecting Date from a table where the column variable type is Varchar in SQL

Suppose I have database table TABLE1 and there is Column COLUMN1. The variable declaration for that column is set up as VARCHAR(20). Also, the column value can be NULL or EMPTY.
Suppose I have some 'dates' in that column (Format 12/01/2011) and I need a query to retrieve the dates, cannot be NULL or Empty and the dates are upto one year in the past (Suppose current date is 01/25/2012. I need the dates before 01/25/2011).
Please, help.
convert the varchar to a date and then work on it.
SELECT convert(date, column1) as date
from table1
where convert(date, column1) < '01/25/2011'
and date is not null and date <> ''
Why don't you use date instead of a varchar for your date since you are keeping it in that form? If you save your dates in a form of YYYY-MM-DD as a varchar you could even use:
SELECT * FROM table1 WHERE column1 < DATE( "2011-01-01" )
but i would highly suggest you convert it to a date since they are using a similar format to the one you provided
SELECT `COLUMN1`
FROM `TABLE1`
WHERE `COLUMN1` is NOT NULL AND
`COLUMN1` != '' AND
STR_TO_DATE(`COLUMN1`, '%m/%d/%Y') < CURRENT_DATE() - INTERVAL 1 YEAR;
SELECT * FROM TABLE1 WHERE CONVERT(DATE, VC_DATE) < CONVERT(DATE,'01/25/2011')
AND LEN(VC_DATE)>0