Solved

Right align (format) Text in SQL

Posted on 2010-11-09
3
1,230 Views
Last Modified: 2012-05-10
When there is no value for “test value” or “test unit”, I would like to have the Date align to the right to be under the Date field with values.  I have tried to use the rtrim and the char spacing and this did not work.  I would like to have some suggestions on this.  Thanks
I am getting the following now:
3.6 K/mcL          Date: 12/18/2008
--->      Date: 10/09/2008
11.1 g/dl - 06/26/2009
Unk
Would like to see:
3.6 K/mcL          Date: 12/18/2008
                          Date: 10/09/2008
11.1 g/dl - 06/26/2009
Unk
CASE WHEN dbo.FN_dmthem_GetLookupDesc('sourceCd',sourceCd) = 'YNCC'
         then  cast(cast(testvalue as float) as varchar)+' ' +testUnit+' - '+isnull(convert(VARCHAR(15),testDte,101),' ')  
     WHEN dbo.FN_dmthem_GetLookupDesc('sourceCd',sourceCd) = 'Unk'
       then convert(VARCHAR(20),dbo.FN_dmthem_GetLookupDesc('sourceCd',sourceCd))
    Else isnull(cast(cast(testvalue as float)as varchar) + ' ' + testUnit,' '  ) +' ' + isnull('      Date: '+ convert(VARCHAR(15),testDte,101),' ')

Tried:
replace(replace(replace(isnull('    Date: '+ convert(VARCHAR(15),testDte,101),''),char(9),''),char(10),''),char(13),'')
Also Tried:
+ ltrim(rtrim(isnull('      Date: '+ convert(VARCHAR(15),testDte,101),' ')))
0
Comment
Question by:YVR33
3 Comments
 
LVL 2

Expert Comment

by:Pacane
ID: 34095250
You may use the format() function..
See the use: http://www.w3schools.com/SQl/sql_func_format.asp
0
 
LVL 7

Accepted Solution

by:
Cboudroz earned 500 total points
ID: 34095434
you need to know in which position you want it.

like this all the date will start in position 20


CASE 
 WHEN CONDITION1 = 1 THEN CONVERT(CHAR(20), isnull(data1,'') ) + date
 WHEN CONDITION2 = 2 THEN CONVERT(CHAR(20), isnull(data2,'') ) + date
ELSE CONVERT(CHAR(20), isnull(data3,'') ) + date
END

Open in new window

0
 

Author Comment

by:YVR33
ID: 34103248
This did not seem to work, I got the following results:
The "Date" in the Else is what I  would like to have aligned.  The value and unit maybe null and when this occurs I would like to have the "Date" label aligned to the right.

Else isnull(cast(cast(testvalue as float)as varchar) + ' ' + testUnit,' '  ) +' '+ + isnull('      Date: '+ CONVERT(VARCHAR(50), isnull(testDte,'') + testDte, 101), '')

Open in new window

EE-Date-Value-scrprnt.docx
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now