I have a datetime field and would like to select the records having a date less than or equal to today but ignoring the time part of it.
My datetime field is as below:
2019-05-17 13:31:15.900, 2019-05-16 13:32:17.277, 2019-05-24 15:20:03.823
I would like to fetch only the rows with datetime 2019-05-17 13:31:15.900 and 2019-05-16 13:32:17.277 ignoring the time part.
Any idea of how to do that in sql server?
Simply, just cast both, your column and todays date to DATE:
SELECT * FROM MyTable
WHERE CAST(dt AS DATE) <= CAST(GETDATE() AS DATE)
You may try casting GETDATE() to a date type, to compare against tomorrow at midnight:
SELECT *
FROM yourTable
WHERE dt < CAST(DATEADD(day, 1, GETDATE()) AS date);
-- earlier than tomorrow at midnight
The date when this answer was written was 17-June-2019, so the above query would return any record whose datetime is strictly less than 18-June-2019 at midnight. The latest record would therefore be '2019-06-17 23:59:59'.
I am running SQL Server 2008 and I have a datetime field, from this field I only want to extract the date and hour. I tried to parse out using datepart() but I think I did it incorrectly as when I attempted to string the two back together I got a number value. What I attempted was
Select Datepart(YEAR, '12/30/2015 10:57:00.000') +
Datepart(HOUR, '12/30/2015 10:57:00.000')
And what was returned was 2025 What I want to see returned is
12/30/2015 10:00:00.000
You could use DATEADD and DATEDIFF:
DECLARE #d DATETIME = '12/30/2015 10:57:00.000';
SELECT DATEADD(second,DATEDIFF(second,'1970-01-01',#d)/3600*3600 , '1970-01-01')
-- 2015-12-30 10:00:00
LiveDemo
How it works:
Get seconds difference from 1970-01-01
Divide by 3600 (integer division so the part after decimal point will be skipped)
Multiply by 3600 to get value back to full hours
Add calculated seconds number to 1970-01-01
With SQL Server 2012+ the neat way is to use DATETIMEFROMPARTS:
SELECT DATETIMEFROMPARTS(YEAR(#d), MONTH(#d), DAY(#d), DATEPART(HOUR, #d),0,0,0)
LiveDemo2
Let's say I have a table like this:
Event (eventID, StartDateTime, EndDateTime)
StartDateTime is datetime datatype
EndDateTime is datetime datatype
Now sample data could be like so:
EventID StartDateTime EndDateTime
-----------------------------------------------------------
1 2014-02-21 00:00:00.000 2014-02-23 23:59:59.000
2 2014-02-22 00:00:00.000 2014-02-24 23:59:59.000
I want to search what events are happening at 2014-02-23 00:00:00.000
SELECT
*
FROM
Event
WHERE
(StartDateTime <= '2/23/2013 00:00:00 AM')
OR (EndDateTime >= '2/23/2013 00:00:00 AM')
I have tried the above code but it doesn't return correct result.
Am I missing something? Can you tell me what I am missing?
You don't want OR, you want AND. You want events that started before or on the date you specified, and that end after the date you specified:
SELECT
*
FROM
Event
WHERE
StartDateTime <= '20130223'
AND
EndDateTime > '20130223'
Also, I'd seriously recommend that you start storing these date ranges as a semi-open interval, with an exclusive end date, if the time portion is important. It's a lot easier to compute exclusive end points, that read more cleanly:
INSERT INTO Event(EventID,StartDateTime,EndDateTime) values
(1,'2014-02-21T00:00:00.000','2014-02-24T00:00:00.000'),
(2,'2014-02-22T00:00:00.000','2014-02-25T00:00:00.000')
Which has the advantage that it's not (arbitrarily) excluding the last minute of the day, as your current ranges do.
use this query
SELECT * FROM Event
WHERE '2014-02-23 00:00:00.000' BETWEEN StartDateTime and EndDateTime
did you try BETWEEN?
DECLARE #EventTime DATETIME
SET #EventTime = '20140223 00:00:00'
SELECT *
FROM EVENT
WHERE #EventTime BETWEEN StartDateTime AND EndDateTime
Use Between operator in WHERE clause . It specifies a range to test.
Note BETWEEN operator is inclusive of boundary cases.
BETWEEN returns TRUE if the value of test_expression is greater than or equal to the value of begin_expression and less than or equal to the value of end_expression.
Here is my table. I need a query which returns the shift id for a specified time
How can I get the value?
shift_ID shift_From shift_To
1 2010-09-21 10:00:00.000 2010-09-21 18:10:00.000
2 2010-09-21 20:00:00.000 2010-09-21 05:00:00.000
Suppose I am giving 02:00:00 as input I need to get the shift ID as 1. How can I do this?
Try:
SELECT shift_ID
FROM time_shift
WHERE
DATEDIFF(hour, shift_From, shift_To) = 2 -- your input
See more about DATEDIFF on MSDN
The first argument is the time part you're specifying to DATETIFF (hour, minute, second).
If your input is strictly like 02:00:00 you need to parse it to determine what specify as the first argument.
To determine does the specified date belong between 2 others, use:
SELECT shift_ID
FROM time_shift
WHERE
CAST(shift_From AS TIME) < CAST(#input AS TIME)
AND
CAST(#input AS TIME) < CAST(shift_To AS TIME)
-- you can specify operators inclusiveness, i.e. <= >= etc
-- or
CAST(#input AS TIME) BETWEEN (CAST(shift_From AS TIME), CAST(shift_To AS TIME))
See more about TIME on MSDN
Im having a problem with derived columns in SSIS. When in SSMS i can set a column to have a default value using the following code:
(dateadd(month,datediff(month,(0),getdate())-(1),(0)))
and when data is entered into the database it will give it the timestamp of the previous month in the following format for example:
2010-09-01 00:00:00.000
This strangely is what im looking for and trying to duplicate/produce similar using the Derived Column DFT.
So far i have:
DATEADD("mm",DATEDIFF("mm",GETDATE(),GETDATE()) - 1,GETDATE())
which produces the month succesfully but the GETDATE() is not a correct replacement for the 0's in the original code.
Does the 0's in the original code signify the start date in SQL?
Any help would be much appreciated.
Regards,
Lee
your first code fragment is "flooring" the datetime to the month (making the date the 1st of the given month with no time) the "-1" makes it the previous month. All the extra unnecessary parenthesis in this code fragment give me a headache, here is the equivalent:
dateadd(month,datediff(month,0,getdate())-1,0)
This is how to floor a datetime to different units:
--Floor a datetime
DECLARE #datetime datetime;
SET #datetime = '2008-09-17 12:56:53.430';
SELECT '0 None', #datetime -- none 2008-09-17 12:56:53.430
UNION SELECT '1 Second',DATEADD(second,DATEDIFF(second,'2000-01-01',#datetime),'2000-01-01') -- Second: 2008-09-17 12:56:53.000
UNION SELECT '2 Minute',DATEADD(minute,DATEDIFF(minute,0,#datetime),0) -- Minute: 2008-09-17 12:56:00.000
UNION SELECT '3 Hour', DATEADD(hour,DATEDIFF(hour,0,#datetime),0) -- Hour: 2008-09-17 12:00:00.000
UNION SELECT '4 Day', DATEADD(day,DATEDIFF(day,0,#datetime),0) -- Day: 2008-09-17 00:00:00.000
UNION SELECT '5 Month', DATEADD(month,DATEDIFF(month,0,#datetime),0) -- Month: 2008-09-01 00:00:00.000
UNION SELECT '6 Year', DATEADD(year,DATEDIFF(year,0,#datetime),0) -- Year: 2008-01-01 00:00:00.000
ORDER BY 1
PRINT' '
PRINT 'Note that when you are flooring by the second, you will often get an arithmetic overflow if you use 0. So pick a known value that is guaranteed to be lower than the datetime you are attempting to floor'
PRINT 'this always uses a date less than the given date, so there will be no arithmetic overflow'
SELECT '1 Second',DATEADD(second,DATEDIFF(second,DATEADD(day,DATEDIFF(day,0,#datetime),0)-1,#datetime),DATEADD(day,DATEDIFF(day,0,#datetime),0)-1) -- Second: 2008-09-17 12:56:53.000
the second code fragment will not properly floor the datetime to the month, it will only move the date to the previous month and use the same day and time as the given datetime.
Beyond that I'm not sure what you are really asking.
here is a breakdown of what is happening in the OP's first code fragment:
select convert(datetime,0),'first datetime "0"'
select datediff(month,0,getdate()), 'months difference between the first datetime (1900-01-01) and given "getdate()"'
select datediff(month,0,getdate())-1, 'months difference between the first datetime (1900-01-01) and month previous to given "getdate()"'
select dateadd(month,datediff(month,0,getdate())-1,0), 'takes the first datetime (1900-01-01) and adds 1328 months onto that'
OUTPUT:
----------------------- ------------------
1900-01-01 00:00:00.000 first datetime "0"
----------- --------------------------------------------------------------------------------
1329 months difference between the first datetime (1900-01-01) and given "getdate()"
----------- --------------------------------------------------------------------------------------------------
1328 months difference between the first datetime (1900-01-01) and month previous to given "getdate()"
----------------------- --------------------------------------------------------------------
2010-09-01 00:00:00.000 takes the first datetime (1900-01-01) and adds 1328 months onto that
Here's what I think you may be looking for. It is an SSIS expression that gets the first day of the previous month for a given day (GETDATE() in the example).
DATEADD("mm",DATEDIFF("mm", (DT_DBTIMESTAMP)2, GETDATE()) - 1, (DT_DBTIMESTAMP)2)
I tried to simulate your SQL version of the expression, which determined the number of months between the 0 datetime and the current datetime. And, then it added the number of months to the 0 datetime.
It's not too important what the 0 datetime is, except that you wanted the 1st day of the month. In SQL the 0 datetime is 1900-01-01 00:00:00.000, so adding months automatically gives you the first day of the month. In SSIS expressions, the 0 datetime is 1899-12-30 00:00:00.000. Since you want the first day of a month, the expression above refers to the 2 datetime. So, in the expression (DT_DBTIMESTAMP)2 casts the number 2 to 1900-01-01 00:00:00.000.
month,0 makes it round down to the beginning of the month.
Can't you use your first expression if it does what you want? Or use the second expression but switch the two GETDATE()s you've added to 0. Do you get an error if you try that?
I think what you need in your derived column is this synatx:
DATEADD("mm",-1,(DT_DBTIMESTAMP)((DT_WSTR,4)DATEPART("YYYY",GETDATE()) + "-" + (DT_WSTR,2)DATEPART("mm",GETDATE()) + "-01"))