Solved

Right align (format) Text in SQL

Posted on 2010-11-09
3
1,284 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql Server group by 10 30
How to construct an if else statement from existing code 6 22
performance query 4 24
Highest and 2nd Highest Totals 11 22
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

832 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