Need better current datetime syntax - sql-server-2008

This is current/old Access query I inherited.Need to retrieve the past year Case log. How can I write a better current date in Where clause?
Select (ID,DateLog,TimeLog)
From tblActiveCase
WHERE (((DatePart("yyyy",[DateLog])) Between DatePart("yyyy",Now())-1 And
DatePart("yyyy",Now())))
--My new StoredProc
Select (ID,DateLog,TimeLog)
From tblActiveCase
WHERE DateLog <= DATEADD(YEAR, -1, GETDATE())
is there a better way to write it?

declare #date date
set #date = DATEADD(YEAR, -1, GETDATE())
Select (ID,DateLog,TimeLog)
From tblActiveCase
WHERE DateLog >= #date

Related

SQL select true if date comparison

Is it possible to return a boolean value as a select field if a date is less than a certain time?
For example, something like:
SELECT true IF (mydate < NOW())
or something like that?
SELECT
CAST(CASE WHEN mydate< getdate() THEN 1 ELSE 0 END AS bit) AS mydate
FROM
MyTable WHERE (ID = 1)
-- Another approach might be
SELECT
CAST(COUNT(*) AS bit) AS mydate
FROM
MyTable
WHERE
ID = 1 AND mydate < getdate()
You can simply inject an inline IF statement as below:
SELECT CAST(IIF(mydate < GETDATE(), 1, 0) AS BIT)

How to get hour and minute only from a DateTime

I have let's say
'2015-01-16 13:50:00.000'
in my database.
How do I get the hours and minute only ?
13:50
in the end I want to make query like this
Update A set status = 1 WHERE endTime = current_time()
*endTime = field name of my sample above.
SELECT DATEPART(HOUR, GETDATE());
SELECT DATEPART(MINUTE, GETDATE());
MSDN: DATEPART
I want to get 13:50. is it the way
SELECT CAST(DATEPART(HOUR, GETDATE())AS VARCHAR(2)) + ':' +
CAST(DATEPART(MINUTE, GETDATE())AS VARCHAR(2))
I'm on SQL-Server 2005 which has no TIME datatype, but this should also work for you:
SELECT LEFT(DATEADD(MINUTE, 10, (CONVERT(TIME(0),GETDATE()))),5)
If you want to filter your records by the hour+minute part:
WHERE DATEPART(HOUR, endTime)= DATEPART(HOUR, GETDATE())
AND DATEPART(MINUTE, endTime)= DATEPART(HOUR, GETDATE())
Try the following
SQL Server 2000/2005
SELECT
CONVERT(VARCHAR(8), GETDATE(), 108) AS HourMinuteSecond,
CONVERT(VARCHAR(10), GETDATE(), 101) AS DateOnly;
GO
SQL Server 2008 Onwards
SELECT
CONVERT(TIME, GETDATE()) AS HourMinuteSeconds;
SELECT
CONVERT(DATE, GETDATE()) AS DateOnly;
GO
you can learn about it more here

convert int to date to add a dayn and convert to int back

I have int value with YYYYMM. I want to:
1. convert it into datetime
2. add one day DATEADD(Day, +1, #date)
3. convert it back into int
What's the easiest way to do this?
Here is a nice exercise and I hope it works out for you...
declare #date date
declare #newDate date
set #date = convert(date, '20100101')
set #newdate = DateAdd(dd, 1, #Date)
select #date
select #newdate
select convert(int, convert(varchar, #newdate, 112)) -- this is your final conversion back to int
If your initial int is, say, 201310 (October 2013), then what I think you want is this:
select convert(datetime, rtrim(201310 * 100 + 1))
The function RTRIM is a trick to convert int to string type.
The result is this:
2013-10-01 00:00:00.000
If you don't want to use RTRIM, the command below will get you the same result:
select convert(datetime, convert(char, 201310 * 100 + 1))

How to give date range in a month & Year format in where clause sql server

I am trying to give date range in where clause like below.. however its not giving correct o/p
declare #StartDate DATETIME, #EndDate DATETIME
set #StartDate='9/01/2011'
set #EndDate='1/30/2012'
Select * from mytable Where MONTH(WT.ToDate) >= MONTH(#StartDate) AND MONTH(WT.ToDate) <=MONTH(#Enddate)
AND YEAR(WT.ToDate)>= YEAR(#StartDate) AND YEAR(WT.ToDate) <=YEAR(#Enddate)
Please help
What is WT.ToDate, What WT is referring to?
it should be.
declare #StartDate DATETIME, #EndDate DATETIME
set #StartDate='9/01/2011'
set #EndDate='1/30/2012'
Select * from mytable as WT Where MONTH(WT.ToDate) >= MONTH(#StartDate) AND MONTH(WT.ToDate) <=MONTH(#Enddate)
AND YEAR(WT.ToDate)>= YEAR(#StartDate) AND YEAR(WT.ToDate) <=YEAR(#Enddate)
Firstly, note I've changed the format you've specified the dates in to avoid any risk of misinterpretation (now yyyyMMdd).
Secondly, try a clause like this:
declare #StartDate DATETIME, #EndDate DATETIME
set #StartDate='20110901'
set #EndDate='20120130'
Select *
from mytable
Where WT.ToDate >= #StartDate AND WT.ToDate < #EndDate
Note, this will not return rows for 30 Jan 2012, so just tweak your #EndDate as appropriate

Set variable in SELECT statement

I'm having the same problem as described here, unfortunately the solution does not work for MS SQL Server.
Does a similar syntax exist for MS SQL Server?
Note: my query is not as simple as in the example. I'd like to reuse DifferenceMinutes in the TooLateTime case.
DATEDIFF(MINUTE, DayOfWeekStopTime, GETDATE()) AS DifferenceMinutes,
CASE
WHEN DATEDIFF(MINUTE, DayOfWeekStopTime, GETDATE()) < 0 THEN NULL
ELSE CONVERT(varchar, GETDATE() - DayOfWeekStopTime, 108)
END AS TooLateTime
It's a little hard to tell exactly what you're trying to do, but I think this might be what you're looking for:
SELECT
DifferenceMinutes,
CASE
WHEN DifferenceMinutes < 0 THEN NULL
ELSE CONVERT(varchar, GETDATE() - DayOfWeekStopTime, 108)
END AS TooLateTime
FROM (
SELECT
DayOfWeekStopTime,
DATEDIFF(MINUTE, DayOfWeekStopTime, GETDATE()) AS DifferenceMinutes
FROM TableName
) X
You'll have to substitute your source table(s) for "TableName" in the FROM section of the inner query.
By rolling your calculated values into a nested select like this, you can refer to them by whatever name you give them in the outer query.
If you want to set variables for each of the values, you can do that as follows, but you'll need to make sure you're only returning one row from the query:
DECLARE #DifferenceMinutes int, #TooLateTime varchar(30)
SELECT
#DifferenceMinutes = DifferenceMinutes,
#TooLateTime = CASE
WHEN DifferenceMinutes < 0 THEN NULL
ELSE CONVERT(varchar, GETDATE() - DayOfWeekStopTime, 108)
END
FROM (
SELECT
DayOfWeekStopTime,
DATEDIFF(MINUTE, DayOfWeekStopTime, GETDATE()) AS DifferenceMinutes
FROM TableName
) X