반응형
반응형

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 |

반응형
반응형

▶ 데이터 타입

SQL Server PostgreSQL
BIGINT 64-bit integer BIGINT
BINARY(n) Fixed-length byte string BYTEA
BIT 1, 0 or NULL BOOLEAN
CHAR(n), CHARACTER(n) Fixed-length character string, 1 ⇐ n ⇐ 8000 CHAR(n), CHARACTER(n)
DATE Date (year, month and day) DATE
DATETIME Date and time with fraction TIMESTAMP(3)
DATETIME2(p) Date and time with fraction TIMESTAMP(p)
DATETIMEOFFSET(p) Date and time with fraction and time zone TIMESTAMP(p) WITH TIME ZONE
DECIMAL(p,s), DEC(p,s) Fixed-point number DECIMAL(p,s), DEC(p,s)
DOUBLE PRECISION Double-precision floating-point number DOUBLE PRECISION
FLOAT(p) Floating-point number DOUBLE PRECISION
IMAGE Variable-length binary data, ⇐ 2G BYTEA
INT, INTEGER 32-bit integer INT, INTEGER
MONEY 64-bit currency amount MONEY
NCHAR(n) Fixed-length Unicode UCS-2 string CHAR(n)
NTEXT Variable-length Unicode UCS-2 data, ⇐ 2G TEXT
NUMERIC(p,s) Fixed-point number NUMERIC(p,s)
NVARCHAR(n) Variable-length Unicode UCS-2 string VARCHAR(n)
NVARCHAR(max) Variable-length Unicode UCS-2 data, ⇐ 2G TEXT
REAL Single-precision floating-point number REAL
ROWVERSION Automatically updated binary data BYTEA
SMALLDATETIME Date and time TIMESTAMP(0)
SMALLINT 16-bit integer SMALLINT
SMALLMONEY 32-bit currency amount MONEY
TEXT Variable-length character data, ⇐ 2G TEXT
TIME(p) Time (hour, minute, second and fraction) TIME(p)
TIMESTAMP Automatically updated binary data BYTEA
TINYINT 8-bit unsigned integer, 0 to 255 SMALLINT
UNIQUEIDENTIFIER 16-byte GUID (UUID) data CHAR(16)
VARBINARY(n) Variable-length byte string, 1 ⇐ n ⇐ 8000 BYTEA
VARBINARY(max) Variable-length binary data, ⇐ 2G BYTEA
VARCHAR(n) Variable-length character string, 1 ⇐ n ⇐ 8000 VARCHAR(n)
VARCHAR(max) Variable-length character data, ⇐ 2G TEXT
XML XML data XML

▶ 함수

SQL Server PostgreSQL
DATEADD Add an interval to datetime INTERVAL expression
ISNULL(exp, replacement) Replace NULL with the specified value COALESCE(exp, replacement)



출처: https://icodebroker.tistory.com/6429 [ICODEBROKER]

 

반응형
반응형

[MSSQL] 트랜잭션 로그가 꽉 차서 해결할때. 로그 축소

 

DB서버가 용량 부족이라서 확인해보니 로그파일이 37기가!!!

 

바로 트랜잭션 축소 해버림. 

DBCC SHRINKFILE   
(  
    { file_name | file_id }   
    { [ , EMPTYFILE ]   
    | [ [ , target_size ] [ , { NOTRUNCATE | TRUNCATEONLY } ] ]  
    }  
)  
[ WITH NO_INFOMSGS ]  



--https://docs.microsoft.com/ko-kr/sql/t-sql/database-console-commands/dbcc-shrinkfile-transact-sql?view=sql-server-ver15
-- DB log 축소 
 
USE [DataBase];

ALTER DATABASE [DataBase] SET RECOVERY SIMPLE;

-- 로그파일을 10MB로 축소

DBCC SHRINKFILE ([DataBase_Log], 10);

ALTER DATABASE [DataBase] 
SET RECOVERY FULL;
-- 로그 파일을 지정한 대상 크기로 축소

USE AdventureWorks2012;  
GO  

-- Truncate the log by changing the database recovery model to SIMPLE.  
ALTER DATABASE AdventureWorks2012  
SET RECOVERY SIMPLE;  
GO  

-- Shrink the truncated log file to 1 MB.  
DBCC SHRINKFILE (AdventureWorks2012_Log, 1);  
GO  

-- Reset the database recovery model.  
ALTER DATABASE AdventureWorks2012  
SET RECOVERY FULL;  
GO  

 

https://docs.microsoft.com/ko-kr/sql/t-sql/database-console-commands/dbcc-shrinkfile-transact-sql?view=sql-server-ver15 

 

DBCC SHRINKFILE(Transact-SQL) - SQL Server

DBCC SHRINKFILE(Transact-SQL)

docs.microsoft.com

 

반응형
반응형

 

CROSS APPLY 와 INNER JOIN

CROSS APPLY 와 INNER JOIN 은 동일한 의미를 지니게 됩니다.

아래 3개의 쿼리를 실행하고 나온 결과값을 확인해 보겠습니다.

SELECT * FROM FIRST_TABLE FT CROSS APPLY APPLY_FUNCTION(FT.C1)


SELECT * FROM FIRST_TABLE FT CROSS APPLY ( SELECT * FROM SECOND_TABLE ST WHERE ST.C1 = FT.C1 ) ST
 

SELECT * FROM FIRST_TABLE FT INNER JOIN SECOND_TABLE ST ON ST.C1 = FT.C1



OUTER APPLY 와 LEFT OUTER JOIN

OUTER APPLY 와 LEFT OUTER JOIN 역시 동일한 의미를 지니게 됩니다.

SELECT * FROM FIRST_TABLE FT OUTER APPLY APPLY_FUNCTION(FT.C1)



SELECT * FROM FIRST_TABLE FT OUTER APPLY ( SELECT * FROM SECOND_TABLE ST WHERE ST.C1 = FT.C1 ) ST



SELECT * FROM FIRST_TABLE FT LEFT OUTER JOIN SECOND_TABLE ST ON ST.C1 = FT.C1

반응형
반응형

update select 로 json data 변경하기

 

json_value, json_modify

-- update select 로 json data 변경하기 

 UPDATE AA  SET 
        AA.컬럼1 = JSON_MODIFY(BB.컬럼1,'$.must.title_inner',JSON_VALUE(BB.컬럼1, '$.must.title'))  
   from Table01 AS AA, Table01 AS BB 
  where BB.key = AA.KEY 
    

위 update문 옆에는 Alias로 배치하고 나머지 쿼리를 해보면 된다. 

 

일단 update 전에 select로 데이터 테스트는 필수!!!

 

잘못하면 훅! 간다~ 

반응형
반응형

기본 함수를 사용하여 JSON 데이터 유효성 검사, 쿼리, 변경(SQL Server)

SELECT id, json_col
FROM tab1
WHERE ISJSON(json_col) > 0 

https://docs.microsoft.com/ko-kr/sql/t-sql/functions/isjson-transact-sql?view=sql-server-ver15 

 

ISJSON(Transact-SQL) - SQL Server

ISJSON(Transact-SQL)

docs.microsoft.com

 

반응형

+ Recent posts