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 Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, September 4, 2014 8:16 AM
Points: 215, Visits: 429
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 @ 2:03 AM
Points: 42,990, Visits: 36,145
+ 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 Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, September 4, 2014 8:16 AM
Points: 215, Visits: 429
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: Friday, September 12, 2014 3:59 AM
Points: 689, Visits: 2,782
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 Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, September 4, 2014 8:16 AM
Points: 215, Visits: 429
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: Yesterday @ 5:44 AM
Points: 607, Visits: 271
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
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, September 11, 2014 6:27 AM
Points: 333, Visits: 742
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 @ 12:03 AM
Points: 37,065, Visits: 31,628
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
Posted Thursday, August 7, 2014 5:48 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 3:05 PM
Points: 355, Visits: 870
Jeff Moden (5/27/2014)
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.


+1
The number is a higher order data type ...so strings will try to be converted to it.
SELECT 5+ '5' --> 10 /* not '55' */
With 2012, use the CONCAT function - in it everything becomes a string first.
Post #1600972
Posted Monday, August 11, 2014 2:42 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 4:05 PM
Points: 2,070, Visits: 3,113
Yep. You just need to CAST the first column as varchar:

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




SQL DBA,SQL Server MVP('07, '08, '09)
"And in the evening, After the fire and the light /
One thing is certain: Nothing can hold back the night /
Time is relentless, And as the past disappears /
We're on the verge of all things new, We are two thousand years" : the inimitable Mr. Billy Joel
Post #1602069
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse