hw to write sql conditions in select query [Resolved]

Posted by Oswaldlily under Sql Server on 3/10/2014 | Points: 10 | Views : 1678 | Status : [Member] | Replies : 3
Vlsino type
123A X
345T X
678Y Y
980T Y

I have written sql queries using join and made these output..

In this,If type is "X" ,vlsino shd be left blank.
If type is "Y",vlsino shd contains values like following


Vlsino type
X
X
678Y Y
980T Y




Responses

Posted by: Bandi on: 3/10/2014 [Member] [MVP] Platinum | Points: 50

Up
1
Down

Resolved
SELECT CASE WHEN TYPE= 'Y' THEN Vlsino 
WHEN TYPE= 'X' THEN ''
END as Vlsino,
Type
FROM TableName


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 3/10/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Try the below one:-

DECLARE @Table TABLE( 
Vlsino varchar(30) NOT NULL,
type varchar(30) NOT NULL
);

INSERT @Table values('123A','X')
INSERT @Table values('345T','X')
INSERT @Table values('678Y','Y')
INSERT @Table values('980T','Y')


SELECT CASE [type] WHEN 'X' THEN 'X' ELSE Vlsino END 'Vlsino', CASE [type] WHEN 'X' THEN '' ELSE [type] END 'type'
FROM @Table


Happy Coding..

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saranpselvam on: 4/4/2014 [Member] Starter | Points: 25

Up
0
Down
-- 1
create table test3 (number int,cosde varchar(10))
-- 2
insert into test3
select 1000,'X'
union
select 4000,'X'
union
select 3000,'Y'
union
select 1000,'Y'
union
select 5000,'z'

-- 3
select * from test3

-- 4

select distinct cosde,
case when cosde ='X' then Null else number end number
from test3

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response