Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Formatting data in SQL

Author  Topic 

dipesh.ccsu
Starting Member

7 Posts

Posted - 2013-03-28 : 13:49:20
Hi All, i am new to SQL and need some help with formatting the data for certain columns. Please see below



CREATE VIEW FINANCE_USER_TBLS.ddc582_OCAM_FINAL AS(

SELECT A.Acct_Uniq_Cust_No,
A.ACT_MAX_DT,
B.Acct_Act_DT,
CASE WHEN A.ACT_MAX_DT= B.Acct_Act_DT THEN 'MAX' ELSE 'NOT' END UPLOAD_DATE

FROM FINANCE_USER_TBLS.ddc582_OCAM_MAX A, FINANCE_USER_TBLS.ddc582_OCAM_UPLOAD B

WHERE A.Acct_Uniq_Cust_No = B.Acct_Uniq_Cust_No

Above is my view table, where I am trying to retrieve data from two tables based on the case statement. Below is my output. The issue is the act max dt and acct act dt has the same info but are formatted different in terms of the dates. Any suggestion how can I change the data format to be the same for act max dt and acct act dt.


RESULT

Act Max Dt
2698571898013/03/22
2604326642013/03/08

Acct Act Dt
269857189802013-03-22
260432664202013-03-08


Upload Date
NOT
NOT



James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-28 : 14:23:47
The columns ACT_MAX_DT and Acct_Act_DT are probably of character data types. In general, the best practice is to use DATETIME or another date/time data type when you want to store data/time. Assuming the columns are character data type, you can use replace to convert one to be like the other. For example, if you wanted ot use "/"
CREATE VIEW FINANCE_USER_TBLS.ddc582_OCAM_FINAL AS(

SELECT A.Acct_Uniq_Cust_No,
A.ACT_MAX_DT,
REPLACE(B.Acct_Act_DT,'-','/') as ,Acct_Act_DT
CASE WHEN A.ACT_MAX_DT= B.Acct_Act_DT THEN 'MAX' ELSE 'NOT' END UPLOAD_DATE

FROM FINANCE_USER_TBLS.ddc582_OCAM_MAX A, FINANCE_USER_TBLS.ddc582_OCAM_UPLOAD B

WHERE A.Acct_Uniq_Cust_No = B.Acct_Uniq_Cust_No )
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2013-03-28 : 14:26:20
You can use replace to remove the / or -.

CASE WHEN replace(a.act_max_dt, '/', '') = replace(b.acct_act_dt, '-', '') THEN 'MAX' ELSE 'NOT'
Go to Top of Page
   

- Advertisement -