How to display future date and today date in SQL - sql

I have this and wish to display future date and today date how can it be done?
Date
-----------
10/08/2014
09/08/2014
11/08/2014
and I only wish to display
10/08/2014
11/08/2014

Most versions of SQL have some way of getting the current date. For instance:
where date >= cast(CURRENT_TIMESTAMP as date)
where date >= cast(getdate() as date)
where date >= date(now())
where date >= trunc(date)
The specific syntax you need depends on the database you are using.
EDIT:
The first or second versions should work with SQL Server Express.

SQL will consider your date "11/08/2014" in mm/dd/yyyy format by default. You would need to rearrange the month before the date like "08/11/2014" before making the comparison. Below code will get you the required results:
SELECT CONVERT(VARCHAR(20),CAST(Time AS DATE),120)
FROM Table
WHERE CONVERT(VARCHAR(20),CAST(Time AS DATE),120)>=CONVERT(VARCHAR(20),GETDATE(),120)

Related

Using CASE statement to compare CURRENT_DATE with future dates, but I get an error

In my SQL script, I have a column of dates in "End_Date" in string format that includes old dates (before 2022) and future dates (March 2022 and onwards). I'm trying to write a CASE statement, where if the future date is greater than today's date, then mark it as "Latest." Basically anything after today's date should be marked as "Latest" Here's part of CASE statement:
CASE WHEN CAST(END_DATE AS DATE) > CURRENT_DATE() THEN "Latest"
I keep getting an error like "generic::out_of_range: Invalid date: '2022-09-30 " when trying to execute the query
Try this:
SELECT
CASE
WHEN CAST('2022-03-18' AS DATE) > CURRENT_DATE() THEN 'Latest'
ELSE 'Old'
END;
Please check the value of END_DATE. There might be possibility of wrong format of date

SQL date comparision in yyyy/mm/dd

Not being that great at sql, i've reached my limit.
I have a date in the yyyy/mm/dd format and i need to get all records "from a week ago"
I think i need some conversion stuff to be done cause this
d.date_begin >= DATEADD(day,-7, GETDATE())
is not working :), i'm TERRIBLE at convert and data type..
This will work if you want records from 7 days ago up to and including today's records
CAST(d.date_begin AS DATE) >= CAST(DATEADD(day,-7, GETDATE()) AS DATE)
where DATEDIFF(month,Your_date_column,getdate()) < 3
SQL server 2012 onwards, if date_begin is of datatype date
where d.date_begin >= cast(DATEADD(day,-7, GETDATE()) as date)
This will get anything in the last 7 days, regardless of time
You should store date/time values using native formats. Ok, sometimes we cannot. But you can easily convert your values to the correct format:
where cast(replace(d.date_begin, '/', '') as date) >= DATEADD(day, -7, GETDATE())
I should note that SQL Server is pretty good about conversions, so your initial code should not generate any errors -- unless you have unusual internationalization settings.
Or, actually, a better way to do this is to convert the current value to a string:
where d.date_begin >= format(dateadd(day, -7, getdate()), 'yyyy/MM/dd')
This is better because it is "sargable", meaning that SQL Server can use an index on the column if available.
SELECT * FROM tbl_name
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Don't manipulate d.date_begin. Making calculation on a column while comparing can give bad performance. You should manipulate getdate() to get the same format as d.date_begin. In this case it works because the format is yyyy/MM/dd - the comparison will give the same result as if both columns were date columns.
WHERE
d.date_begin >= convert(char(10),DATEADD(day,-7, GETDATE()), 111)

Calculating time difference between 2 dates in Teradata

I'm trying to calculate time difference (in months) between the current date, and the date the customer opened his account (only for those who joined in January 2012).
I try to use current_date and cast, but I think my problem is in my date field which is in the following format: dd/mm/yyyy
I'm working on Teradata.
Your help will be appreciated.
You can try like this:
SELECT CURRENT_DATE - CAST('2016-06-06' AS DATE) MONTH(4);
and if your date is in dd/mm/yyyy format then you can try like
SELECT CURRENT_DATE - cast(myDate as date format 'YYYY-MM-DD') MONTH(4);

SQL to query greater than today's date and time, with time defaulting to 12:00:00 AM

Format of the time/date in the table from which I am querying is the following: 11/12/2015 12:00:00 AM.
I would like to be able to query if the date is greater than or equal today, but look for results as the above format. Using getdate() returns the current specific time. Also, the specific format includes two spaces between the date and time.
Thanks for you help.
You can remove the time from consideration as follows:
WHERE CAST(YourDateTime AS DATE) >= CAST(getdate() AS DATE)

How to find the value from with in dates

here below is my table data,now i want to get the local tax value as on today,how to get the in between date values,i expected output should be 5.5
Category LocalTax LocalStartDate LocalEndDate
Frames 5 01-01-2011 31-12-2011
Frames 5.2 01-01-2012 31-12-2012
Frames 5.3 01-01-2013 31-12-2013
Frames 5.5 01-01-2014 14-04-2014
With this statement you get the tax valid for the current day
select LocalTax
from YourTaxTable YTT
where YTT.LocalStartDate <= getdate( )
and YTT.LocalEndDate > getdate( )
This will work for SYSDATE. You might want to adjust it according to date format you are using:
SELECT LocalTax
FROM table_name
WHERE SYSDATE BETWEEN LocalStartDate AND LocalEndDate;
In SQL Server , todays date and current time is found using GETDATE() function.
To strip time component from GETDATE() for comparing with dates cast GETDATE() to DATE.
Also your localstart and endadte are in dd-mm-yyyy format so you need to change date value returned from getdate() to same format i.e dd-mm-yyyy for correct comparison.
SELECT LocalTax
FROM tableName
WHERE CONVERT(varchar(12), CAST(getdate() as DATE), 103)
BETWEEN LocalStartDate AND LocalEndDate