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

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

Related

Updating a table column with oldest date in each row, except where the oldest date has already passed?

I would like to update the contents of the Date1 column to reflect the oldest date in each row, unless the date has already passed (Date1 < current date), in which case i'd like Date1 to be populated with the 2nd oldest date in the row.
For added context, The table has thousands of rows and ~15 columns, only a handful of which are dates. I've used the least function in the past to update this column with the oldest date in each row, but I can't figure out how to update it with the 2nd oldest date if the oldest date is prior to the current date.
ID
Date 1
Date 2
Date 3
Date 4
001
01/14/2022
01/15/2022
01/16/2022
002
04/15/2019
03/20/2021
06/16/2021
You want the oldest date today or later. You can do this without a subquery, using least() and conditional expressions:
update mytable t
set date1 = least(
case when date2 >= current_date then date2 end,
case when date3 >= current_date then date3 end,
case when date4 >= current_date then date4 end
)
The case expression turns "past" dates to null, which least() ignores.
An alternative unpivots the columns to rows and then uses filtering and aggregation:
update mytable t
set date1 = (
select min(date)
from (values (date2), (date3), (date4)) d(dt)
where dt >= current_date
)
So something like. You would need to modify to create appropriate WHERE conditions for the UPDATE. This is obviously untested on your setup, so try on test/expendable data.:
WITH dt AS
(SELECT unnest(ARRAY['11/02/2020'::date, '12/31/2020'::date, '01/01/2021'::date]) AS dt_val)
UPDATE
some_table
SET
dt1 =
(SELECT
*
FROM
dt
WHERE
dt_val >= '12/30/2020'::date
ORDER BY
dt_val
LIMIT 1);
dt_val
------------
12/31/2020
Where you would replace the values in ARRAY with the field names. The query creates an array from the date values and then unnest(s) the array to create rows. It then looks for the row values that are greater then or equal to the target date where the rows are ordered by the date values. The result is limited to the date value that is oldest by the LIMIT 1.

Check if input date falls within table of start and end dates

I'd like to run a query to check if an input date falls within any given term dates from a table.
Example table of Term Dates:
<table border="1">
<tr><th>termID</th><th>txtStartDate</th><th>txtFinishDate</th></tr>
<tr><td>37</td><td>2017-09-05 00:00:00</td><td>2017-12-15 23:59:00</td></tr>
<tr><td>38</td><td>2018-01-09 00:00:00</td><td>2018-03-29 23:59:00</td></tr>
<tr><td>39</td><td>2018-04-24 00:00:00</td><td>2018-07-06 23:59:00</td></tr>
<tr><td>40</td><td>2018-09-04 00:00:00</td><td>2018-12-14 23:59:00</td></tr>
<tr><td>41</td><td>2019-01-08 00:00:00</td><td>2019-03-29 23:59:00</td></tr>
<tr><td>42</td><td>2019-04-24 00:00:00</td><td>2019-07-05 23:59:00</td></tr></table>
Given an date, let's say today's date, does it fall between any of the rows' start and end date.
Example code approach
date = now()
for (row in rows):
if date between row.txtStartDate and row.txtEndDate:
return "yes"
I can use between logic with given dates but unsure how to apply this to the entire table.
select case when getdate() between '2019-04-24 00:00:00' and '2019-07-05 23:59:00' then 'yes' else 'no' END
Thanks
Try to use WHERE operator:
SELECT *
FROM YourTable yt
WHERE GETDATE() BETWEEN yt.txtStartDate AND yt.txtEndDate
You need to reference the table's columns instead of hard-coding the values. You do this using the table's alias and the proper column name.
DECLARE #InputDate DATE = '2019-01-05'
SELECT
T.*
FROM
YourTable AS T
WHERE
#InputDate BETWEEN T.txtStartDate AND T.txtEndDate
Using a WHERE clause will filter the rows from the table and only display the ones in which the condition is true. If you move your condition as an expression of a new column (in the SELECT column list) then it will display all rows with this new expression:
DECLARE #InputDate DATE = '2019-01-05'
SELECT
T.*,
IsInputDateBetweenDates = CASE
WHEN #InputDate BETWEEN T.StartDate AND T.EndDate THEN 'Yes'
ELSE 'No' END
FROM
YourTable AS T
You can try with IF EXISTS also as shown below.
If Exists (Select 1 from YourTable where GETDATE() between txtStartDate AND txtEndDate)
Begin
--Your if logic
end
else
begin
--Your else logic
end

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

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