Kumee
Kumee

Reputation: 211

Set default value for column using alias in sql select query

I am facing an problem to set an default value in sql select query

ex: t1.Employee as EmpDeptName

here i need to set an default value so that it returns always the default value

what i tried t1.Employee as EmpDeptName 'ITdept'

But this fails always telling incorrect syntax

Upvotes: 1

Views: 6331

Answers (1)

Kermit
Kermit

Reputation: 34054

If you want to SELECT this value when you call a query,

SELECT 'ITdept' AS EmpDeptName, ...

If you want to use "ITdept" as the default value when the column is empty,

SELECT ISNULL(EmpDeptName, 'ITdept') AS EmpDeptName, ...

If you want to set the column default,

CREATE TABLE t1 (
    EmpDeptName <data type> DEFAULT 'ITdept'
);

Upvotes: 6

Related Questions