0

I have a join query, I want Replace a NULL value with a string if is null. If it is not NULL, replace with a null string.

select distinct Code= R_Schedule.FoodId,Name= FoodName + ' = price ' +(CONVERT( varchar(50),R_Foods.FoodPrice) + ISNULL(str(R_Schedule.dayId ),' - public-'))    from R_Foods join R_Schedule on R_Foods.FoodId=R_Schedule.FoodId where R_Schedule.DayId=6 or DayId is null

please this this Image : https://i.sstatic.net/Kv81f.png

Important section of my query is :

 ISNULL(str(R_Schedule.dayId ),' - public -'))

This section if it is null return 'public' else if it is not null return column's id. I don't want return id if it is not null.

6
  • 1
    which database you are using ? coz its DB specific Commented Mar 2, 2015 at 6:45
  • Case When Then would work Commented Mar 2, 2015 at 6:46
  • @JenishRabadiya Can you post your answer .please Commented Mar 2, 2015 at 6:46
  • @JenishRabadiya I don't know how work with case . Commented Mar 2, 2015 at 6:50
  • @PsarTak can you please post your table schema? Commented Mar 2, 2015 at 6:52

1 Answer 1

2

You can use the CASE WHEN. When dayId is null then it will return - public- Else it will concat with '' Empty string which you can use the other value to set if you want.

select distinct 
   Code = R_Schedule.FoodId,
   Name = CASE WHEN R_Schedule.dayId IS NULL THEN  FoodName + ' = price ' +(CONVERT( varchar(50),R_Foods.FoodPrice)) + ' - public-'
              ELSE  FoodName + ' = price ' +(CONVERT( varchar(50),R_Foods.FoodPrice)) + 'othervalue' END
from 
   R_Foods join R_Schedule 
      on R_Foods.FoodId=R_Schedule.FoodId 
where 
     R_Schedule.DayId=6 
     or DayId is null
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.