Select records after specific date in SQL - 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'

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 by date in oracle

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

Display a date that is the same as today in SQL Server [duplicate]

I have a table TEST with a DATETIME field, like this:
ID NAME DATE
1 TESTING 2014-03-19 20:05:20.000
What I need a query returning this row and every row with date 03/19/2014, no matter what the time is. I tried using
select * from test where date = '03/19/2014';
But it returns no rows. The only way to make it work that I found is to also provide the time portion of the date:
select * from test where date = '03/19/2014 20:03:02.000';
use range, or DateDiff function
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
or
select * from test
where datediff(day, date, '03/19/2014') = 0
Other options are:
If you have control over the database schema, and you don't need the
time data, take it out.
or, if you must keep it, add a computed column attribute that has the time portion of the date value stripped off...
Alter table Test
Add DateOnly As
DateAdd(day, datediff(day, 0, date), 0)
or, in more recent versions of SQL Server...
Alter table Test
Add DateOnly As
Cast(DateAdd(day, datediff(day, 0, date), 0) as Date)
then, you can write your query as simply:
select * from test
where DateOnly = '03/19/2014'
Simple answer;
select * from test where cast ([date] as date) = '03/19/2014';
I am using MySQL 5.6 and there is a DATE function to extract only the date part from date time. So the simple solution to the question is -
select * from test where DATE(date) = '2014-03-19';
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
This works for me for MS SQL server:
select * from test
where
year(date) = 2015
and month(date) = 10
and day(date)= 28 ;
select * from test
where date between '03/19/2014' and '03/19/2014 23:59:59'
This is a realy bad answer. For two reasons.
1.
What happens with times like 23.59.59.700 etc.
There are times larger than 23:59:59 and the next day.
2.
The behaviour depends on the datatype.
The query behaves differently for datetime/date/datetime2 types.
Testing with 23:59:59.999 makes it even worse because depending on the datetype you get different roundings.
select convert (varchar(40),convert(date , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime , '2014-03-19 23:59:59.999'))
select convert (varchar(40),convert(datetime2 , '2014-03-19 23:59:59.999'))
-- For date the value is 'chopped'.
-- For datetime the value is rounded up to the next date. (Nearest value).
-- For datetime2 the value is precise.
use this
select * from TableName where DateTimeField > date() and DateTimeField < date() + 1
Try this
select * from test where Convert(varchar, date,111)= '03/19/2014'
you can try this
select * from test where DATEADD(dd, 0, DATEDIFF(dd, 0, date)) = '03/19/2014';
There is a problem with dates and languages and the way to avoid it is asking for dates with this format YYYYMMDD.
This way below should be the fastest according to the link below. I checked in SQL Server 2012 and I agree with the link.
select * from test where date >= '20141903' AND date < DATEADD(DAY, 1, '20141903');
Bad habits to kick : mis-handling date / range queries
You can use this approach which truncates the time part:
select * from test
where convert(datetime,'03/19/2014',102) = DATEADD(dd, DATEDIFF(dd, 0, date), 0)
-- Reverse the date format
-- this false:
select * from test where date = '28/10/2015'
-- this true:
select * from test where date = '2015/10/28'
Simply use this in your WHERE clause.
The "SubmitDate" portion below is the column name, so insert your own.
This will return only the "Year" portion of the results, omitting the mins etc.
Where datepart(year, SubmitDate) = '2017'
select *, cast ([col1] as date) <name of the column> from test where date = 'mm/dd/yyyy'
"col1" is name of the column with date and time
<name of the column> here you can change name as desired
select *
from invoice
where TRUNC(created_date) <=TRUNC(to_date('04-MAR-18 15:00:00','dd-mon-yy hh24:mi:ss'));
Test this query.
SELECT *,DATE(chat_reg_date) AS is_date,TIME(chat_reg_time) AS is_time FROM chat WHERE chat_inbox_key='$chat_key'
ORDER BY is_date DESC, is_time DESC
select * from invoice where TRANS_DATE_D>= to_date ('20170831115959','YYYYMMDDHH24MISS')
and TRANS_DATE_D<= to_date ('20171031115959','YYYYMMDDHH24MISS');
SELECT * FROM test where DATEPART(year,[TIMESTAMP]) = '2018' and DATEPART(day,[TIMESTAMP]) = '16' and DATEPART(month,[TIMESTAMP]) = '11'
use trunc(column).
select * from test t where trunc(t.date) = TO_DATE('2018/06/08', 'YYYY/MM/DD')

Table not modified since date

I have a table with following fields.
Part ID, Quantity, Last modified date
I am using the following simple query to retrieve data, how can I add more criteria to show that quantity in hand 0 and part did not modified since 01/01/2015 till now.
SELECT *
FROM table001
where quantity_on_hand=0
Just add another filter in your WHERE clause like below:
SELECT *
FROM table001
WHERE quantity_on_hand=0
AND ModificationDate <= '20150101' -- Date format = 'YYYYMMDD'
I think this will do what you're looking for
SELECT * FROM table001 WHERE quantity_on_hand = 0 AND last_modified_date <= '2015-01-01'
If you are looking to combine multiple conditions you could use AND,OR operators after WHERE clause.
Here I think you might be looking for:
SELECT
*
FROM
table001
WHERE
qunatity_on_hand=0 AND last_modified_date <= '2015-01-01';