在 SQL 中进行 convert() 时保留列名
Retain the column name when you convert() in SQL
如果我有 SQL 查询:
select
project.Employee,
project.HoursQuantity,
convert(varchar(10), project.EntryDate, 120)
from db.Project as project
它将 return ProjectHours 和 HoursQuantity 与正确的列名称,但 return EntryDate 为(无列名称)。
如何解决此问题或重命名该列,以便在 table returned 上向用户显示标题?
我原以为数据会保留在同一列中,但我想它会移到新的列中。我试过给它加上别名和其他一些东西,但没有用。
给它一个同名的alias
select
project.Employee,
project.HoursQuantity,
convert(varchar(10), project.EntryDate, 120) as EntryDate
from db.Project as project
我首选的列别名方法是 alias = expression
:
Bad Habits to Kick : Using AS instead of = for column aliases - Aaron Bertrand - 2012-01-23
select
project.Employee,
project.HoursQuantity,
EntryDate = convert(varchar(10), project.EntryDate, 120)
from db.Project as project
有效的列别名方法:
select 1 as x; -- #1
select x = 1; -- #2
select 'x' = 1 ; -- #3
select [x] = 1 ; -- #4
select 1 x; -- #5
select 1 as 'x'; -- #6
select 1 'x'; -- #7
select 1 as [x]; -- #8
select 1 [x]; -- #9
如果我有 SQL 查询:
select
project.Employee,
project.HoursQuantity,
convert(varchar(10), project.EntryDate, 120)
from db.Project as project
它将 return ProjectHours 和 HoursQuantity 与正确的列名称,但 return EntryDate 为(无列名称)。
如何解决此问题或重命名该列,以便在 table returned 上向用户显示标题?
我原以为数据会保留在同一列中,但我想它会移到新的列中。我试过给它加上别名和其他一些东西,但没有用。
给它一个同名的alias
select
project.Employee,
project.HoursQuantity,
convert(varchar(10), project.EntryDate, 120) as EntryDate
from db.Project as project
我首选的列别名方法是 alias = expression
:
Bad Habits to Kick : Using AS instead of = for column aliases - Aaron Bertrand - 2012-01-23
select
project.Employee,
project.HoursQuantity,
EntryDate = convert(varchar(10), project.EntryDate, 120)
from db.Project as project
有效的列别名方法:
select 1 as x; -- #1
select x = 1; -- #2
select 'x' = 1 ; -- #3
select [x] = 1 ; -- #4
select 1 x; -- #5
select 1 as 'x'; -- #6
select 1 'x'; -- #7
select 1 as [x]; -- #8
select 1 [x]; -- #9