Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Right align (format) Text in SQL

Posted on 2010-11-09
3
Medium Priority
?
1,554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1500 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

715 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