Update a DateTimes's Time, but not the date - sql

MyTable:
id int
Date1 DateTime
Date2 DateTime
How can I set the time of Date2 to be the same as Date1 without affecting the date of Date2?

Update Date2 with only the difference in days between the two dates.
UPDATE MyTable
SET Date2 = DATEADD(DAY, DATEDIFF(DAY, Date1, Date2), Date1);
This will preserve the date portion of Date2.
UPDATE: This method essentially reconstructs Date2 by using Date1 as a reference and only adding the difference in days between Date1 and Date2 - preserving the TimeStamp from Date1.

This works from sql server 2008+
select cast(cast(date2 as date) as datetime) + cast(date1 as time) newdate2
from (select getdate() date2, cast('2012-01-01 20:00' as datetime) date1) a

SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, #Date2)) + DATEADD(day, -datediff(day, 0, #Date1), #Date1)

Related

Getdate() functionality returns partial day in select query

I have a query -
SELECT * FROM TABLE WHERE Date >= DATEADD (day, -7, -getdate()) AND Date <= getdate();
This would return all records for each day except day 7. If I ran this query on a Sunday at 17:00 it would only produce results going back to Monday 17:00. How could I include results from Monday 08:00.
Try it like this:
SELECT *
FROM SomeWhere
WHERE [Date] > DATEADD(HOUR,8,DATEADD(DAY, -7, CAST(CAST(GETDATE() AS DATE) AS DATETIME))) --7 days back, 8 o'clock
AND [Date] <= GETDATE(); --now
That's because you are comparing date+time, not only date.
If you want to include all days, you can trunc the time-portion from getdate(): you can accomplish that with a conversion to date:
SELECT * FROM TABLE
WHERE Date >= DATEADD (day, -7, -convert(date, getdate())
AND Date <= convert(date, getdate());
If you want to start from 8 in the morning, the best is to add again 8 hours to getdate.
declare #t datetime = dateadd(HH, 8, convert(datetime, convert(date, getdate())))
SELECT * FROM TABLE
WHERE Date >= DATEADD (day, -7, -#t) AND Date <= #t;
NOTE: with the conversion convert(date, getdate()) you get a datatype date and you cannot add hours directly to it; you must re-convert it to datetime.
Sounds like you want to remove the time. Correct? If so then do the following.
SELECT * FROM TABLE WHERE Date >= (DATEADD (day, -7, -getdate()) AND Date DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))

How to compare dates in SQL stored procedure?

I want to query the database within a stored procedure with the following SQL statement:
SELECT date1 FROM table INTO dates
WHERE date1 < CONVERT(VARCHAR(8), GETDATE(), 112)
AND date1 > CONVERT(VARCHAR(8), DATEADD(DAY, -4, GETDATE()), 112);
I get an error on the WHERE clause, what am I doing incorrectly?
The syntax erro is because the INTO is wrong.
1) But as #Jonathan has mentioned , the funcitions GETDAT, CONVERT, DATEADD
aren't native and not exists at INFORMIX database. Unless you have create it...
2) The "dates" should be a variable at your SPL.
3) The SELECT should return only 1 row....
SELECT date1 INTO dates FROM table
WHERE date1 < CONVERT(VARCHAR(8), GETDATE(), 112)
AND date1 > CONVERT(VARCHAR(8), DATEADD(DAY, -4, GETDATE()), 112);
This should be a valid Informix syntax:
SELECT date1 INTO dates FROM table
WHERE date1 < today
AND date1 > today - 4 units day ;
For more information about the syntax , please check at the IBM Knowledge Center
Assuming date1 is datetime type then Use Between
SELECT date1 INTO dates FROM table
WHERE date1 BETWEEN CONVERT(VARCHAR(8), DATEADD(DAY, -4, GETDATE()), 112) AND
CONVERT(VARCHAR(8), GETDATE(), 112);
if date1 is not datetime, then convert it to then Use Between
SELECT date1 INTO dates FROM table
WHERE CONVERT(VARCHAR(8), date1, 112) BETWEEN CONVERT(VARCHAR(8), DATEADD(DAY, -4, GETDATE()), 112) AND
CONVERT(VARCHAR(8), GETDATE(), 112);

SQL select where startdate is today's date

I am trying to write a query where the clause is when the start date for an employee is todays date.
select * from tbl_employees
where Startdate = getdate()
The issue is that Startdate is '2014-12-09 00:00:00.000'
and the function getdate is coming back with the date and time like '2014-12-09 08:25:16.013'
How can I write a query that only consider's the date?
You want just the date portion. The easy way is:
select *
from tbl_employees
where cast(Startdate as date) = cast(getdate() as date);
However, if you want to use an index, it is best not to have the column in a function call. So, this is better:
where (StartDate >= cast(getdate() as date) and StartDate < cast(getdate() + 1 as date))
select * from tbl_employees
where CONVERT(VARCHAR(10),Startdate,110) = CONVERT(VARCHAR(10),GETDATE(),110)
You can use to compare only DATE section not time..some thing like
IF CAST(DateField1 AS DATE) = CAST(DateField2 AS DATE)
OR
CONVERT(VARCHAR(10), GETDATE(), 112)
SELECT CONVERT (date, SYSDATETIME())
,CONVERT (date, SYSDATETIMEOFFSET())
,CONVERT (date, SYSUTCDATETIME())
,CONVERT (date, CURRENT_TIMESTAMP)
,CONVERT (date, GETDATE())
,CONVERT (date, GETUTCDATE());
/* Returned
SYSDATETIME() 2007-05-03
SYSDATETIMEOFFSET()2007-05-03
SYSUTCDATETIME() 2007-05-04
CURRENT_TIMESTAMP 2007-05-03
GETDATE() 2007-05-03
GETUTCDATE() 2007-05-04
*/
From: http://msdn.microsoft.com/en-us/library/ms188751.aspx
So using any of these will give you just the date

WHERE clause condition checking for NULL

let's say I have a table with columns ID, Date1 and Date2 where Date2can be NULL. I now want to have an SQL statement that ignores Date2 if it is NULL.
So I need something like that:
SELECT *
FROM
[myTable]
WHERE
ID = #someId
AND Date1 <= GETDATE()
AND Date2 >= GETDATE() IF Date2 IS NOT NULL
So I want to check if Date2 is not NULL and then compare it with the current date. If it is NULL then I just want to ignore it.
Hope my request is clear and understandable.
Cheers
Simon
AND Date1 <= GETDATE()
AND (Date2 IS NULL OR Date2 >= GETDATE() )
or
AND Date1 <= GETDATE()
AND COALESCE(Date2, GETDATE()) >= GETDATE()-- which means : if Date2 IS NULL say Date2 = GETDATE()
AND (Date2 >= GETDATE() OR Date2 IS NULL)
You could do it this way:
ISNULL(Date2, GETDATE()) >= GETDATE()
Give this sql a try.
SELECT *
FROM yourtable
WHERE ID = #someId
AND Date1 <= GETDATE()
AND (Date2 IS NULL
AND Date2 >= GETDATE());
SELECT *
FROM
[myTable]
WHERE
ID = #someId
AND Date1 <= GETDATE()
AND Date2 NOT IS NULL
AND Date2 >= GETDATE();

TSQL SELECT previous date's records

I want to select all records from a table Log where the DateAndTime field values (of type datetime) are for the day before today, whatever day it is.
So if today is 2011-06-08, I want to select all rows where DateAndTime is greater than or equal to 2011-06-07 00:00:00 and also less than 2011-06-08 00:00:00.
I'm guessing the potential pitfall here would be it's behaviour on the 1st day of the month, as obviously a date like 2011-06-00 is invalid, and should be 2011-05-31.
For SQL Server 2008 you can use this.
select *
from [log]
where cast(DateAndTime as date) = cast(getdate()-1 as date)
Pre 2008 you can use this
select *
from [log]
where DateAndTime >= dateadd(d, datediff(d, 0, getdate())-1, 0) and
DateAndTime < dateadd(d, datediff(d, 0, getdate()), 0)
Related on DBA: Cast to date is sargable but is it a good idea?
SELECT * FROM Log
WHERE DateAndTime >= DATEADD(DAY,-1, CAST(GETDATE() AS DATE))
AND DateAndTime < CAST(CAST(GETDATE() AS DATE) AS DATETIME)
This example assumes SQL Server:
select *
from log
where convert(varchar(8), DateAndTime , 112) = convert(varchar(8), getdate()-1, 112)
Essentially, convert the date to yyyymmdd (the 112 parameter) and then check it is equal to yesterday's date (getdate()-1), also converted to yyyymmdd.
Assuming SQL Server
declare #today date
set #today = GETDATE()
select * from Log where DateAndTime between DATEADD(dd, -1, #today ) and #today
It should include conditional operator and not between .
Otherwise it includes today's records as well.
Declare #today date
Set #today = GETDATE()
Select YourcolumnNames from log
Where DateAndTime >= DATEADD(dd, -1, #today ) and DateAndTime < DATEADD(dd, -1, #today )
Moreover, you should mention the column name and * should be avoided in the select statement. This can improve the performance