Need a help to solve an SQL Server Query regarding a Date Time condition - sql-server-2008-r2

Please can anyone help/suggest me to solve the following condition which is inside one of my SELECT queries
ExamDate >= CONVERT(date, getdate())
AND ExamEndTime >= CONVERT(VARCHAR(8),GETDATE(),108)
Where I need to check the second condition when only the ExamDate is equal to getdate(), if ExamDate is > than getdate() I need to ignore the second condition.

ExamDate > CONVERT(date, getdate())
or (ExamDate = CONVERT(date, getdate()) AND ExamEndTime >= CONVERT(VARCHAR(8),GETDATE(),108))

Use DateDiff when compairing date objects
DATEDIFF(day,ExamDate, getdate()) >= 0
OR (DATEDIFF(hh,ExamEndTime,GETDATE()) >= 0 AND DATEDIFF(day,ExamDate, getdate())=0)

Related

PostgreSQL (WITH RECURSIVE)

I have a query that returns all dates between 01/01/2011 and 12/31/2041 (excluding Saturdays and sundays). It's working fine, but when I try to put the filter "TRIM(TO_CHAR(dt, 'DAY')) NOT IN ('SATURDAY', 'SUNDAY')" in the WITH, It only returns one row. Any suggestions? Please keep in mind that i'm brand new to PostgreSQL.
WITH RECURSIVE bd(dt,rn) AS
(
SELECT
TO_DATE('01/01/2011', 'MM/DD/YYYY') AS dt,
1 AS rn
UNION
SELECT
dt + 1, rn + 1
FROM
bd
WHERE
dt BETWEEN TO_DATE('01/01/2011', 'MM/DD/YYYY') AND TO_DATE('12/31/2041', 'MM/DD/YYYY') - 1
)
SELECT
dt, rn
FROM
bd
WHERE TRIM(TO_CHAR(dt, 'DAY')) NOT IN ('SATURDAY', 'SUNDAY')
No need for a recursive query. This can be done much easier using generate_series()
select t.dt::date
from generate_series(date '2011-01-01', date '2014-12-31', interval '1 day') as t(dt)
where extract(isodow from t.dt) not in (6,7);

Expression to query whether date is within last month?

I'm struggling to think up the easiest way to get all rows/records that were create within the last month. I can build a sortof convoluted one below but is there a simplier way?
SELECT
*
FROM
MyTable
WHERE
MONTH( createdAt ) >= MONTH( GET_DATE() ) - 1
AND
YEAR( createdAt ) = YEAR( GET_DATE );
The above would work but not for December records. Any advice how to simplify this and handle December created records?
DECLARE #startOfMonth date = DATEFROMPARTS( YEAR( SYSUTCDATETIME() ), MONTH( SYSUTCDATETIME() ), 1 )
SELECT
*
FROM
myTable
WHERE
createdAt >= #startOfMonth
Or inline it (SQL Server should detect the RHS is constant so using a variable won't increase performance):
SELECT
*
FROM
myTable
WHERE
createdAt >= DATEFROMPARTS( YEAR( SYSUTCDATETIME() ), MONTH( SYSUTCDATETIME() ), 1 )
If you are looking for records WITHIN the last month I am assuming you mean the last 30 days this will work:
SELECT *
FROM myTable
where createdAt >= CAST(DATEADD(day,-30,GETDATE()) as date)
If you are actually looking for records that were in last month then this will work:
SELECT *
from myTable
WHERE createdAt >= CAST( DATEADD(month, DATEDIFF(month, -1, GETDATE()) - 2, 0) as date)

Get difference between two dates in Years

I am working with a table that has StartDate and EndDate fields. I need to find difference between then in years.
Example:
StartDate = 1/1/2017
EndDate = 12/31/2017
I expect Result = 1 for the date difference.
Also, I'd like to round it to nearest whole number.
Example:
StartDate = 1/1/2017
EndDate = 11/30/2017
I expect Result = 1 for the date difference.
Using datediff function, I am able to get the result, but it isn't rounding to nearest whole number.
Example query:
I am getting 6 years even though 65 months / 12 would be less than 5.5:
select (DATEDIFF(yy, '01/01/2016', '5/31/2021')
+ CASE WHEN abs(DATEPART(day, '01/01/2016') - DATEPART(day, '05/31/2021')) > 15 THEN 1 ELSE 0 END)
select (DATEDIFF(mm, '01/01/2016', '05/31/2021')
+ CASE WHEN abs(DATEPART(day, '01/01/2016') - DATEPART(day, '05/31/2021')) > 15 THEN 1 ELSE 0 END)
DECLARE #startdate DATETIME = '1-1-2017',
#enddate DATETIME = '12-31-2018'
SELECT #startdate as StartDate, #enddate as EndDate,
DATEDIFF(YEAR, #startdate, #enddate)
-
(CASE
WHEN DATEADD(YEAR,
DATEDIFF(YEAR, #startdate,#enddate), #startdate)
> #enddate THEN 1
ELSE 0 END) 'Date difference in Years'
Use this code, I hope it will help you.
So far following query seems to be working okay. My mistake was I dividing by 12 instead of 12.0 for rounding to work correctly. Who knew! :
select
Round((DATEDIFF(mm, '01/01/2016', '07/1/2017')
+ CASE WHEN abs(DATEPART(day, '01/01/2016') - DATEPART(day, '06/30/2017')) > 15 THEN 1 ELSE 0 END) / 12.0, 0)
This may be a bit old but when using Oracle SQL Developer you can use the following. Just add your Dates below. I was using DateTime. This was used to get years between 0 and 10.
TRUNC((MONTHS_BETWEEN(<DATE_ONE>, <DATE_TWO>) * 31) / 365) > 0 and TRUNC((MONTHS_BETWEEN(<DATE_ONE>, <DATE_TWO>) * 31) / 365) < 10

Select all rows 7 days prior from a specific date

I want to get all rows 7 days prior from 01/09/2017
I know I can do
Load_DTM <= '2017-01-09' and Load_DTM >= '2017-01-02'
But can I not use DateAdd or DatePart?
i.e. DateAdd(dd, -7, '2017-01-09')
Load_DTM BETWEEN DATEADD(dd,-7,'2017-01-09') AND '2017-01-09 11:59:59'
ought to work.
You should be able to do exactly what you showed in your example:
SELECT * FROM Table WHERE DateField = DATEADD(DAY, -7, '2017-09-01')
Since running:
SELECT DATEADD(DAY, -1, GETDATE())
Gives you:
2017-03-15 19:26:29.833

T-SQL Date Function

I have a T-SQL script that runs every weekday. The script does a lookup for new customers in the past 24 hours, with the exceptionof Monday it will do a lookup in the past 72 hours (Friday through Sunday)
Select FirstName, LastName, CustomerID, Date
FROM Customers
WHERE
(
(
DATEPART(WEEKDAY, GetDate())=2 AND
DATEDIFF(DAY, Customers.Date, GetDate()) <= 3 AND
DATEDIFF(DAY, Customers.Date, GetDate()) >= 1
)
OR DATEDIFF(DAY, Customers.Date, GetDate()) = 1
)
I need to change this to do a lookup 30 days prior instead.
ANy ideas? Thanks.
WHERE DATEDIFF(DAY, Customers.Date, GetDate()) <= 30