Yoon.s

[mssql][oracle] 문자열 합치기 - concat, || 본문

백/DB

[mssql][oracle] 문자열 합치기 - concat, ||

yo_onHJ 2023. 3. 7. 22:05

SQL Server 

보통 문자열 합칠 때 concat 함수 or  더하기(+) 연산자 사용

concat 함수 사용을 권장

  • 입력된 값을 문자형으로 변환 후, 합치는 작업 진행 
  • => 숫자와 문자가 함께 입력되어도 오류 발생 X (더하기 연산자는 문자+숫자 오류 발생)
  • 문자 파라미터는 254개 까지 입력이 가능

concat_ws : 구분자로 합치기

select concat('hello', 'apple', 123) as str1;   -- helloapple123
select concat_ws(',', 'hello', 'apple', 123) as str2;   -- hello,apple,123
select concat(player_id, ',', player_name) as '이름' from player;	-- 결과 아래

 

 

*저 사이가 엄청 빈거는 player_id를 char(7)을 줘서 고정값이 생겨버림.

 

 

Oracle

concat 함수 혹은 || 을 사용함 . 

실무에서는 ||을 더 많이 사용함. 

select 'Hello ' || 'world' as st from player2;  
select concat('Hello ', 'world') as st from player2;
select PLAYER_ID || ',' ||PLAYER_NAME from player2;

st의 결과(1,2번째 결과 동일)
str 결과

 

' > DB' 카테고리의 다른 글

공백제거: TRIM, LTRIM, RTRIM  (0) 2023.03.07
[mssql] 시작하기  (0) 2023.03.03
mysql에서 발생했던 오류들 정리  (0) 2021.01.08
[mysql] Access denied for user ''@'localhost' 오류  (0) 2020.10.03
[mysql] mariaDB 시작하기  (0) 2020.10.01
Comments