I have 2 script as following:
First one:
SELECT [Fm].[Id], [Sdp].[FirstName], [Sdp].[LastName], [Sdp].[SSN],
[Sdp].[StoreName], [Sdp].[PostalCode], [Fc].[Id], [Sdp].[Address]
FROM [SRM].[SiteMembers].[DProfile] AS [Sdp]
INNER JOIN [SRM].[SiteMembers].[Member] AS [Sm]
ON [Sdp].[Member_Id] = [Sm].[Id]
INNER JOIN [FRM].[Members].[Member] AS [Fm]
ON [Sm].[UserId] = [Fm].[UserId]
INNER JOIN [SRM].[General].[City] AS [Sc]
ON [Sdp].[City_Id]=[Sc].[Id]
INNER JOIN [FRM].[General].[City] AS [Fc]
ON [Fc].[Title]=[Sc].[Title]
COLLATE SQL_Latin1_General_CP1_CI_AS
WHERE ISNUMERIC([Sdp].[PostalCode])=1;
And Second one:
SELECT [Fm].[Id], [Sdp].[FirstName], [Sdp].[LastName], [Sdp].[SSN],
[Sdp].[StoreName], 0, [Fc].[Id], [Sdp].[Address]
FROM [SRM].[SiteMembers].[DProfile] AS [Sdp]
INNER JOIN [SRM].[SiteMembers].[Member] AS [Sm]
ON [Sdp].[Member_Id] = [Sm].[Id]
INNER JOIN [FRM].[Members].[Member] AS [Fm]
ON [Sm].[UserId] = [Fm].[UserId]
INNER JOIN [SRM].[General].[City] AS [Sc]
ON [Sdp].[City_Id]=[Sc].[Id]
INNER JOIN [FRM].[General].[City] AS [Fc]
ON [Fc].[Title]=[Sc].[Title]
COLLATE SQL_Latin1_General_CP1_CI_AS
WHERE ISNUMERIC([Sdp].[PostalCode])=0;
The difference between this 2 script is that the first one select columns with numeric postal code and second one select 0 for the non numeric postal code so how can I merge this 2 script together in one script, I am not talking about Union, I am interesting to use some condition in select query for non numeric postal code select 0. Does any one have any idea?
I would skip the where statement and create one column for the Numeric postal code and one for the other one. Like this:
SELECT
[Fm].[Id],
[Sdp].[FirstName],
[Sdp].[LastName],
[Sdp].[SSN],
[Sdp].[StoreName],
(
CASE WHEN ISNUMERIC([Sdp].[PostalCode])=1
THEN 0
ELSE NULL
END
) AS NumericPostalCode,
(
CASE WHEN ISNUMERIC([Sdp].[PostalCode])=0
THEN [Sdp].[PostalCode]
ELSE NULL
END
) AS PostalCode,
[Fc].[Id],
[Sdp].[Address]
FROM
[SRM].[SiteMembers].[DProfile] AS [Sdp]
INNER JOIN [SRM].[SiteMembers].[Member] AS [Sm]
ON [Sdp].[Member_Id] = [Sm].[Id]
INNER JOIN [FRM].[Members].[Member] AS [Fm]
ON [Sm].[UserId] = [Fm].[UserId]
INNER JOIN [SRM].[General].[City] AS [Sc]
ON [Sdp].[City_Id]=[Sc].[Id]
INNER JOIN [FRM].[General].[City] AS [Fc]
ON [Fc].[Title]=[Sc].[Title]
COLLATE SQL_Latin1_General_CP1_CI_AS´
Edit 1
I know that this will result in two columns. But you can not have to data types in one column. But I think that you can do it something like this as well.
SELECT
[Fm].[Id],
[Sdp].[FirstName],
[Sdp].[LastName],
[Sdp].[SSN],
[Sdp].[StoreName],
(
CASE WHEN ISNUMERIC([Sdp].[PostalCode])=0
THEN [Sdp].[PostalCode]
ELSE '0'
END
) AS PostalCode,
[Fc].[Id],
[Sdp].[Address]
This will work becuase the numeric one are '0' as a varchar.
Edit 2
You can also do it like this:
SELECT CAST('asdasd' AS sql_variant)
UNION ALL
SELECT CAST(0 AS sql_variant)
But this is a dirty solution for this problem. The sql_variant is a database object. So my conclusion is: Use to column if you what to have different data types. If the values can be the same data type use the same column. Do not use sql_variant to solve problems like this. You will pay a high price in maintaining the code.
Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With