SQL IF statement for todays date - sql

I'm trying to create a condition for my SQL, that shows a 1 if that row reflects today's day.
case when A.EntryDate = GETDATE() then '1' else '0' end as Today
That's accepted but it doesn't show anything but zeros. I've only worked with Access SQL and this one seems to dislike Date()
I've been looking all around for answers and I cannot seem to find one.

The GetDate() method return datetime. To compare today's date you need to convert datetime to date.
case when cast(A.EntryDate as date) = cast(getdate() as date) then 1 else 0 end

Related

Why is '=' operator not working on comparing Datetime?

I am trying to fetch records only of the current day. I have used the = operator to compare the date but it is not working, if I subtract 1 from the current date and then use >= operator then it works.
I am putting both queries here.
This code works:
1 = (
CASE WHEN ISNULL(DO.CREATEDON, '') >= GETDATE()-1
THEN 1
END
This code doesn't work:
1 = (
CASE WHEN ISNULL(DO.CREATEDON, '') = GETDATE()
THEN 1
END
Why is the latter code not working?
GETDATE() returns a datetime, so (for me), right now, using GETDATE() returns something like 2021-01-19T09:43:27.123. It's therefore very unlikely the value in your column CREATEDON is going to be the same exact time that GETDATE() returns, accurate to the nearest 1/300 of a second.
If your column CREATEDON is a date and time value, use inclusive date ranges:
WHERE DO.CREATEDON >= CONVERT(date,GETDATE())
AND DO.CREATEDON < CONVERT(date,DATEADD(DAY, 1, GETDATE())
If CREATEDON is a date, then just CONVERT the value of GETDATE() to a date:
WHERE DO.CREATEDON = CONVERT(date,GETDATE())
Note that there's no need for the ISNULL, as is the value of NULL it is by definition not the value of GETDATE(). Also, converting'' to a date and time value is a little odd; but it would convert to 1900-01-01, which again, is not today. Adding an ISNULL on DO.CREATEDON in the WHERE will only harm the performance; don't do it.

Using Cast to Convert DateTime to Date and select today's date

I'm trying to count the number of accounts opened on today's date in the SELECT Statement. I'm doing so with an IIF statement and using CAST to convert the DateTimeStamp to Date. That said, I'm having trouble figuring out where to date column (Open_Date) and how to check to see if it's is today's date. Would I place -1 right after the ) following as date or???
COUNT(IIF(CAST(GETDATE() AS date))), SHARE.MEMBER_NBR, null)) AS ALLNEWACCOUNTSTODAY
You can use case expression with Open_Date :
COUNT(CASE WHEN CONVERT(DATE, GETDATE()) = Open_Date THEN SHARE.MEMBER_NBR END) AS ALLNEWACCOUNTSTODAY
Can you provide the architectur please ?
Without it I would suggest that :
SELECT COUNT(*) FROM Accounts Where CONVERT(date,accountDate)=CONVERT(DATE,getdate());
It assumes that you have a column accountDate containing the date you added the account and that this column is into an accounts table.

SQL Server code to add time to date time if after certain time

I am NEW to SQL Server coding, so please be kind.
I am trying to look at a column and if the time is after 11am then I need to add 1 day to the date and display the new date. If prior to 11am, it doesn't need to add a day, but instead just show the date itself.
As you can see in the picture the "FDP Date" is adding a day no matter what time it shows (IE:lines 3 & 4). As if the time is before 11am it show not add any time.
Please let me know if you can help.
Thank you in advance,
Brian
Code:
SELECT
[ReceiptDate],
[DeptRcptDate],
CASE
WHEN DeptRcptDate >= '11:00:00'
THEN DATEADD (DAY, 1, DeptRcptDate)
WHEN DeptRcptDate < '11:00:00'
THEN (DeptRcptDate)
ELSE 'Unknown'
END AS "FDP Date",
[OutcomeLtrDate],
CASE
WHEN OutcomeLtrDate >= '16:00:00'
THEN (OutcomeLtrDate) + 1
WHEN OutcomeLtrDate < '16:00:00'
THEN (OutcomeLtrDate)
ELSE 'Unknown'
END AS "LDP Date"
Picture of (same) code.
You can use a CASE statement with DATEPART
declare #date datetime = '20160801 11:01:00'
select
case
when datepart(hour,#date) >= 11
then dateadd(day,1,#date)
else #date
end
'11:00:00' is 11am of the zero date (January 1, 1753). Obviously all your dates are greater than that.
If you want to compare times only, cast to time first:
case when cast(DeptRcptDate as time(0)) >= '11:00:00' then dateadd(d, 1, DeptRcptDate)
You're checking a datetime against a string contaiing a time. I'm not surprised it's giving unexpected results.
Get the time portion of your datetime with datepart(), and compare that.
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017

I am tring to add a year to a date if it is not before today.

I am tring to add a year to a date if it is not before today. So in the statement below I would like to display 4/20/2018
declare #StartDate datetime
set #StartDate='4/20/2015'
select case when dateadd(year,1,#StartDate)> GETDATE() then
dateadd(year,1,#StartDate) else dATEADD(year,1,(datepart(year,GETDATE()))) end
I've changed the else clause so it works out the difference between the start date and today's date +1 to alter make '4/20/2015' become '4/20/2018'
declare #StartDate date
set #StartDate='4/20/2015'
select
case
when dateadd(year,1,#StartDate)> GETDATE() then dateadd(year,1,#StartDate)
else DATEADD(year,(datepart(year,GETDATE())+1-datepart(year,#StartDate)),#StartDate)
end
Also if you are not using the time part of a datetime data type it's best practice to use date instead as it only requires 3 bytes of storage rather than 8 for datetime.
More on date here: https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-transact-sql
And a cheatsheet of sql data types here: https://sqlserverrider.files.wordpress.com/2013/04/pic116.png

How do I check whether the input date's DAY is the last day of the given month in SQL?

DECLARE #Date DATE = '2/28/2014'
I need to test whether the DAY in the DATE above is the LAST day of the month and it should work for all months in the year.
If it's true, then I need to return a true, if not, then false.
E.g.
SET #Date = '2/27/2014'
This should return FALSE.
SET #Date = '12/31/2014'
This should return TRUE.
I know you can manipulate this based on month but I'm just wondering whether there is an easy way to do it.
An easy way that works in almost any version of SQL Server is:
select (case when month(#date) <> month(dateadd(day, 1, #date))
then 'true' else 'false'
end)
That is, add a day and see if you are in the same month. This works for leap years, and Dec 31 and so on.
This will return 1 if its the last day of the month, and 0 otherwise
DATEDIFF(month,#date,#date+1)
As no one has given the 2012+ answer as an answer yet...
SELECT IIF(EOMONTH(#date) = #date, 'Yes','No')
As an aside you should use an unambiguous format for date literals such as ISO yyyy-mm-dd to avoid surprises when the code is executed under a login with different default options.