为列值设置别名

时间:2016-03-02 06:15:19

标签: sql sql-server alias

是否可以为sql server中的列标题设置列值的别名。

或者,如果有任何其他方法可以将我的列值转换为客户端的可读格式。

我有以下系统生成的值:

BILL_DETAILS
BILLING_MENU
ComplaintNumberInput
CUSTOMER_ACCOUNT_NUMBER_INPUT
DEFAULTER
FAULTS_SHUTDOWN_MENU
KUNDA_CONNECTION
LOAD_SHEDDING_MENU
LOAD_SHEDDING_SCHEDULED
loadSheddingScheduleReplayer
loadSheddingStatus
loadSheddingStatusReplayer
MENU_CONTEXT_EVAL
POWER_COMPLAINTS_MENU
repaetComplaintStatus

是否可以在以下内容中进行更改:

BILL DETAILS
BILLING MENU
COMPLAINT NUMBER INPUT
CUSTOMER ACCOUNT NUMBER INPUT
DEFAULTER
FAULTS SHUTDOWN MENU
KUNDA CONNECTION
LOAD SHEDDING MENU
LOAD SHEDDING SCHEDULED
LOAD SHEDDING SCHEDULE REPLAYER
LOAD SHEDDING STATUS
LOAD SHEDDING STATUS REPLAYER
MENU CONTEXT EVAL
POWER COMPLAINTS MENU
REPEAT COMPLAINT STATUS

2 个答案:

答案 0 :(得分:1)

在sql中,Alias是数据库对象的不同名称。值不属于此类别,因此无法对它们进行别名。但是,您可以格式化查询的输出,但格式化通常最好在表示层而不是数据层中进行。

话虽如此,你的问题有一个t-sql解决方案:

SELECT REPLACE(ColumnName, '_', ' ') As ColumnName
FROM TableName

这会将所有下划线转换为空格。

要处理其他格式,您也可以感谢Jeff Moden解决该问题(see this link)。

SELECT COALESCE(STUFF(ColumnName, NULLIF(patindex('%[a-z][A-Z]%', ColumnName COLLATE Latin1_General_BIN), 0) + 1, 0, ' '), Col) AS ColumnName
FROM TableName

所以结合2个解决方案你的最终sql应该是这样的:

SELECT REPLACE(COALESCE(STUFF(ColumnName, NULLIF(patindex('%[a-z][A-Z]%', ColumnName COLLATE Latin1_General_BIN), 0) + 1, 0, ' '), ColumnName), '_', ' ') AS ColumnName
FROM TableName

通过这种方式,您可以在纯t-sql中处理这两种格式,而无需在每次向表中添加新值时更改查询。

以下是您发布的值的测试用例:

DECLARE @t TABLE (Col VARCHAR(40))

INSERT INTO @t VALUES
('BILL_DETAILS'),
('BILLING_MENU'),
('ComplaintNumberInput'),
('CUSTOMER_ACCOUNT_NUMBER_INPUT'),
('DEFAULTER'),
('FAULTS_SHUTDOWN_MENU'),
('KUNDA_CONNECTION'),
('LOAD_SHEDDING_MENU'),
('LOAD_SHEDDING_SCHEDULED'),
('loadSheddingScheduleReplayer'),
('loadSheddingStatus'),
('loadSheddingStatusReplayer'),
('MENU_CONTEXT_EVAL'),
('POWER_COMPLAINTS_MENU'),
('repaetComplaintStatus')

SELECT Col
       ,UPPER(REPLACE(COALESCE(STUFF(col, NULLIF(patindex('%[a-z][A-Z]%', Col COLLATE Latin1_General_BIN), 0) + 1, 0, ' '), Col), '_', ' ')) AS NewCol
FROM @t

结果:

Col                                      NewCol

BILL_DETAILS                             BILL DETAILS
BILLING_MENU                             BILLING MENU
ComplaintNumberInput                     COMPLAINT NUMBERINPUT
CUSTOMER_ACCOUNT_NUMBER_INPUT            CUSTOMER ACCOUNT NUMBER INPUT
DEFAULTER                                DEFAULTER
FAULTS_SHUTDOWN_MENU                     FAULTS SHUTDOWN MENU
KUNDA_CONNECTION                         KUNDA CONNECTION
LOAD_SHEDDING_MENU                       LOAD SHEDDING MENU
LOAD_SHEDDING_SCHEDULED                  LOAD SHEDDING SCHEDULED
loadSheddingScheduleReplayer             LOAD SHEDDINGSCHEDULEREPLAYER
loadSheddingStatus                       LOAD SHEDDINGSTATUS
loadSheddingStatusReplayer               LOAD SHEDDINGSTATUSREPLAYER
MENU_CONTEXT_EVAL                        MENU CONTEXT EVAL
POWER_COMPLAINTS_MENU                    POWER COMPLAINTS MENU
repaetComplaintStatus                    REPAET COMPLAINTSTATUS

答案 1 :(得分:0)

为每个值使用case语句,例如:

case old_column_name
     when 'LOAD_SHEDDING_MENU'
     then 'LOAD SHEDDING MENU'
     when 'loadSheddingScheduleReplayer'
     then 'LOAD SHEDDING SCHEDULE REPLAYER'
     when ...........
     then ...........
end as column_name