안녕하세요.
항상 눈팅으로 좋은 정보들을 가져가고 있습니다.
한가지 궁금한 사항이 있어서 이렇게 글을 쓰게 됩니다.
요지는 where 문에 case을 적용하는 사항입니다.
DB는 MS SQL 2012 버젼 입니다.
테스트 사항은 아래와 같습니다.
create table tb (
seq int identity(1,1) not null,
name nvarchar(100) not null
);
insert into dbo.tb
select 'A' union all
select 'A' union all
select 'B' union all
select 'C'
declare @type tinyint = 1;
select * from dbo.tb
where
case @type
when 0 then seq
when 1 then name
end =
case @type
when 0 then 1
when 1 then 'A'
end
@type을 0으로 하면 정상적으로 정보들이 출력이 됩니다.
그런데 @type을 1로 하면
varchar 값 'A'을(를) 데이터 형식 int(으)로 변환하지 못했습니다.
라는 에러 메세지들이 나타 납니다.
어떤 방식으로 해결을 하면 될까요?
고수님들의 답변 부탁 드립니다.
Comment 4
-
항해자™
2013.12.12 23:40
case @type when 0 then '1' ...... -
건우아빠
2013.12.13 13:09
데이타타입 우선순위를 참고 하세요.. http://technet.microsoft.com/ko-kr/library/ms190309.aspx
숫자 컬럼을 문자형으로 변환 http://www.sqler.com/590668
우선순위에 따라 문자형을 앞으로 순서를 바꾸시면 될듯 합니다.
case @typewhen 1 then namewhen 0 then seqend =case @typewhen 1 then 'A'when 0 then 1end -
104동 401호
2013.12.13 14:20
select * from dbo.tbwherecase @typewhen 0 then convert(varchar,seq)when 1 then nameend =case @typewhen 0 then convert(varchar,1)when 1 then 'A'end -
뽕남
2013.12.13 16:49
좋은 정보 감사 드립니다.
우선 순위는 생각을 하지 못하고 있었네요..
감사합니다.