I have datetime field in DB, which stores invoice dates (format is 7/15/2014 12:00:00 AM)
Then I have two datetimepickers, for From date and To date.
I am trying to get it through SQL query
>Where TRDate between #DT1 and #DT2 Order By TRRef DESC
The problem is, If I select 7/15/2014 using datetime picker, it does not show the data for 15th. For this I need to select 14th.
Isn't selected date inclusive?
Thanks
Method1:
You should change input parameters like this
SET #DT1 = CONVERT(varchar(11), #DT1 ,101) + ' 00:00:00'
SET #DT2 = CONVERT(varchar(11), #DT2 ,101) + ' 23:59:57';
Then write search query
Or else use this
Method2
Where TRDate between #DT1 and DateAdd(DD,1,#DT2) Order By TRRef DESC
I don't know what RDBMS you are using but here's a simple tip. You can use >= and < for the comparison but you need to add 1 DAY on the second date.
This is not the correct syntax for adding a day in a date. It depends on what RDBMS you used.
WHERE TRDate >= #DT1 AND TRDate < (#DT2 + 1 DAY)
^ add 1 day
so here's what happened. Assuming you want to get the records between 7/1/2014 12:00:00 AM and 7/15/2014 12:00:00 AM
WHERE TRDate >= '2014-07-01 00:00:00' AND
TRDate < '2014-07-16 00:00:00' -- result for adding 1 day on 15th
Related
I have a simple query that needs to pull in all employees that were hired in the past 10 days, up until end of day yesterday at 11:59, excluding the current date, using Start & End date variables.
I know there's MANY ways of setting this date range.
What is the most efficient SQL server expression that can give you the start and end dates for the past 10 days, excluding the current date?
DECLARE #StartDate = ???
DECLARE #EndDate = ???
SELECT #StartDate, #EndDate
Desired outcome:
2020-04-02 00:00:00.000, 2020-04-11 23:59:59.000
The :59 schemes are a bad idea. Comparisons should be less than the (start of) next day in order to include the full previous day.
SELECT #StartDate = DATEADD(dd,-10,CAST(getdate() AS DATE)), #EndDate = CAST(getdate() AS DATE)
However if you still want it:
SELECT #StartDate = DATEADD(dd,-10,CAST(getdate() AS DATE)), #EndDate = DATEADD(ss,-1,CAST(CAST(getdate() AS DATE) AS DATETIME))
Hope this Code Works fine for your Case:
SELECT
#Start=CAST(CAST(GETDATE()-10 AS DATE) AS DATETIME),
#End=CONCAT(CAST(GETDATE()-1 AS DATE),' 23:59:59.000') ---
I need to check whether the current time is between two datetime column values.
And I only need to check the time is between the range and I don't want to check the date.
I know how to check a date is exists between a daterange like below
SELECT
*
FROM
Table1 T
WHERE
CAST(GETDATE() AS DATE) BETWEEN T.StartDate AND T.EndDate
We have stored start date and end date as folllows..
StartDate - 1900-01-01 08:00:00.000
EndDate - 1900-01-01 19:00:00.000
Is there something similar to this to check whether the time is exists in a date range?
if you only want to check time
SELECT *
FROM Table1 T
WHERE CAST(GETDATE() AS TIME) BETWEEN cast(T.StartDate as TIME) AND cast(T.EndDate as TIME)
Something like this I guess
declare #d1 datetime ='20171220 10:00'
, #d2 datetime = '20171220 12:00'
, #t time ='11:00';
select 'Yes' where #t between cast(#d1 as time) and cast(#d2 as time);
It worked when I tried like below
SELECT *
FROM Table1 T
WHERE CAST(GETDATE() AS TIME) BETWEEN CAST(T.StartDate AS TIME) AND CAST(T.EndDateAS TIME)
CONVERT() to TIME and then back to DATETIME and compare
WHERE CONVERT(DATETIME, CONVERT(TIME, GETDATE()))
BETWEEN T.StartDate AND T.EndDate
I am new to programming.
I want to count the column value from 6 Am to next day 6 AM. I tried using the below query but it gives the value for 12 AM to 12 AM. The Query is
SELECT (COUNT (COLUMN_NAME)) AS TOTAL
FROM TABLE_NAME
WHERE AREA = 1
AND TRIM(DATE_COLUMN) = TRIM ((SYSDATE)+6/24)
Here I cannot use date, this will update the value in Windowsforms app developed in delphi2007.
select count(column_name) as total
from the_table
where area = 1
and the_date_column
between trunc(sysdate) + interval '6' hour
and trunc(sysdate + 1) + interval '6' hour;
trunc(sysdate) sets the time part of the date to 00:00:00 when you then add 6 hours to that you get 06:00 (6 AM)
trunc(sysdate + 1) returns a date a midnight so if it's currently 2016-10-08 17:00:00 that will return 2016-10-09. If you add 6 hours to that, it's 06:00 the next day.
Not sure what you mean with "I can not use DATE". If made the big mistake to store your dates as VARCHAR you should change that and store them correctly in a DATE column. If for one reason you can't do that, you should convert that column to a date and compare dates, not strings:
and to_date(the_date_column, 'yyyy-mm-dd hh24:mi:ss') between ...
Make sure you specify the correct format mask to convert the varchar to a DATE.
Why you can't use date?
you may use a charcter string and change format to properly one.
variable = '2016-10-08'
to_Date(varable||' 06:00:00','yyyy-mm-dd HH24:MI:SS')
If you want just get values between today 6 o'clock and ensuing day 6 oclock
For example
SELECT (COUNT (COLUMN_NAME)) AS TOTAL
FROM TABLE_NAME
WHERE AREA = 1
AND DATE_COLUMN between trunc(sysdate,'DD') + 6/24 and trunc(sysdate+1,'DD') + 6/24
DECLARE #StartDate datetime = '2014-01-01';
DECLARE #EndDate datetime = '2014-01-02';
SET #BeginDate = DATEADD(HOUR, 9, #StartDate);
SET #EndDate = DATEADD(HOUR, 8, #EndDate);
SELECT
YourTable.ReadingDate,
YourTable.Hours,
YourTable.Data1,
YourTable.Data2,
YourTable.Data3
FROM
YourTable
WHERE
DATEADD(HOUR, YourTable.Hours, YourTable.ReadingDate) BETWEEN #StartDate AND #EndDate;
A shift in our plant is defined as starting at 4am and continues until 2 am on the following day.
At 3 am on a given day I want to get all the records for the previous shift.
The query below gets me the previous day upto now but also includes 12am to 2 am on the "previous previous" shift. How do i get the query to get data ONLY after 4am ?
select
*
from yourTable
WHERE TimeStamp >= dateadd(day,datediff(day,1,GETDATE()),0)
DECLARE #yesterday DATE = GETDATE()-1
DECLARE #time TIME = '04:00:00'
DECLARE #shiftstart DATETIME = CAST(#yesterday AS DATETIME) + CAST(#time AS DATETIME)
select
*
from yourTable
WHERE TimeStamp >= #shiftstart
The logic is to subtract 2 hours from the shift time and use the date portion. A simplistic implementation is:
where cast(dateadd(hour, -2, TimeStamp) as date) = cast(dateadd(day, -1, GetDate()) as date)
Sometimes, it is more efficient to do all the arithmetic on the "constant" (i.e. getdate()):
where TimeStamp >= dateadd(hour, 2, cast(GetDate() as Date))
Note: the appropriate function for this logic is dateadd() not datediff().
select rateperkg from K_FS_FeedMrpDetails where date = getdate()-1
I want to display rateperkg column before date values
Try this
get Yesterday
select rateperkg from K_FS_FeedMrpDetails where date = DATEADD(d,-1,GETDATE())
see this link for more details.
I think this will helps you
http://blog.sqlauthority.com/2008/08/29/sql-server-few-useful-datetime-functions-to-find-specific-dates/
This code will set time to zero.
declare #DaVal as datetime
--111 will return yyyy/MM/dd
--101 will return MM//dd/yyyy
set #DaVal = convert(datetime, convert(varchar(25), DATEADD(d,-1,GETDATE()), 111))
select rateperkg from K_FS_FeedMrpDetails where date = #DaVal
Try using DATEADD() function and cast it to Date to remove time part like below
SELECT rateperkg
FROM k_fs_feedmrpdetails
WHERE date = Cast(Dateadd(day, -1, Getdate()) AS DATE);
If your date column stores date and time then use the below condition
where date >=convert(date,getdate()-1)
and date < getdate()
else
where date = convert(date,getdate()-1)