Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

TSQL no longer working in SQL2012 Expand / Collapse
Author
Message
Posted Tuesday, May 27, 2014 5:40 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 9:19 AM
Points: 198, Visits: 397
Hi there,

I had the following script that used to work in SQL2000 but now that I have migrated it, it no longer does.

				,TEN.[tncy-sys-ref] 
+ CASE WHEN PER.[GENDER] = 'M' THEN '1'
WHEN PER.[GENDER] = 'F' THEN '2'
ELSE '0'END AS 'ID'

This used to either add a 1 or a 2 to the end of the TEN.[tncy-sys-ref]. What it does now is actually make an addition.

Example the TEN.[tncy-sys-ref] is 25337.
On SQL2000 it is 253371 for a Male or 253372 for a female.
On SQL2012 it's coming back 25338 for a male and 25339 for a female.

I need it to concatenate instead of addition.
Post #1574743
Posted Tuesday, May 27, 2014 5:42 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 12:00 PM
Points: 42,436, Visits: 35,490
+ CAST(CASE WHEN PER.[GENDER] = 'M' THEN '1' 
WHEN PER.[GENDER] = 'F' THEN '2'
ELSE '0' END AS CHAR(1))




Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1574744
Posted Tuesday, May 27, 2014 6:28 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 9:19 AM
Points: 198, Visits: 397
Thanks for the reply. That's not working I'm afraid.

It's still increasing the TEN.[tncy-sys-ref] by 1 or 2 as opposed to just concatenating a 1 or a 2 at the end of TEN.[tncy-sys-ref]

Post #1574753
Posted Tuesday, May 27, 2014 6:36 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 5:34 AM
Points: 689, Visits: 2,753
Could Try I'm guessing at the Varchar Length:


,CAST(TEN.[tncy-sys-ref] AS VARCHAR(50))
+ CAST(CASE WHEN PER.[GENDER] = 'M' THEN '1'
WHEN PER.[GENDER] = 'F' THEN '2'
ELSE '0' END AS CHAR(1))


Or you could use SQL 2012 CONCAT

,CONCAT(CAST(TEN.[tncy-sys-ref] AS VARCHAR(50)),
CAST(CASE WHEN PER.[GENDER] = 'M' THEN '1'
WHEN PER.[GENDER] = 'F' THEN '2'
ELSE '0' END AS CHAR(1)))

Andy


==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
Post #1574757
Posted Tuesday, May 27, 2014 6:41 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 9:19 AM
Points: 198, Visits: 397
Got it working -


CONCAT (TEN.[tncy-sys-ref],CASE WHEN PER.[GENDER] = 'M' THEN '1'
WHEN PER.[GENDER] = 'F' THEN '2'
ELSE '0'END) AS 'ID'
Post #1574758
Posted Tuesday, May 27, 2014 6:42 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, May 27, 2014 6:27 AM
Points: 604, Visits: 261
Hi,

It seems as if TEN.[tncy-sys-ref] is numeric or int?

If you want the result as a char, you probably have to adjust Gilas code by adding another CAST:

CAST(TEN.[tncy-sys-ref] AS CHAR(7 /* or whatever you want */)

On the other hand, if you want to have a numeric response, you need to multiply by 10 (and then you could skip all the cast:ing to char):

(TEN.[tncy-sys-ref]  * 10)

/Markus
Post #1574759
Posted Tuesday, May 27, 2014 6:46 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 10:06 AM
Points: 267, Visits: 597
This is a great example showing that Explicit is better than Implicit.
Post #1574760
Posted Tuesday, May 27, 2014 7:58 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 11:31 AM
Points: 36,714, Visits: 31,164
Ryan Keast (5/27/2014)
Hi there,

I had the following script that used to work in SQL2000 but now that I have migrated it, it no longer does.

				,TEN.[tncy-sys-ref] 
+ CASE WHEN PER.[GENDER] = 'M' THEN '1'
WHEN PER.[GENDER] = 'F' THEN '2'
ELSE '0'END AS 'ID'

This used to either add a 1 or a 2 to the end of the TEN.[tncy-sys-ref]. What it does now is actually make an addition.

Example the TEN.[tncy-sys-ref] is 25337.
On SQL2000 it is 253371 for a Male or 253372 for a female.
On SQL2012 it's coming back 25338 for a male and 25339 for a female.

I need it to concatenate instead of addition.


If that's true, then something went haywire with the migration because that code will still work as you have it if TEN.[tncy-sys-ref] is still a VARCHAR() and could only work as is if it was a VARCHAR(). Check the old system that you migrated and see if the column was, infact a VARCHAR() because I think it was and something changed it during the migration.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1574792
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse