IF there is 24 hours between dates CASE statment - sql

I am trying to create a case statment saying if 1 day or less has passed between my 2 date parameters then do this otherwise do this.....

Try this, however this only works in a query
case when datediff(hh,#Date1,#Date2) < 24 then.....
if it is in regular non query T-SQL just use an IF statement
IF datediff(hh,#Date1,#Date2) < 24
begin
-- stuff here
end
else
begin
-- stuff here
end

Explain "do this", because a CASE expression doesn't control flow.

SELECT CASE WHEN CAST(d1-d2 AS FLOAT) > 1 THEN '> 1 Day' ELSE '<= Day' END

Related

SQL simple case statement with formula

I tried adding in a THEN after the WHEN but don't know how to just make it show up as the result of the formula
CASE
WHEN (PLANAVAILDATE - CURRENT_DATE) =>280
ELSE ''
END AS Days Overdue
You don't need a case statement for this you can directly count the days of overdue
by using this
DATEDIFF(hour, PLANAVAILDATE, CURRENT_DATE)/24 AS 'Days Overdue'
if you can provide the full question I will give you exact solution

I want to put 0 instead of negative values from DATEDIFF

The table with the following structure is considered:
MEDICINE: (name, price, quantity, expiration_date );
I did
SELECT Name,
DATEDIFF(expiration_date, CURRENT_DATE)
AS days
FROM MEDICINE;
I got negative results from datediff, but I want to put 0 instead of the negative values. How can I make this? Any help?
Try this:
SELECT Name
, CASE
WHEN DATEDIFF(expiration_date, CURRENT_DATE) < 0 THEN 0
ELSE DATEDIFF(expiration_date, CURRENT_DATE)
END AS days
FROM MEDICINE;
Unfortunately you have to repeat the expression, but the engine should be smart enough to only calculate it once.
You can use GREATEST() in combination with a - because DATEDIFF() doesn't exist in PostgreSQL:
SELECT Name,
GREATEST(expiration_date - CURRENT_DATE, 0)
AS days
FROM MEDICINE;
This should work for you.
GREATEST(0, DATEDIFF(expiration_date, CURRENT_DATE))
Read about these conditional operators here. Spend some time reading about the suite of functions and operators in your SQL language: it's time well spent.
You can do it using SQL case.
This should work:
SELECT Name,
CASE WHEN DATEDIFF(expiration_date, CURRENT_DATE) > 0 THEN DATEDIFF(expiration_date, CURRENT_DATE)
ELSE 0
END
AS days
FROM MEDICINE;
You can read more about it here: https://www.w3schools.com/sql/sql_case.asp

How to assign a value to a date depending on it in PostgreSQL?

I'm using PostgreSQL and want to create a function and use it in the same query. The function that I want to create should do the same as the following python function:
def get_season(dia):
season = (abs(dia.year) % 100) + (1 if dia.strftime('%m-%d') >= '10-01' else 0)
return season
Giving a datetime, the function returns the last two digits of its year, or the previous one plus 1 if the date is greater than October the first, for example:
input = '2017-3-5' -> output = 17
input = '2019-11-1' -> output = 20
The problem is that I don't know what functions use for doing that in PostgreSQL.
Currently, I'm using the following code, but throw errors:
CREATE FUNCTION get_season(dia, datetime) RETURNS integer AS $$
BEGIN
MOD(EXTRACT(year FROM dia), 100) +
CASE WHEN EXTRACT(MONTH FROM dia) >= 10 THEN 1 ELSE 0 END;
END $$
LANGUAGE PLPGSQL;
SELECT date_column, get_season(date_column)
FROM my_table
The error throw is: (psycopg2.errors.UndefinedObject) dia type doesn't exist
The following should do this:
create or replace function get_season(p_input date)
returns integer
as
$$
select case
when extract(month from p_input) > 9 then extract(year from p_input)::int + 1
else extract(year from p_input)::int
end % 100
$$
language sql
immutable;
The condition extract(month from p_input) > 9 checks if the date is in October or later and returns the next year's value.
However, I would strongly recommend not to use two-digit years.
The postgres EXTRACT function will pull part of a date out, so I'd thus imagine you can make something like:
MOD(EXTRACT(year FROM yourdate), 100) +
CASE WHEN EXTRACT(month FROM yourdate) > 9 THEN 1 ELSE 0 END
The first gets the date as two digits, the second gets 0 or 1 to add depending on the month
ps; I'm not sure I would call such a function "get_season", there being 4 seasons a year etc - perhaps "get_academic_year", but this is entirely contextual I'm sure.. Just a note of what an unknowing third party assumed when they saw the word "season"

MSSQL case statement - If the time is midnight then false, else true

I am looking to build a case statement that follows the logic below:
If the time of the date provided is midnight(00:00:00) then false(0), else true(1)
I am looking to present this in a view that lists a number of orders sent down to schedule delivery, where midnight is our default time (and since deliveries do not happen at midnight, this would mean it has not been scheduled yet, setting it to 0/false. This will be used as a condition to show either a red cross or green tick on a web interface.)
Any help is greatly appreciated.
Thanks in advance.
How about
SELECT CASE WHEN CAST([my-datetime] AS TIME) = '00:00:00.0000000' THEN 0 ELSE 1 END
FROM [my-table];
SELECT case WHEN CONVERT(nvarchar(10), myDate, 108)='00:00:00' then 0 else 1 END
statement FROM [my-table];
THIS SHOULD WORK
DECLARE #TBL TABLE (midnight DATETIME)
INSERT INTO #TBL VALUES
('2012-06-18T10:34:09'),('2018-09-25T10:54:31'),('2018-09-25T00:00:00'),
('2018-09-25T12:07:09'),('2017-05-06T00:00:00'),('2016-08-19T08:11:35')
SELECT
midnight,
CAST(midnight AS TIME),
CASE WHEN CAST(midnight AS TIME)='00:00:00' THEN 0 ELSE 1 END AS 'midnight_Col'
FROM #TBL

Not getting actual minutes in SQL DateDiff

I searched in different places and found below queries. I am using the following queries to get the actual minutes difference in SQL. The dates I provide are the same day. I need difference in minutes only but SQL is returning 35 instead of 5 minutes in the first query. And the second query return milliseconds.
SELECT DATEDIFF(MINUTE,GETDATE(), CONVERT(datetime,'2016-08-11 16:04:24'))%3600/60 AS MinuteDiff
SELECT datediff(minute,GETDATE(), CONVERT(datetime,'2016-08-11 16:04:24')) as MinutesDiff
What is missing. Please help.
I need to put a condition that if time is less than 20 minutes then
do this
else
do this
Updated:
The issue occurs when i use GetDate(). When I use a fix date it works fine
You need to place the GETDATE() after your datetime value, other wise in your case you will get the minutes in negative value.
SELECT DATEDIFF(MINUTE,CONVERT(datetime,'2016-08-11 16:04:24'), GETDATE()) AS MinuteDiff
The current GETDATE() is 2016-08-11 17:05:39.053, so it returns 61.
Then based on the value, using IF ... ELSE ... you can do your expected operation:
IF DATEDIFF(MINUTE,CONVERT(datetime,'2016-08-11 16:04:24'), GETDATE()) < 20
PRINT 'With in 20 mins'
ELSE
PRINT 'More than 20 mins'
Here is a working example of what your after...although you probably need to switch out the date components as appropriate for your usage.
select
case
when
(SELECT datediff(minute,GETDATE(), CONVERT(datetime,'2016-08-11 06:00:00'))) < 20 then
(select 'do this')
else
(select 'do something else')
end as answer
If you want minute span between two datetime, then your second one is enough.
SELECT datediff(n, CONVERT(datetime,'2016-08-11 16:04:24'),GETDATE()) as MinutesDiff
you can use CASE for your further
select
case when
datediff(n, CONVERT(datetime,'2016-08-11 16:04:24'),GETDATE()) < 20 then
`your code`
else
`your else code`
end minte
Hey sorry for the initial poor explanation.
I use something like the following frequently this will return a INT and decide if it's then you can do the logic on it, equal to, not equal less than greater than etc.
If it is true it will return a 1 or it is false a 0. You can get it to return columns or set it to a string.
Hope it helps
select
Case
When DateDiff(minute,[column],Getdate()) > 20 then 1 else 0
end as [alias]