i am working on SQL query which take records between two dates "from" To "to" as show in below picture.my query working well but when i change "to" date from 24 to 23 then it does not display the record of 23 date,means last row not display.
my SQL query is given below:
select * from prescription_master where (pr.date between #from_date and #to_date) or (pr.date=#to_date)
i want that the record of 23 date also display when i select 23 date from "to" date picker.for this purpose i use "or (pr.date=#to_date)" in above query but it not working.how i can solve this.
You have to handle time part:
select *
from prescription_master
where (CAST(pr.date AS DATE) between #from_date and #to_date)
or (CAST(pr.date AS DATE) = #to_date)
-- this also will made a query non-SARGable
When you provide parameter the date is set to 23-06-2019 00:00:00 and you are comparing it with 23-06-2019 13:08:00
Related
I have a ssrs report that has a date/time parameter that allows the user to select the date when running report. how to I get it to exclude the time part of the date field.
currently the field in the db is a date/time field so when I run query
select count(*) from table where date <= #dateparameter
it is not including records where the time part of field is greater than 00.00.00
how can I ignore the time part so all records are returned for that date
The simplest (and probably best performance) solution would be to add a day to the date passed by the user amd change the <= to <:
select count(*) from table where date < DATEADD(DAY, 1, #dateparameter)
I've created a virtual table in SQL Server that has 28 days from the current date and each date has rows for time that range from 12-10 pm incremented by 15 min and another value to indicate that it's turned on/off for availability, so it would be something like this:
date time onoff
-------------------------------------------------
2015-04-08 12:00 1
2015-04-08 12:15 1
....continue until 22:00 then start next day
2015-04-09 12:00 1
..... continue for 28 days
I'd like to update the availability based on a query from another table which would return the date, start and end time...
So far I came up with this
update table1
set onoff = 0
where tbl1date in (select tbl2date from table2 where userid = 1)
The problem I'm having is adding in the between certain hours part of the equation and I'm not sure how to do something like this in SQL or how to even search for the answer based on not being able to word it properly...
Can someone help or point me in the right direction?
use a DATETIME, don't use separate DATE and TIME fields.
I think you should take a look at DATEDIFF (https://technet.microsoft.com/pl-pl/library/ms189794(v=sql.110).aspx) function.
Your where query could look like this:
update table1 set onoff = 0
where
DATEDIFF(minute, <MIN_RANGE>, tbl1date) >= 0 and
DATEDIFF(minute, tbl1date, <MAX_RANGE>) >= 0
How you calculate MIN_RANGE and MAX_RANGE depends on your table2 structure.
As suggested, if you have control over the structure, use datetime fields as they are easier to do the comparisons on. I'm going to assume you don't have control over the structure.
In order to compare the datetimes you need to create them from your separate date and times. You can either parse the time field for the hours and minutes and use DATEADD to add the appropriate offsets to the date, or you can use CONVERT to interpret a date time string as a date. Something like
CONVERT(datetime, SUBSTRING(CONVERT(varchar, tbl1date, 121), 1, 10) + ' ' + tbl1time, 121)
What this does is to convert the date to odbc cannonical format and throwaway the time part as it takes only the first 10 characters. Then it appends the time and interprets the whole string as a odbc cannonical datetime string. That format is yyyy-mm-dd hh:mi:ss.mmm. The hours are based on 24 hours. So if your times are in AM/PM format you're going to have to convert them.
If your other table has separate date and times you'd use a similar expression to combine them.
Once you have the datetimes you can do something like this
UPDATE table1
SET onoff = 0
WHERE <expression above> BETWEEN (SELECT min_value FROM table2) AND (SELECT max_value FROM table2)
I have a table which has list of some events with dates. I am trying to write a stored procedure that will return only the upcoming events.
I have written the following query in the stored procedure:
SELECT *
FROM Events
WHERE tDate >= (select CAST(GETDATE() as DATE))
But this is not returning correct result. This is also showing results that have dates less than current date. How to write a query that will return all the events that have date equal or greater than today's date.
Edit: Dates that have been entered on the table have the format yyyy/dd/mm and getdate() returns date in the format yyyy/mm/dd. I think this is causing the problem. Dates that have been entered into the table has been taken using jquery date picker. Any solution to this problem?
Not sure why you have an additional select
SELECT *
FROM Events
WHERE tDate >= CAST(GETDATE() as DATE)
your DATE data is incorrectly stored within Sql Server. When your application passes the string '2015-09-04' and you save that your date column, it is saved as 4th Sept 2015 and not 9th April 2015. Hence your query returns such rows as they are greater than GETDATE().
Example
DECLARE #D VARCHAR(10) = '2015-09-04'
SELECT CONVERT(VARCHAR(20),CONVERT(DATE,#D),109)
you need to fix your data and then use a CONVERT with style when saving dates in your table from application, using something like this. CONVERT(DATE, '20150409',112)
DECLARE #D VARCHAR(10) = '20150409'
SELECT CONVERT(VARCHAR(20),CONVERT(DATE,#D,112),109)
Refer these threads for more info:
Impossible to store certain datetime formats in SQL Server
Cast and Convert
I am pretty new to SQL and hope someone here can help me with the following:
I have a table in SQL Server with one column storing dates and I have a simple stored procedure to select data from this table.
Currently it fetches the date including hours and minutes.
How can I achieve that it fetches the date in format yyyy-mm-dd (without the hours and minutes) ?
My column is called "logDate" and my table is called "logTable".
Thanks for any help with this, Tim
If you only want to store the date, convert the column to a date type, not a datetime.
If you want to keep the date and time in the data, but just display the date
select convert(date, logDate) from logTable
If you want to return the date as a string, use convert
select convert(varchar(10), logDate, 120) from logTable
Hi all i want to take record from table named Tblbatch where batch starting date should be from augest 2007 to july 2010...
I want to fetch such records which came in between this two dates
Select * from Tblbatch where startDate between '01-08-2007' and '31-07-2010'
provided you have a datetime column "startDate"
Note : that using between includes both the dates specified.
If you want to avoid the dates either change the boundary dates to + - 1 respectively or use > and < conditions