views:

101

answers:

3

Hi,

I am scheduling the a query to a table for each database in a particular instance. The query and table for each database are similar. I direct the query results to text file. Before the query results, I include the database where the particular query is being made. In my test in AdventureWorks, however, the result I got is a database name with very long underline.

Below is the output I got:

DatabaseName
-------------------------------------------------------------------------------
AdventureWorks                                                                                                                          

AttemptDate             
----------------------- 
2009-05-29 12:54:28.460

Below is the query I invoked:

set nocount on
use AdventureWorks
GO
select DB_NAME()as DatabaseName
select AttemptDate from dbo.ChangeAttempt

My question is: How do I shorten the lines below the database name?

A: 

What output would you get from this query?

set nocount on
use AdventureWorks
GO
select DB_NAME() as DatabaseName, AttemptDate from dbo.ChangeAttempt
Tomas Lycken
+2  A: 

use LEFT()

try this:

declare @mytabel table (longvalue  varchar(1000))

select longvalue from @mytabel

select left(longvalue,50) as longvalue from @mytabel

output:

longvalue
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

(0 row(s) affected)

longvalue
--------------------------------------------------

(0 row(s) affected)
KM
Huh. I learned something new today. Thanks for posting this.
Aaron Alton
in text mode, sql server will put one "-" minus char for each possible character len of the column. reduce the possible max length and you get fewer "-" minus characters...
KM
+1  A: 

Try:

SELECT LEFT(DB_NAME(), 20) AS DatabaseName
Jose Basilio
Thanks. It works. I just increase the 20 to 40 to accomodate long database name which I notice in some of the databases.
titanium