Required Query to get specific output
-
2012년 8월 21일 화요일 오전 11:54
I have a view which has structure like below
CountryCode TempRegistrationID PermRegistrationID Deleted
IND 0001-07730/00 N
USA 835094326035007 0835-0943260-35-007 N
NWZ H111200 NEvery country code will have tempRegistrationID, PerRegistrationID. I want to display RegistrationID which will be depend on following condition:
1. If user is having both tempRegistrationID, PerRegistrationID then it will show PerRegistrationID As RegistrationID
2. otherwise if it is having tempRegistrationId and not PerRegistrationID, then tempRegistrationId will be its RegistrationID.
This RegistrationId is temporary column where I want to display this values, its not present in my existing View.I am not able to get this resultset using case structure or by any other option.I want result as shown below. Can you please suggest the way to do this?"
RegistrationID
0001-07730/00
0835-0943260-35-007
H111200I am trying query as below:
select
r.PermRegistrationID,
r.TempRegistrationID,
case when (ISNULL(r.PermRegistrationID,'') ='') then r.TempRegistrationID
else r.TempRegistrationID end as RegistrationID
from myView r
where r.CountryCode IN( 'IND','NWZ','USA' ) and r.Deleted ='N'- 편집됨 t_deepak 2012년 8월 21일 화요일 오후 12:22
모든 응답
-
2012년 8월 21일 화요일 오후 12:11답변자If I understood correctly , look at COALESCE system function in the BOL.
Best Regards,Uri Dimant SQL Server MVP,http://sqlblog.com/blogs/uri_dimant/
-
2012년 8월 21일 화요일 오후 12:12
Use Case When in your select query appropriately.
EDIT: Added code snippet.Create Table T1 (Code varchar(3),TempRegID Varchar(100),PermRegID Varchar(100)) Insert into T1 Values('IND','0001-07730/00',NULL) Insert into T1 Values('USA','835094326035007','0835-0943260-35-007') Insert into T1 Values('NWZ','H111200','') Select Code,Case When LEN(LTRIM(RTRIM(PermRegID)))>0 Then PermRegID Else TempRegID End as RegID From T1
Please click the Mark as Answer or Vote As Helpful button if a post solves your problem or is helpful!
- 편집됨 Latheesh NKMicrosoft Community Contributor 2012년 8월 21일 화요일 오후 12:26 EDIT: Added code snippet.
-
2012년 8월 21일 화요일 오후 12:28
I have a view which has structure like below
CountryCode TempRegistrationID PermRegistrationID Deleted
IND 0001-07730/00 N
USA 835094326035007 0835-0943260-35-007 N
NWZ H111200 NEvery country code will have tempRegistrationID, PerRegistrationID. I want to display RegistrationID which will be depend on following condition:
1. If user is having both tempRegistrationID, PerRegistrationID then it will show PerRegistrationID As RegistrationID
2. otherwise if it is having tempRegistrationId and not PerRegistrationID, then tempRegistrationId will be its RegistrationID.
This RegistrationId is temporary column where I want to display this values, its not present in my existing View.I am not able to get this resultset using case structure or by any other option.I want result as shown below. Can you please suggest the way to do this?"
RegistrationID
0001-07730/00
0835-0943260-35-007
H111200I am trying query as below:
select
r.PermRegistrationID,
r.TempRegistrationID,
case when (ISNULL(r.PermRegistrationID,'') ='') then r.TempRegistrationID
else r.TempRegistrationID end as RegistrationID
from myView r
where r.CountryCode IN( 'IND','NWZ','USA' ) and r.Deleted ='N'
What if first expression is empty string -
2012년 8월 21일 화요일 오후 12:36답변자
Then you can REPLACE it with NULL (REPLACE(col,'',NULL)) or check in CASE EXPRESSION for >''.Best Regards,Uri Dimant SQL Server MVP,http://sqlblog.com/blogs/uri_dimant/
- 답변으로 표시됨 amber zhangModerator 2012년 8월 28일 화요일 오전 6:06

