List rows after specific date - sql

I have a column in my database called "dob" of type datetime. How do I select all the rows after a specific DoB in SQL Server 2005?

Simply put:
SELECT *
FROM TABLE_NAME
WHERE
dob > '1/21/2012'
Where 1/21/2012 is the date and you want all data, including that date.
SELECT *
FROM TABLE_NAME
WHERE
dob BETWEEN '1/21/2012' AND '2/22/2012'
Use a between if you're selecting time between two dates

Let's say you want to get all records from a table called Table_One with a datetime column called date_value that have happened in the past six months...
CREATE TABLE (
date_value DATETIME
)
SELCECT *
FROM Table_One
WHERE date_value > DATEADD(month, -6, getdate());
This gives a bit more dynamic of a solution.

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

Query a table which select all dates in table which have same year

I have a table tk. It has a column effective_date with a data type of date. The table also has some other columns. What I want to do is query the table such that output contains all dates having same year but month should differ
I have tried with below query in SQL Server, but it's not returning the desired result:
select * tk_id
from tk
group by tk_id, YEAR(effective_date);
Are you looking for a where clause?
select *
from t
where effective_date >= '2019-01-01' and effective_date < '2020-01-01'

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