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
 Why doesn't this line of code work?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rasta Pickles
Posting Yak Master

United Kingdom
172 Posts

Posted - 01/28/2013 :  12:27:13  Show Profile  Reply with Quote

CASE WHEN CAST(YR.YEAR_LABEL AS VARCHAR) + CAST(NL.DET_PERIODNUMBR AS VARCHAR) < '201207' THEN P678 ELSE P999


YR.YEAR_LABEL is 2012, NL.DET_PERIODNUMBR is 01, it doesn't work, it returns P999.

It DOES work if YR.YEAR_LABEL is 2011 or 2010?

What's wrong with my syntax?

Thanks as always.

James K
Flowing Fount of Yak Knowledge

3741 Posts

Posted - 01/28/2013 :  12:32:05  Show Profile  Reply with Quote
It's probably because the string concatenation results in 20121 rather than 201201. Can you try this:
CASE WHEN CAST(YR.YEAR_LABEL AS VARCHAR) + RIGHT('00'+CAST(NL.DET_PERIODNUMBR AS VARCHAR),2) < '201207' THEN P678 ELSE P999
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 01/28/2013 :  13:02:22  Show Profile  Reply with Quote
Also, make sure to specify the length of the varchar

CASE WHEN CAST(YR.YEAR_LABEL AS VARCHAR(4)) + RIGHT('00'+CAST(NL.DET_PERIODNUMBR AS VARCHAR(3)),2) < '201207' THEN P678 ELSE P999 END

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/28/2013 :  13:06:40  Show Profile  Reply with Quote
see the reason here

http://visakhm.blogspot.in/2010/02/importance-of-specifying-length-in.html



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

Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3741 Posts

Posted - 01/28/2013 :  13:07:47  Show Profile  Reply with Quote
Thank you Jim, for catching that. Here is a nice example of why what Jim said is important: http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-declaring-varchar-without-length.aspx
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3741 Posts

Posted - 01/28/2013 :  13:08:26  Show Profile  Reply with Quote
Visakh, you and I are thinking alike! I was just a minute too slow in my thinking, but oh well!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/28/2013 :  13:11:37  Show Profile  Reply with Quote
quote:
Originally posted by James K

Visakh, you and I are thinking alike! I was just a minute too slow in my thinking, but oh well!!


Great minds think alike

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

Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
172 Posts

Posted - 01/28/2013 :  14:06:40  Show Profile  Reply with Quote
Thanks all, I'll try in the morning.
Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
172 Posts

Posted - 01/29/2013 :  04:46:08  Show Profile  Reply with Quote
Tried the code this morning and it works fine so thanks again.

Just a final question re the original issue: 20121 IS less than 201201 but still failed the test - is it something to do with how SQL is "seeing" the value?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/29/2013 :  04:53:14  Show Profile  Reply with Quote
quote:
Originally posted by Rasta Pickles

Tried the code this morning and it works fine so thanks again.

Just a final question re the original issue: 20121 IS less than 201201 but still failed the test - is it something to do with how SQL is "seeing" the value?


Thats because SQL Server orders them as string values not as numerical values.

See the difference




SELECT *
FROM(
SELECT '20121' AS val UNION ALL
SELECT '201207'
)t
ORDER BY Val

SELECT *
FROM(
SELECT '20121' AS val UNION ALL
SELECT '201207'
)t
ORDER BY Val*1

output
----------------------------
val
--------------------------
201207
20121


val
------------------------------
20121
201207



in second case the * 1 forces it to make it integer before ordering which makes the difference

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

Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
172 Posts

Posted - 01/29/2013 :  11:44:06  Show Profile  Reply with Quote
Excellent illustration, many thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/29/2013 :  11:45:07  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
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.06 seconds. Powered By: Snitz Forums 2000