반응형
반응형

 IF…ELSE IF…ELSE 

 

https://simplesqltutorials.com/if-elseif-else/

 

The IF…ELSE IF….ELSE Statement: Everything You Should Know

The IF...ELSE structure and the IF...ELSE IF...ELSE structure are the most basic SQL Server Decision Structures you should know.

simplesqltutorials.com

IF(<condition is true>)
BEGIN
<execute some code>
END
ELSE IF(<different condition is true>)
BEGIN
<execute some other code>
END
ELSE
BEGIN
<execute some other other code>
END
반응형
반응형

월의 마지막 날짜 구하기

SELECT EOMONTH('2020-09-09') AS result

SELECT DATEADD (DAY, -1, DATEADD (MONTH, DATEDIFF (MONTH, 0, '2020-09-09') + 1, 0)) AS result


SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, '2020-09-09') + 1, 0) - 1 AS result
반응형
반응형

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;
반응형
반응형

명시적 값을 테이블의 ID 열에 삽입할 수 있도록 합니다.

 

https://github.com/ngio/DB_query_mssql/blob/main/SET_IDENTITY_INSERT.sql

 

GitHub - ngio/DB_query_mssql: mssql and Query

mssql and Query . Contribute to ngio/DB_query_mssql development by creating an account on GitHub.

github.com

USE AdventureWorks2012;  
GO  
-- Create tool table.  
CREATE TABLE dbo.Tool(  
   ID INT IDENTITY NOT NULL PRIMARY KEY,   
   Name VARCHAR(40) NOT NULL  
);  
GO  
-- Inserting values into products table.  
INSERT INTO dbo.Tool(Name)   
VALUES ('Screwdriver')  
        , ('Hammer')  
        , ('Saw')  
        , ('Shovel');  
GO  
  
-- Create a gap in the identity values.  
DELETE dbo.Tool  
WHERE Name = 'Saw';  
GO  
  
SELECT *   
FROM dbo.Tool;  
GO  
  
-- Try to insert an explicit ID value of 3;  
-- should return an error:
-- An explicit value for the identity column in table 'AdventureWorks2012.dbo.Tool' can only be specified when a column list is used and IDENTITY_INSERT is ON.
INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel');  
GO  
-- SET IDENTITY_INSERT to ON.  
SET IDENTITY_INSERT dbo.Tool ON;  
GO  
  
-- Try to insert an explicit ID value of 3.  
INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel');  
GO  
  
SELECT *   
FROM dbo.Tool;  
GO  
-- Drop products table.  
DROP TABLE dbo.Tool;  
GO
반응형
반응형

탭   : char(9)

 

select * from 테이블명

where 컬럼명 = char(9)

 

엔터값 : char(10) + char(13)

 

select * from 테이블명

where 컬럼명 = char(10) + char(13)

 

 

Tab char(9)
Line feed char(10)
Carriage return char(13)

 

select 'a'+char(9)+'b'

select 'a'+char(13)+'b'

select 'a'+char(10)+'b'

 

엑셀출력 등을 하거나 화면에서 공백을 제거해야할 경우 공백이랑 엔터 같이 제껴야 한다.

탭까지 제낄 필요있으면 char(9) 추가

 

REPLACE( REPLACE(column,char(13),'' ), char(10), '')

반응형
반응형

정렬 조건 없이 순번을 매겨보자

 

 

일반적으로 순번을 지정할 때 ROW_NUMBER(), RANK, DENSE_RANK 등을 이용한다.

 

[MSSQL] ROW_NUMBER, RANK, DENSE_RANK 순위함수
 

 

SELECT ROW_NUMBER() OVER(ORDER BY 컬럼명) FROM 테이블명
SELECT RANK() OVER(ORDER BY 컬럼명) FROM 테이블명
SELECT DENSE_RANK() OVER(ORDER BY 컬럼명) FROM 테이블명

 

 

 

이렇게 정렬할 기준 컬럼을 지정 후 순위를 매긴다.

 

하지만 SELECT 해서 나오는 결과 그대로 순위를 매기려고 한다.

 

SELECT ROW_NUMBER() OVER(ORDER BY 1)
FROM 테이블명

 

다음과 같이 ORDER BY 1로 하면 될 거 같은데...! 안된다..

 

그렇다면 어떻게 처리해야할까?

 

 

 

첫번째 방법. 의미없는 변수 사용

 

DECLARE @row INT = 1 -- 의미 없는 변수
 
SELECT ROW_NUMBER() OVER(ORDER BY @row)
FROM 테이블명

 

이렇게 의미없는 변수를 선언해주고 해당 변수를 ORDER BY 절에 넣어준다.

 

 

 

 

두번째 방법. SELECT 1 사용

 

SELECT ROW_NUMBER () OVER(ORDER BY (SELECT 1))
FROM 테이블명

 

 

 

반응형

+ Recent posts