SQL – Select/Case

It’s so common to modify the select results based on conditions. Here’s a sample SQL CASE statement that would work with any of the sql engines:

SELECT t.id, 
	CASE WHEN t.include_filter is NULL THEN FALSE
		ELSE TRUE
	END AS filter_include,
	CASE WHEN t.age > 21 THEN 'Allowed'
		ELSE 'Not Allowed'
	END AS allowed,
	t.city AS city
FROM my_table t
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s