SQL Server 2012 :: DATEDIFF Between Date For Bills

Sep 17, 2015

I would like to know how I can make a script in SQL Server you can determine the difference days with bills based on date.

I show you an example:

Invoice Date Difference Day Amount
0001 1/1/2015 0 500.00
0003 18/1/2015 17 600.00
0008 2/2/2015 15 300.00

It is with DATEDIFF, but I'm stuck with logic.

View 3 Replies


ADVERTISEMENT

SQL Server 2012 :: Turning Datediff Minutes Into Dd:hh:mm

Jul 7, 2015

I am going put in the whole query but my question is on changing a datediff minutes amount into DD:HH:MM.

Query is:
select c.APPLICANT_ID as [Applicant ID], aetc.EVENT_TYPE as [Event Type],
cast(aetr.CREATE_DATE as date) as [Registration Date],
cast(aetc.CREATE_DATE as date) as [C Creation Date],
datediff(mi,cast(aetr.CREATE_DATE as datetime),cast(aetc.CREATE_DATE as datetime)) as [time diff],

[Code] .....

I want this as dd:hh:mm rather than just minutes.

Currently this field is just minutes so a figure such as 20, depending on the time difference between the 2 dates.

I have tried the convert statement convert (char(5) ...........,108) within this but that's not working and I have used floor before to do this type of thing but not sure where that should go.

View 9 Replies View Related

SQL Server 2012 :: Using DateDiff To Work As Excel Network Days

Jan 2, 2015

So I am trying to work out the difference between today's date (GETDATE()) and a Target Date in a specific table (targetdate)

When I use the DATEDIFF function it is including non working days in the calculation (weekends and bank holidays). Although our date calandar table provided to us from a third party supplier will tell you the weekends, it does not tell you the bank holidays.

Luckily there is another table in the database called - ih_non_work_days.

The format of the date is "2014-12-25 00:00:00.000" for example in that table.

How do I using my "targetdate" and today's date calculate in days the differance - excluding the dates that exist in the ih_non_work_days database?

So for now my basic script looks like -

SELECT com.comm_reference AS 'Referance'
,com.current_task_target_date AS 'TargetDate'
, DATEDIFF(D,com.current_task_target_date,GETDATE()) AS 'Incorrect Date Calculation'
FROM [dbo].[em_communication] as com

View 2 Replies View Related

Fetch Last 2 Bills

Apr 2, 2015

I want to fetch all bills (except cancelled bills) for a given month and fetch the previous bill date.

This is to find out the frequency of a vehicle visit to a workshop.

Sample Data

Division,ChasNo,BillDt,Status
D1,C1,01/01/2015,Null
D1,C1,15/01/2015,Null
D1,C1,23/02/2015,Null
D2,C1,26/02/2015,Null
D2,C2,02/01/2015,Null
D2,C2,16/01/2015,Null
D2,C2,21/02/2015,Null
D1,C3,24/01/2015,Cancelled

Result Expected

D1,C1,23/01/2015,15/01/2015
D2,C1,26/02/2015,Null
D2,C2,21/02/2015,16/01/2015

What I tried is

Declare @Mh smallint,@Yr smallint
Select @Mh=2,@Yr=2015

Select Division,ChasNo,BillDt Into #CMBills from Service_Bills Where Month(BillDt)=@Mh and Year(BillDt)=@Yr and Status Is Null

Select SB.Division,SB.ChasNo,CMB.BillDt as CurBillDt,Max(SB.BillDt) as PreBillDt,Datediff(day,Max(SB.BillDt),Max(CMB.BillDt)) as DiffDays from #CMBills CMB
Left Join Service_Bills SB On SB.Division=CMB.Division and SB.ChasNo=CMB.ChasNo and SB.Status Is Null and SB.BillDt<CMB.BillDt
Group By SB.Division,SB.ChasNo,CMB.BillDt

But I'm not getting details for all the bills I fetched.

View 4 Replies View Related

Datediff (today - Date)

Jul 13, 2006

HelloI want to return the number of days between a date in the database and todaysomething likeSELECT user.fName,user.lName & " (" & (datediff(now - user.lastVisit)) & " )" FROM user I must return John Turner (38)where 38 are the days between last visit and nowthank you

View 2 Replies View Related

SQL Server 2012 :: How To Match Two Different Date Columns In Same Table And Update Third Date Column

May 30, 2015

I want to compare two columns in the same table called start date and end date for one clientId.if clientId is having continuous refenceid and sartdate and enddate of reference that I don't need any caseopendate but if clientID has new reference id and it's start date is not continuous to its previous reference id then I need to set that start date as caseopendate.

I have table containing 5 columns.

caseid
referenceid
startdate
enddate
caseopendate

[code]...

View 4 Replies View Related

SQL 2012 :: Datediff - Query Does Not Work With Yyyyddmm Format

Mar 26, 2014

I have two dates '2014-25-03 01:02:03' & '2014-26-03 01:02:03' (yyyyddmm).

I need the difference between two dates in hh:mm:ss format.

I tried following query

declare @mindate datetime='2014-03-25 01:02:03'
declare @maxdate datetime='2014-03-26 02:03:04'
select cast(
(cast(cast(@maxDate as float) - cast(@minDate as float) as int) * 24) /* hours over 24 */
+ datepart(hh, @maxDate - @minDate) /* hours */
as varchar(10))
+ ':' + right('0' + cast(datepart(mi, @maxDate - @minDate) as varchar(2)), 2) /* minutes */
+ ':' + right('0' + cast(datepart(ss, @maxDate - @minDate) as varchar(2)), 2) /* seconds */

This works fine but as you can see the date I have used in the query is in yyyymmdd format.

The query does not work with yyyyddmm format

The expected answer is =25:01:01.

View 5 Replies View Related

SQL Server 2012 :: Calculate Number Of Days Based On Computer System Date And Due Date Row

Mar 18, 2014

I have a query to run a report where the results has a column named “Due Date” which holds a date value based on the project submission date.Now, I need to add 4 columns named, “45 Days Expectant”, “30 Days Overdue”, “60 Days Overdue” and “90 Days Overdue”.I need to do a calculation based on the “Due Date” and “System (I mean default computer date) Date” that if “System Date” is 45 days+ to “Due Date” than put “Yes” in “45 Days Expectant” row.

Also, if “Due Date” is less than or equal to system date by 30 days, put “Yes” in “30 Days Overdue” and same for the 60 and 90 days.how to write this Case Statement? I have some answers how to do it in SSRS (Report Designer) but I want to get the results using T-SQl.

View 2 Replies View Related

Find Items And Datediff From Current Date And Print

Apr 4, 2012

What I want to do is I have a table checks which looks like-:

Items--Waitarea--Datecreated
1-------A1-------2011-10-12
1-------A1-------2011-10-13
1-------A1-------2011-10-14
1-------A1-------2011-10-15
1-------A2-------2011-10-16
1-------A2-------2011-10-18
2-------A2-------2011-10-19
2-------A2-------2011-11-17
2-------A2-------2011-12-17
3-------A2-------2012-01-17
3-------A2-------2012-01-18
3-------A3-------2012-01-19
3-------A1-------2012-01-20
4-------A2-------2012-01-21
4-------A2-------2012-01-22
4-------A2-------2012-01-31

What i need to find is items and datediff from current date to created date with respect to wait area. You will get more clear with example

For Eg. For items 1 the wait area changes from A1 to A2 at
2011-10-16 and remains A2 so i need to find the date differeence from
today date to 2011-10-16

For item 2 that wait area remains same for entire period that is
A2 so datediff from today date to 2011-10-19(last created date of that item)

For item 3 that wait area A2 then it becomes A3 then becomes A1
so need datediff from today to 2012-01-20

For item 4 that wait area A2 remains for the entire period
so need datediff from today-2012-01-21

The expected output wanted
items---daycount
1-----datediff(day,checks.datecreated,getdate()--here checks.datecreated will be 2011-10-16
2-----datediff(day,checks.datecreated,getdate()--here checks.datecreated will be 2011-10-19
3-----datediff(day,checks.datecreated,getdate()--here checks.datecreated will be 2012-01-20
4-----datediff(day,checks.datecreated,getdate()--here checks.datecreated will be 2012-01-21

View 9 Replies View Related

SQL Server 2012 :: Script To Specify Date Field To Use For Activity Date

Jun 2, 2014

I'm writing a view to check record counts in a table that has numerous datasets and therefore various "Activity Dates". Is it possible as part of the SQL statement to have a CASE statement for example so that it can identify the field to use as the activity date?

The field to use is being identified using a seperate table so at the moment I have CASE WHEN FieldToUse = '2' THEN MapCol ELSE '[Activity_Date]' END, where FieldToUse = '2' identifies the date field to use and the MapCol data is the field name to be used as the activity date.

Is this even possible?

View 3 Replies View Related

SQL Server 2012 :: Date Between In Procedure

Dec 19, 2013

I need procedure query for two date between fromdate and todate

alter procdure date
declare @Fromdate varchar(10)
declare @todate varchar(10)
as begin
select * from master
where convert(varchar(10),@Fromdate,121) between CONVERT(varchar(10),@Todate,121) and CONVERT(varchar(10),rr.Original_Date,121)
end

View 3 Replies View Related

SQL Server 2012 :: Add Month To A Date

Jun 3, 2014

I am trying to add month to a date. Here is my code

declare @CollectionDate date='10-28-2014'
select @CollectionDate
;WITH CTemp AS (
SELECT TransactionDate=CAST(@CollectionDate AS DATE) ,RemainingTransaction=1
UNION all

[Code] ....

It is working fine. But when I am giving date '10-30-2014' it shows me the error

Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.

I can understand the problem that it is for the month of February. But How do I overcome the situation?

View 3 Replies View Related

SQL 2012 :: How To Get Last Run Date On All Databases On One Server

Oct 23, 2015

I know how to get the last run date on all the databases on one serveri have a lot of servers and i was wanting a way of getting the last run date across all servers.

View 3 Replies View Related

SQL Server 2012 :: Format Output As Date

Feb 28, 2014

I have the following code and the result set is coming out as nvarchar. So, when I create a report in SSRS, it is not formatting as a date.

, CASE
WHEN isnull(cv2.Accepted,0)='True' AND cv2.Visit ='V2' AND cv2.StepNo='3' THEN 'Y'
ELSE CONVERT(varchar,[dbo].[fn_Get_WorkingDays] (co.PlannedGoLiveDt, -10))
END AS 'System Verified'

View 3 Replies View Related

SQL Server 2012 :: Modifying Date Range

Jun 27, 2014

I would like changing the date range to be -1 year from today until today

This is the current WHERE, AND

WHERE [entry].[Posting Date] >= DATEADD(YEAR, YEAR(GETDATE()) - 1900, 0)
AND [entry].[Posting Date] < DATEADD(YEAR, YEAR(GETDATE()) - 1899, 0)

View 2 Replies View Related

SQL Server 2012 :: Convert Integer To Date

Oct 16, 2014

In VBA, CLng(Now) will return the integer portion of a date CLng(Now) returns 41928, CDate(41928) then returns 10/16/2014. Is there something equivalent in SQL Server that will allow me to convert an integer value to a date?

In short, how can I convert a 100 year date to Gregorian (any format)?

View 3 Replies View Related

SQL Server 2012 :: Serial Date Conversion

Nov 4, 2014

I am in need of converting serial date to regular date ie...735510.40461 and only need the hours, minutes and seconds, I have used the examples I've seen on different forums,

DATEADD(d, -1, DATEADD(m, DATEDIFF(m, '1900-1-1', getdate()) + 1, '1900-1-1'))

View 9 Replies View Related

SQL Server 2012 :: DATE As Dynamic Table Name

Jan 13, 2015

I am trying to select data from table that have YYMM as table names, they are formatted table1410,table1411, table1412. I am trying to format it like this

declare @tablename60 varchar(50) = 'table' + SUBSTRING(CAST(DATEPART(YY,dateadd(yy, -1, getdate())) as varchar(4)),3,4) + SUBSTRING(CAST(DATEPART(MM,dateadd(mm, -1, getdate())) as varchar(2)),1,2)

But this is hard coding the YYMM, and I would like to have it pull 30,60,90 days fromthe first of the current month. I am having a bit of trouble formatting, how to accomplish this.

View 1 Replies View Related

SQL Server 2012 :: Date Range Calculation

Feb 3, 2015

I have some location assignment data that I need to convert. I need to know how long each account spent in a certain location for each month of it's overall startdate/enddate period.

E.g.
Account 1 stayed in USA for 31 days in January, and 15 days in February.
Account 1 stayed in UK for 13 days in February and 26 days in March.
Etc.

create table #temp(account int, loc varchar(10), startdate datetime, enddate datetime)
insert into #temp select 1,'USA','2014-01-01','2014-02-15'
insert into #temp select 1,'UK','2014-02-16','2014-03-26'
insert into #temp select 1,'AU','2014-03-27','2014-06-07'
insert into #temp select 2,'UK','2014-08-15','2014-09-01'
insert into #temp select 2,'AU','2014-09-02','2014-10-17'
select * from #temp
drop table #temp

View 6 Replies View Related

SQL Server 2012 :: Handling Odd Date Format

Aug 21, 2015

I need to handle date format "41981".

View 9 Replies View Related

SQL Server 2012 :: String To Date Without Time

Sep 15, 2015

I have string as '1-12-2012', i wanted to convert it into date as format '2012-12-01'. I used the function as cast('1-12-2012' as Date) it's working but for a set of values where Null occurs it gives error as conversion failed when casting date and/or time from character string.

View 5 Replies View Related

SQL 2012 :: Use Date Trunc Or Date Function To Select Date Range For Month On Month View

Jul 29, 2015

My goal is to select values from the same date range for a month on month view to compare values month over month. I've tried using the date trunc function but I'm not sure what the best way to attack this is. My thoughts are I need to somehow select first day of every month + interval 'x days' (but I don't know the syntax).In other words, I want to see

Select
Jan 1- 23rd
feb 1-23rd
march 1-23rd
april 1-23rd
,value
from
table

View 9 Replies View Related

SQL Server 2012 :: CTE To Return All Periods Between First And Last Transaction Date

Jan 21, 2014

I'm trying to create a cte to return a list of lots and every period between the first and last transaction date for each lot. I've gotten this far:

SELECTIntLotKey
,DATEPART(YYYY, StartDate)StartYear
,DATEPART(MM, StartDate)StartPeriod
,DATEPART(YYYY, EndDate)EndYear
,DATEPART(MM, EndDate)EndPeriod

[Code] ....

This gives me the following results:

IntLotKeyStartYearStartPeriodEndYearEndPeriod
271532013120135
28468201312201312
2846920131201312
2847020131201312
28472201312201312
593022013120131
593032013120131

Now what I need is something that looks like this:

LotKeyYearPeriod
2715320131
2715320132
2715320133
2715320134
2715320135

[Code] .....

Some lots may not have any transactions for some of the periods between the start and end dates but I need to report every period between the start and end period for each lot. I have a period table that I thought I could use but haven't come up with a way to get the results I'm after.

View 9 Replies View Related

SQL Server 2012 :: Window Function On Different Date Ranges?

Feb 5, 2014

I have a question regarding windowing functions. I have a sales order table with the columns "orderid", "customerid", "order_date" and "amount". I use the following query to get the amount of every customer as a additional column:

Select customerid,
orderid,
order_date,
amount,
SUM(amount) OVER (PARTITION BY customerid)
FROM sales_orders

My question is if there is a good way to add another column, which includes the SUM(amount) of the customerid, where the order_date > 2012-01-15 , something like this:

Select customerid,
orderid,
order_date,
amount,
SUM(amount) OVER (PARTITION BY customerid),
SUM(amount) OVER (PARTITION BY customerid WHERE order_date > 2012-01-15)
FROM sales_orders

I know, this is not a valid method, so do you know a way to achieve this? Can I maybe use CROSS APPLY or something like this? I know that I could use a subquery to get this, but is there maybe a way / a better way via window functions?

View 9 Replies View Related

SQL Server 2012 :: How To Add Minutes To A Date Within Working Hours Only

Sep 4, 2014

I need to be able to add minutes to a datetime value, which only cover working hours.

I have a holiday table as below:

Examples: (dd/MM/yyyy hh:mm)
Date..........Description
01/01/2015 New Years Day
26/12/2014 Boxing Day
25/12/2014 Christmas Day
25/08/2014 August Bank Holiday

Our Business hours are 08:00-18:00 Mon-Fri (unless the day is in the holiday table)

Start Date............Minute to Add......Expected outcome
01/09/2014 10:00........30...................01/09/2014 10:30
01/09/2014 17:00........65...................02/09/2014 08:05
29/08/2014 17:00........65...................01/09/2014 08:05
22/08/2014 17:00........65...................26/08/2014 08:05
31/08/2014 02:30........65...................01/08/2014 09:05
01/09/2014 19:00........65...................02/08/2014 09:05
01/09/2014 10:00........3005...............08/09/2014 10:05
22/08/2014 17:00........3005...............01/09/2014 17:05

I have tried to create a function to do this (fn_pp_AddMinutesWithinWorkingHours(@StartDate,@Minutes)) but I am unable to come up with a solution which factors in everything correctly.

View 1 Replies View Related

SQL Server 2012 :: Conversion Failed When Converting Date

Oct 30, 2014

I have been trying to convert datetime but keep getting this error(Conversion failed when converting date and/or time from character string.It works just fine if I don't use execute sp_executesql,.

<code>
DECLARE @tablename AS nvarchar(max)
DECLARE @SQLQuery AS NVARCHAR(MAX)
DECLARE @ParameterDefinition AS NVARCHAR(100)
DECLARE @startdate datetime

[code]....

View 5 Replies View Related

SQL Server 2012 :: Select Row Having Latest Backup Date

Nov 29, 2014

I have a select query that results all the dbs and their several backup dates×. Howover I only need the latest backup dates for each of the db.How can I write that in tsql?

View 5 Replies View Related

SQL Server 2012 :: Filling Gaps In Date Ranges?

Dec 10, 2014

See sample data below. I want hourly breakdown for the last X years, the month and day will be the same for each year.

SELECT '2013-12-10 04:00:00.000' as dt, 220.50 as amt UNION ALL
SELECT '2013-12-10 06:00:00.000' as dt, 24.50 as amt UNION ALL
SELECT '2013-12-10 07:00:00.000' as dt, 527.50 as amt UNION ALL
SELECT '2013-12-10 08:00:00.000' as dt, 28.50 as amt UNION ALL
SELECT '2013-12-10 11:00:00.000' as dt, 25.50 as amt UNION ALL

[Code] .....

-- expected result

SELECT '2013-12-10 00:00:00.000' AS dt, NULL AS Amt UNION ALL
SELECT '2013-12-10 01:00:00.000', NULL AS Amt UNION ALL
SELECT '2013-12-10 02:00:00.000', NULL AS Amt UNION ALL
SELECT '2013-12-10 03:00:00.000', NULL AS Amt UNION ALL
SELECT '2013-12-10 04:00:00.000', 220.50 AS Amt UNION ALL

[code].....

View 9 Replies View Related

SQL Server 2012 :: Date Table For Financial Year

Dec 23, 2014

I need to create a table which holds date information for our financial year.

I have all the dates between now and 2045 and the start of the week and the end of the week. What I also have is the first sunday of the previous week in the spreadsheet too.

Please see below attachment

What I need to autofill once I import these three dates into a database is the week and the month.

The difficulty surrounding the month is that, we start a new month on the FIRST Sunday of the month.

So dates 07/04/14 to 04/05/2014 would be month 1.

Month 2 would begin on 05/05/2014 as it is the day after the first Sunday of the month, and so on....Month 5 would start on the 04/08/14.

Need to script something that would automatically calculate the week and month for me on the basis on above, if I have the start date, end date and 1st sunday already in a table?

View 9 Replies View Related

SQL Server 2012 :: Get Previous Years Holiday Date?

Jan 6, 2015

Given this table;

DECLARE @table TABLE (HolidayDate DATE, HolidayName NVARCHAR(50))
INSERT INTO @table
( HolidayDate, HolidayName )
VALUES ('2012-01-01','New Years Day'),
('2012-01-16', 'MLK Day'),
('2012-02-20', 'Presidents Day'),

[code]....

How to get a result set that shows a new column called PreviousHolidayDate with the corresponding holidays last years date?

HolidayDateHolidayNamePreviousHolidayDate
1/1/2012New Years DayNULL
1/16/2012MLK DayNULL
2/20/2012Presidents DayNULL
4/6/2012Good FridayNULL
5/28/2012Memorial DayNULL

[code]....

View 4 Replies View Related

SQL Server 2012 :: Date Range Checking In Table

Mar 16, 2015

I have a table with EmployeeID, StartDate, and EndDate with a PK of EmployeeID, StartDate. How can I check to see that there's no overlap for StartDate and EndDate for a given employee? That is, on any given day there must only be 1 row for an employee where Getdate() is Between StartDate and EndDate. For an active employee their EndDate is set to 06/06/2079.

I've tried it using Row_Number() with Over() but am returning too many rows indicating overlap when none exists.

View 7 Replies View Related

SQL Server 2012 :: Just Get Month And Date Only In A Query Without Year?

Mar 18, 2015

This is what I have for a query but i would like to take the year off. is there a way i can just have the month and day without the year attached?

View 3 Replies View Related

SQL Server 2012 :: Date Sequence Missing Values?

Apr 27, 2015

Write the query that produces the below results. I'm not ale to join the two sets in a way so that it displays NULLs if no purchase was made on a given day for a particular product. I need NULLs or s so that it shows up correctly on my SSRS report.

-- declare @from DATE='2015-1-5',@to DATE='2015-1-10'

-- test data

;with testdata as(
SELECT 1 AS Id,'1/6/2014' AS Date, 21 As Amount UNION ALL
SELECT 1 ,'1/8/2014', 25 UNION ALL
SELECT 1 ,'1/9/2014', 30 UNION ALL
SELECT 1 ,'1/10/2014', 60 UNION ALL
SELECT 1 ,'1/5/2015', 3800 UNION ALL
SELECT 1 ,'1/6/2015', 7120 UNION ALL

[code]....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved