Jason Clark
Jason Clark

Reputation: 1425

How to get firstname from using this query?

I am trying to fetch firstname from employeedetail table where the character string of firstname = 5, I am executing below code :

select firstname=(case when len(firstname) = 5 then firstname end) from employeedetail

it gives me what i want, but it giving me the null values as well, just because of character string value. Now i don't want these null values in my output. How can i do this?

Upvotes: 0

Views: 53

Answers (2)

cco
cco

Reputation: 6281

Add where len(firstname) = 5 to your query.

This will help you avoid all rows where length of firstname column data is not 5 characters altogether.

Your problem arose as CASE statement did not have the ELSE part defined which resulted in NULL for all those case whose WHEN part was false.

If you want to handle the cases where firstname is longer or shorter than 5 characters another way, just add more when clauses to your case statement.

Upvotes: 3

vinay koul
vinay koul

Reputation: 346

select firstname=(case when ISNULL(firstname,'')<>'' AND len(firstname) = 5 then firstname end) from employeedetail

Upvotes: 1

Related Questions