May 27, 2014 at 5:40 am
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.
May 27, 2014 at 5:42 am
+ 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
May 27, 2014 at 6:28 am
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]
May 27, 2014 at 6:36 am
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
May 27, 2014 at 6:41 am
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'
May 27, 2014 at 6:42 am
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
May 27, 2014 at 6:46 am
This is a great example showing that Explicit is better than Implicit.
May 27, 2014 at 7:58 am
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
Change is inevitable... Change for the better is not.
August 7, 2014 at 5:48 pm
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.
----------------------------------------------------
August 11, 2014 at 2:42 pm
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) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply