0
SELECT * FROM MY_TABLE;

CLOUMN_NAME
1st
2nd
3rd

Am getting the output when I run this query.

I need to have a default value for 'Please Select' as first row always even column holds the empty or null value of the result set.

Exptected Result

CLOUMN_NAME
Please Select
1st
2nd
3rd

Thanks in advance.

1
  • how many column you need to return only on or more than one ? Commented May 21, 2015 at 11:08

3 Answers 3

1

Try to use union all as below

SELECT 'Please Select' as CLOUMN_NAME 
union all
SELECT cast(CLOUMN_NAME as varchar) FROM MY_TABLE;

I'm not sure what type is CLOUMN_NAME but should be string type because in first query we return this type.

Sign up to request clarification or add additional context in comments.

1 Comment

There is no order here... it could be in any order. Order not guaranteed by UNION.
0
select 'please select' col_name
union all
select col_name::text FROM MY_TABLE;

Edit : as per this comment

In your case you need to use order by in second select statement

select 'please select' col_name 
union all
(select col_name::text FROM MY_TABLE order by 1)

sqlfiddle - demo

you need to convert column in that table(MY_TABLE) to text if that column is not a Text,varchar

2 Comments

Works fine but fails in a scenario.If the column holds the empty value then the empty value is coming first while sorting the.data. My requirement is that whatever the column data it may have 'Please Select' value should come first.
There is no real order here. Order not guaranteed by UNION.
0

If you want a stable sort you need some kind of criteria to distinguish the "first" row from the "real" ones:

select column_name
from (
  select 'please select' as column_name, 0 as sort_order
  union all
  select column_name, 1 as sort_order
  FROM MY_TABLE
) t
order by sort_order;

Note that the sort order for the actual data is undefined in this case because all rows from the table get the same "sort_order" value. If you e.g. want the real rows to be sorted alphabetically, you can add the display column to the order by: `order by sort_order, column_name.

The above assumes that my_table.column_name is a character data type. If it is not, you need some type casting.

1 Comment

This is the correct answer because it uses correct ordering

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.