我正在为特定实例中的每个数据库的表安排查询。每个数据库的查询和表都是类似的。我将查询结果指向文本文件。在查询结果之前,我包含了进行特定查询的数据库。但是,在我在AdventureWorks中的测试中,我得到的结果是一个带有很长下划线的数据库名称。
以下是我得到的输出:
DatabaseName
-------------------------------------------------------------------------------
AdventureWorks
AttemptDate
-----------------------
2009-05-29 12:54:28.460
以下是我调用的查询:
set nocount on
use AdventureWorks
GO
select DB_NAME()as DatabaseName
select AttemptDate from dbo.ChangeAttempt
我的问题是:如何缩短数据库名称下面的行?
答案 0 :(得分:2)
使用LEFT()
试试这个:
declare @mytabel table (longvalue varchar(1000))
select longvalue from @mytabel
select left(longvalue,50) as longvalue from @mytabel
输出:
longvalue
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(0 row(s) affected)
longvalue
--------------------------------------------------
(0 row(s) affected)
答案 1 :(得分:1)
尝试:
SELECT LEFT(DB_NAME(), 20) AS DatabaseName
答案 2 :(得分:-1)
您从此查询中获得什么输出?
set nocount on
use AdventureWorks
GO
select DB_NAME() as DatabaseName, AttemptDate from dbo.ChangeAttempt