1

I learn SQL from this: http://www.w3schools.com/sql/sql_select.asp

SELECT LastName,FirstName FROM Persons

this show me:

LastName   |    FirstName
Hanse      |        Ola
Svendson   |    Tove
Pettersen  |    Kari

how can i add for this own columns without get data from DATABASE? for example:

SELECT TEST(default: aaa), LastName,FirstName FROM Persons

this should show me:

TEST | LastName   | FirstName
aaa  | Hanse      |        Ola
aaa  | Svendson   | Tove
aaa  | Pettersen  | Kari

3 Answers 3

8
SELECT 'aaa' as test, LastName,FirstName FROM Persons
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to create a "default" value on an actual field in a table you could use either the Coalesce or IsNull functions (please assume that Test is an actual field):

SELECT COALESCE(test, 'aaa') as Test, LastName,FirstName FROM Persons

SELECT ISNULL(test, 'aaa') as Test, LastName,FirstName FROM Persons

Both of these will return either the actual value of the field Test or 'aaa' if Test is Null.

We use COALESCE when retrieving the next Primary Key from a table (we don't use the Identity Property to maintain our PK's)

SELECT (COALESCE(MAX(PK), 0) + 1) FROM TableName

This way if there if the table doesn't contain any rows then 1 will be returned.

Comments

0

Randy has given best approach for an imagery field but if test is an actual field you can do this too..

select case when test is null then 'aaa' else test end, LastName,FirstName 
FROM Persons;

will return aaa if test is null else test's value.

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.