SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Changing a returned value depending on date?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rasta Pickles
Posting Yak Master

United Kingdom
133 Posts

Posted - 08/17/2012 :  11:56:41  Show Profile  Reply with Quote
Hi all,

I am returning the value of a cost centre. However, for reporting purposes, the value needs to be converted to '9999' if the date of the record is pre-2012.

Here's the relevant bit of code:

'P9-'+CAST(RIGHT('0000'+CASE WHEN NL.DET_COSTCENTRE IS NULL THEN '0' ELSE NL.DET_COSTCENTRE END,4) as VARCHAR) as CC_Co,RIGHT('0000'+NL.DET_COSTCENTRE,4) as Cost_Centre,


So I need something incorporated into this bit of code that checks the record date and if it's pre-2012 it simply returns 9999.

Any advice greatly appreciated.

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 08/17/2012 :  12:11:32  Show Profile  Reply with Quote
which is the date field involved here?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
133 Posts

Posted - 08/17/2012 :  13:20:16  Show Profile  Reply with Quote
The date isn't included in this bit of the code.

Assume the date field is called TRANSACTION_DATE.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 08/17/2012 :  13:22:33  Show Profile  Reply with Quote
and is it in same table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3833 Posts

Posted - 08/17/2012 :  13:22:50  Show Profile  Reply with Quote
CASE
    WHEN DateColumn < '20120101' THEN '9999'
    ELSE <forumal here>
END AS Cost_Centre
Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
133 Posts

Posted - 08/17/2012 :  13:42:17  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

and is it in same table?



Yes, it's in the Nominal Ledger (NL) table.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 08/17/2012 :  13:59:16  Show Profile  Reply with Quote

...
CASE WHEN Datefield >= '20120101'
THEN 'P9-'+CAST(RIGHT('0000'+CASE WHEN NL.DET_COSTCENTRE IS NULL THEN '0' ELSE NL.DET_COSTCENTRE END,4) as VARCHAR) as CC_Co,RIGHT('0000'+NL.DET_COSTCENTRE,4) 
ELSE '9999'
END  as Cost_Centre
...


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
133 Posts

Posted - 08/17/2012 :  14:15:27  Show Profile  Reply with Quote
Thank you my friend, I will give it a whirl in work on the morrow.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 08/17/2012 :  14:18:56  Show Profile  Reply with Quote
ok no probs..
lets us know if you face any issues and need more help

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000