Skip to main content

Get the difference between two dates in years,months and days as a string from a SQL Server

Just use the function pass 2 date parameter & get your expected Year/Month/Day
SELECT  [dbo].[fnGetDateDiffAsYMD] ('01/01/2012','02/02/2014')as Duration  





 set ANSI_NULLS ON  
 set QUOTED_IDENTIFIER ON  
 go  
 --SELECT [dbo].[fnGetDateDiffAsYMD] ('01/01/2012','02/02/2013')   
 ALTER FUNCTION [dbo].[fnGetDateDiffAsYMD] (@FromDate AS DateTime,@ToDate AS DATETIME)  
 RETURNS VARCHAR(30)  
 AS  
 BEGIN  
      DECLARE @date datetime,  
                @tmpdate datetime,  
                @years int,  
                @months int,  
                @days int,  
                @exp varchar(30),  
                @mm int,  
                @experiance datetime  
 if (datediff(dd,@FromDate ,@ToDate)< 0) or (@FromDate='') or (@ToDate is null)  
    select @exp ='Invalid joining date'  
 else  
 begin  
    select @experiance=Dateadd(yy,Datediff(yy,@FromDate,@ToDate),@fromDate)  
    select @years=Datediff(yy,@FromDate,@ToDate) - (CASE  
 WHEN @experiance > @ToDate THEN 1  
 ELSE 0  
 END)  
      select @months=Month(@ToDate - @experiance) -1  
      select @days = Day(@ToDate - @experiance) - 1  
 if @years<=0 and @months<=0 and @days<=0  
      set @exp = '0';  
 else if @years<=0  
 begin  
      if @months>0  
 begin  
      if @days>0  
 begin  
      if @months>1  
 begin  
      if @days>1  
      set @exp= CAST(@months as varchar) + ' months' +' and ' + CAST(@days as varchar) + ' Days'  
 else if @days=1  
      set @exp= CAST(@months as varchar) + ' months' +' and ' + CAST(@days as varchar) + ' Day'  
 end  
      else if @months=1  
 begin  
 if @days>1  
      set @exp= CAST(@months as varchar) + ' month' +' and ' + CAST(@days as varchar) + ' Days'  
 else if @days=1  
      set @exp= CAST(@months as varchar) + ' month' +' and ' + CAST(@days as varchar) + ' Day'  
 end  
 end  
 else if @days<=0  
 begin  
 if @months>1  
      set @exp= CAST(@months as varchar) + ' months'  
 else if @months=1  
      set @exp= CAST(@months as varchar) + ' month'  
 end  
 end  
 else if @months<=0  
 if @days>1  
      set @exp = CAST(@days as varchar) + ' Days'  
 else if @days=1  
      set @exp = CAST(@days as varchar) + ' Day'  
 end  
 else if @years>0 and @months>0 and @days>0  
 begin  
  if @years>1  
 begin  
 if @months>1  
 begin  
 if @days>1  
  set @exp= CAST(@years as varchar) +' Years' + ', '+ CAST(@months as varchar) + ' months' +' and ' + CAST(@days as varchar) + ' Days'  
 else if @days=1  
      set @exp= CAST(@years as varchar) +' Years' + ', '+ CAST(@months as varchar) + ' months' +' and ' + CAST(@days as varchar) + ' Day'  
 end  
 else if @months=1  
 begin  
 if @days>1  
 set @exp= CAST(@years as varchar) +' Years' + ', '+ CAST(@months as varchar) + ' month' +' and ' + CAST(@days as varchar) + ' Days'  
 else if @days=1  
 set @exp= CAST(@years as varchar) +' Years' + ', '+ CAST(@months as varchar) + ' month' +' and ' + CAST(@days as varchar) + ' Day'  
 end  
 end  
 else if @years=1  
 begin  
 if @months>1  
 begin  
 if @days>1  
 set @exp= CAST(@years as varchar) +' Year' + ', '+ CAST(@months as varchar) + ' months ' +'and ' + CAST(@days as varchar) + ' Days'  
 else if @days=1  
 set @exp= CAST(@years as varchar) +' Year' + ', '+ CAST(@months as varchar) + ' months ' +'and ' + CAST(@days as varchar) + ' Day'  
 end  
 else if @months=1  
 begin  
 if @days>1  
 set @exp= CAST(@years as varchar) +' Year' + ', '+ CAST(@months as varchar) + ' month ' +'and ' + CAST(@days as varchar) + ' Days'  
 else if @days=1  
 set @exp= CAST(@years as varchar) +' Year' + ', '+ CAST(@months as varchar) + ' month ' +'and ' + CAST(@days as varchar) + ' Day'  
 end  
 end  
 end  
 else if @years>0 and @days>0 and @months<=0  
 begin  
 if(@years>1)  
 begin  
 if(@days>1)  
 set @exp = CAST(@years as varchar) +' Years' +' and ' + CAST(@days as varchar) + ' Days'  
 else if(@days=1)  
 set @exp = CAST(@years as varchar) +' Years' +' and ' + CAST(@days as varchar) + ' Day'  
 end  
 else if(@years=1)  
 begin  
 if(@days>1)  
 set @exp = CAST(@years as varchar) +' Year' +' and ' + CAST(@days as varchar) + ' Days'  
 else if(@days=1)  
 set @exp = CAST(@years as varchar) +' Year' +' and ' + CAST(@days as varchar) + ' Day'  
 end  
 end  
 else if @years>0 and @days<=0 and @months<=0  
 begin  
 if @years>1  
 set @exp = CAST(@years as varchar) +' Years'  
 else if @years=1  
 set @exp = CAST(@years as varchar) +' Year'  
 end  
 else if @years>0 and @days<=0 and @months>0  
 begin  
 if @years>1  
 begin  
 if @months>1  
 set @exp = CAST(@years as varchar) +' Years' +' and ' + cast(@months as varchar) + ' months'  
 else if @months=1  
 set @exp = CAST(@years as varchar) +' Years' +' and ' + cast(@months as varchar) + ' month'  
 end  
 else if @years=1  
 begin  
 if @months>1  
 set @exp = CAST(@years as varchar) +' Year' +' and ' + cast(@months as varchar) + ' months'  
 else if @months=1  
 set @exp = CAST(@years as varchar) +' Year' +' and ' + cast(@months as varchar) + ' month'  
 end  
 end  
 end  
 return @exp  
 END  

you can also use

 SELECT DATEDIFF(DAY, GETDATE(), GETDATE() + 1) AS DayDiff  
 SELECT DATEDIFF(MINUTE, GETDATE(), GETDATE() + 1) AS MinuteDiff  
 SELECT DATEDIFF(SECOND, GETDATE(), GETDATE() + 1) AS SecondDiff  
 SELECT DATEDIFF(WEEK, GETDATE(), GETDATE() + 1) AS WeekDiff  
 SELECT DATEDIFF(HOUR, GETDATE(), GETDATE() + 1) AS HourDiff  

Comments

Popular posts from this blog

C# run powershell script as administrator

Recently I was fetching a problem that I need to run a PowerShell script that will change TFS user Display name and SID. I was trying to run that script from C# that was not working due to TFS security update and TLS certificate. Using this code block I resolve the Issue. var newProcessInfo = new System.Diagnostics.ProcessStartInfo(); newProcessInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe"; newProcessInfo.Verb = "runas"; // Define Run as administrator newProcessInfo.Arguments = script; //Define your powershell script newProcessInfo.UseShellExecute = false; newProcessInfo.RedirectStandardOutput = true; // This will enable to read Powershell run output newProcessInfo.RedirectStandardError = true; Process proces = System.Diagnostics.Process.Start(newProcessInfo); proces.WaitForExit(); // I want to read the output string from powershell window StringBuilder output = new StringBuilder(); output.Append("Started"); while (!proces.St

ASP.NET MVC razor SAP Crystal report

Crete a new project: Add a aspx Master Page Create a new folder Reports and 2 sub folder crystal & crystalviewer Now add a web form page in crystalviewer  folder. Add the master page namespace in your web form page. MasterPageFile ="~/Views/Shared/ReportSite.Master" Replace your web form by this code < asp : Content ID ="Content1" ContentPlaceHolderID ="ContentPlaceHolder1" runat ="server">      </ asp : Content > Now go to design mode of your web form drag & drop the crystal report viewer in your web form. After that your page will be look look like this. Replace the code: < CR : CrystalReportViewer ID ="EmployeeList" runat ="server"   HasCrystalLogo ="False"     AutoDataBind ="True"   Height ="50px"   EnableParameterPrompt ="false" EnableDatabaseLogonPrompt

mvc razor textboxfor change event change another textboxfor value

Based on value of Weight, Rate , CNF & AWB it will change the value of Freight , TTLCNF anfd TTLFright . Freight= Weight*Rate; TTLCNF  = Weight*CNF; TTLFright=  Freight+ TTLCNF  + AWB; @Html.TextBoxFor(model => model.Weight, new { onChange="return GetWight(this);"}) @Html.TextBoxFor(model => model.Rate, new { onChange="return GetWight(this);"})/Kg @Html.TextBoxFor(model => model.Freight, new {disabled = "disabled" , @readonly = "readonly" ,onChange="return GetTTLFright(this);"}) @Html.TextBoxFor(model => model.CNFPK, new { onChange="return GetCNFPK(this);"}) @Html.TextBoxFor(model => model.TTLCNF, new {disabled = "disabled" , @readonly = "readonly",onChange="return GetTTLFright(this);" }) @Html.TextBoxFor(model => model.AWB, new { onChange="return GetTTLFright(this);"}) and script <script> function GetW