SQL date related - sql-server

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

Related

I want to calculate Week-Ending date for particular dates given in a separate column

I am currently working on SQL Server 2019. There is need to calculate week-ending date (every Sunday) for a given effective date in a column. I am trying to make it work using the below logic.
dateadd(dd,((datediff(dd,'17530107',GETDATE()+13)/7)*7)+7,'17530107') as Weekend_Date
Desired Output,
Effective Date ( this date is give): Week-Ending Date (this needs to be calculated)(desired output)
10/08/2019 10/13/2019
11/01/2019 11/03/2019
11/07/2019 11/10/2019
If Getdate() is the field from your database and Sunday is day of week 1 then this should work.
Select dateadd (dd,(7- datepart(dw,getdate()) + 1),cast(getdate() as date))

between query for dates not working as i want

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

SSRS date/time parameter

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)

Entity Converting all DateTimes to Jan 1

I'm using Entity to pull some data from a customer's SQL table and I want to filter it based on date. The customer stored the relevant month of each row as a 6-digit string in the YYYYMM format. My attempt to resolve this (because I can't change the column type to a datetime2) has been to create a SQL View that does the following to create a datetime2 column representing the month:
CONVERT(datetime2, MON.Month + '01') AS CoveredMonth
Then inside of .NET, I have two DateTime objects, yearStart and yearEnd, that represent January 1st, 2016 and January 1st, 2017, as well as the specific employee whose records I'm looking for. I have the following code to attempt to filter on this column:
IList<MonthlyRecord> monthlyRecords = m_LTContext.MonthlyRecords
.Where(r => r.EmployeeID == employee.ID && r.CoveredMonth >= yearStart && r.CoveredMonth < yearEnd)
.ToList<MonthlyRecord>();
When I place a break point to check what is returned to monthlyRecords I see the expected count of records. However, each record has a CoveredMonth set to January 1st, 2016. Running the same query in SQL I get an identical count of records again, but the appropriate dates: Jan 1 '16, Feb 1 '16, etc.
Is there an issue with Entity somehow mapping properties that are non-standard on a View? There a few hacks I have in my mind but I'd really like to resolve this "properly".
Figured it out - the View was using the EmployeeID as the primary key and not the MonthlyRecordID.

SSIS Using Derived column to reference column from same table

I am trying to import an xls spreadsheet into a table, and one of the columns needs to be derived from a different column in the same table. I would like to do this during the import phase rather then creating a SQL Task to do it after the import. I am horrible when it comes to creating expressions in SSIS, so this is probably an easy task - but I just can't get it right.
DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,columndata)+1,0))
That is the expression that I am trying to use.
You want the following:
DATEADD("S",-1,DATEADD("M",DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))+1,(DT_DATE)0) )
1) You need to use " to surround your dateparts.
2) You need to cast your 0 date to an explicit date type.
This expression takes the column dateserial number [dataDate] adds that number of days to the date -2
DATEADD("D",[dataDate],(DT_DATE)-2)
(apparently sql day 0 and excel day 0 are different by 2 days on my versions (sql2008r2 and excel 2007)) and finds the number of months from date 0 to that date,
DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))
then adds 1 to that number of months and adds it back to date 0
DATEADD("M",DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))+1,(DT_DATE)0)
(to get the beginning of the next month following the column date value), Then it subtracts 1 second from the date to get the last second of the month of the column date value in the datetime format.
DATEADD("S",-1,DATEADD("M",DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))+1,(DT_DATE)0) )

Resources