concatenating and two decimal places

  • Follow


Hello:

Below is my query, and I need help with two things.

First, I am trying to get each field in this query to be concatenated 
together.  But, there is a space between the field "CMTrxNum" and "CASE WHEN 
TRXAMNT....".  How do I get rid of this spacing?

Secondly, I need for the two fields that contain the phrases 
"CAST(-1*TRXAMNT as varchar)" and "CAST(TRXAMNT as varchar)" to be two 
decimal places.  How do I do this?

select '2000038028654' + '0000' + CMTrxNum + CASE WHEN TRXAMNT <0 THEN 
CAST(-1 * TRXAMNT as varchar) 
ELSE CAST(TRXAMNT as varchar) END
+ convert(varchar, TRXDATE, 112) 
+ CASE WHEN VOIDED = '1' then 'V' ELSE '' END
from CM20200
where CHEKBKID = 'UPTOWN TRUST' and SOURCDOC = 'PMPAY' or SOURCDOC = 'PMCHK'

SQL Programmer (it's just a name)
0
Reply Utf 5/24/2010 2:37:01 AM

It is good practice to always specify the VARCHAR length. Try this:

SELECT '2000038028654' + '0000' + CMTrxNum + 
       CASE WHEN TRXAMNT < 0 THEN LTRIM(STR(-1 * TRXAMNT, 20, 2)) 
            ELSE LTRIM(STR(TRXAMNT, 20, 2))
       END + CONVERT(VARCHAR(8), TRXDATE, 112) + 
       CASE WHEN VOIDED = '1' THEN 'V' ELSE '' END
FROM CM20200
WHERE CHEKBKID = 'UPTOWN TRUST' 
  AND SOURCDOC IN ('PMPAY', 'PMCHK');

-- 
Plamen Ratchev
http://www.SQLStudio.com
0
Reply Plamen 5/24/2010 3:06:29 AM


1 Replies
497 Views

(page loaded in 0.035 seconds)

Similiar Articles:
















7/23/2012 6:48:24 PM


Reply: