Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, December 2, 2016 4:41 AM
Points: 333, Visits: 700
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: 2 days ago @ 5:18 AM
Points: 45,619, Visits: 44,147
+ 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, MVP, M.Sc (Comp Sci)
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
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, December 2, 2016 4:41 AM
Points: 333, Visits: 700
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: Monday, November 21, 2016 8:42 AM
Points: 698, Visits: 3,018
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
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, December 2, 2016 4:41 AM
Points: 333, Visits: 700
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
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: Monday, November 30, 2015 8:44 AM
Points: 662, Visits: 297
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: Today @ 7:17 AM
Points: 364, Visits: 831
This is a great example showing that Explicit is better than Implicit.
Post #1574760
Posted Tuesday, May 27, 2014 7:58 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 @ 9:30 PM
Points: 42,060, Visits: 39,443
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."

Helpful Links:
How to post code problems
How to post performance problems
Post #1574792
Posted Thursday, August 7, 2014 5:48 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 7:27 PM
Points: 989, Visits: 1,930
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.


----------------------------------------------------
How to post forum questions to get the best help
Post #1600972
Posted Monday, August 11, 2014 2:42 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, December 2, 2016 3:56 PM
Points: 3,852, Visits: 6,566
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)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
Post #1602069
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse