반응형
반응형

DATEDIFF, DATEPART

SELECT DATEDIFF(DAY,   '2021-06-12', '2021-07-13') AS [일차이]
     , DATEDIFF(MONTH, '2021-06-12', '2021-07-13') AS [개월차이] 
     , DATEDIFF(YEAR,  '2021-06-12', '2021-07-13') AS [년차이]

 

LAG, LEAD

https://learn.microsoft.com/en-us/sql/t-sql/functions/lag-transact-sql?view=sql-server-ver16 

 

LAG (Transact-SQL) - SQL Server

LAG (Transact-SQL)

learn.microsoft.com

SELECT empno  
     , ename
     , job
     , sal
     , LAG(sal) OVER(PARTITION BY job ORDER BY job, sal) AS sal_prev
     , LEAD(sal) OVER(PARTITION BY job ORDER BY job, sal) AS sal_next
  FROM emp
 WHERE job IN ('MANAGER', 'ANALYST', 'SALESMAN')
 ORDER BY job, sal
 
 
 CREATE TABLE T (a INT, b INT, c INT);   
GO  
INSERT INTO T VALUES (1, 1, -3), (2, 2, 4), (3, 1, NULL), (4, 3, 1), (5, 2, NULL), (6, 1, 5);   
  
SELECT b, c,   
    LAG(2*c, b*(SELECT MIN(b) FROM T), -c/2.0) OVER (ORDER BY a) AS i  
FROM T;

https://learn.microsoft.com/en-us/sql/t-sql/functions/lead-transact-sql?view=sql-server-ver16 

 

LEAD (Transact-SQL) - SQL Server

LEAD (Transact-SQL)

learn.microsoft.com

CREATE TABLE T (a INT, b INT, c INT);   
GO  
INSERT INTO T VALUES (1, 1, -3), (2, 2, 4), (3, 1, NULL), (4, 3, 1), (5, 2, NULL), (6, 1, 5);   
  
SELECT b, c,   
    LEAD(2*c, b*(SELECT MIN(b) FROM T), -c/2.0) OVER (ORDER BY a) AS i  
FROM T;
반응형
반응형

Adding string to DATEDIFF(day,getdate(),enddate) as DayRemain how please?

 

select 'Days remain ' + convert(varchar(20),(DATEDIFF(day,getdate(),'29 Oct 2011 12:00:00'))) as 'Days Remain'

the output of above query will be 

| Days Remain     |

| Days remain 30 |

반응형
반응형

DateDiff 를 이용한 현재 일시 비교

 

<% dim nowtime_chk
   nowtime_chk = DateDiff("n", "2022년 01월 07일 00:00:00", now()) 

   if( nowtime_chk > 0 )then %>
         console.log(" 날짜 지났다.");
<%  else %>
         console.log(" 날짜 아직.");
<%
end if
%>
반응형
반응형
 datediff 구현

 

 


    function getDateDiff(date1,date2){
        var arrDate1 = date1.split("-");
        var getDate1 = new Date(parseInt(arrDate1[0]),parseInt(arrDate1[1])-1,parseInt(arrDate1[2]));
        var arrDate2 = date2.split("-");
        var getDate2 = new Date(parseInt(arrDate2[0]),parseInt(arrDate2[1])-1,parseInt(arrDate2[2]));

        var getDiffTime = getDate1.getTime() - getDate2.getTime();

        return Math.floor(getDiffTime / (1000 * 60 * 60 * 24));
    }

 

 

getDateDiff("2016-08-01","2016-07-21")

 

 

 

반응형

+ Recent posts