Related
I'm new to PostgreSQL, sorry!
I need this query to return the data ordered by month and year, but I'm not getting it and I can't adjust it.
Here we go!
select
CASE
WHEN datepart(month, created_at) = '1' THEN 'January'
WHEN datepart(month, created_at) = '2' THEN 'February'
WHEN datepart(month, created_at) = '3' THEN 'March'
WHEN datepart(month, created_at) = '4' THEN 'April'
WHEN datepart(month, created_at) = '5' THEN 'May'
WHEN datepart(month, created_at) = '6' THEN 'June'
WHEN datepart(month, created_at) = '7' THEN 'July'
WHEN datepart(month, created_at) = '8' THEN 'August'
WHEN datepart(month, created_at) = '9' THEN 'September'
WHEN datepart(month, created_at) = '10' THEN 'October'
WHEN datepart(month, created_at) = '11' THEN 'November'
WHEN datepart(month, created_at) = '12' THEN 'December'
end as Month,
datepart(year, created_at) as Year,
count (distinct id) as Countd
from table
where
created_at > CURRENT_TIMESTAMP - INTERVAL '12 months'
Group by Month, Year
Order by Month, Year desc
Thanks for all support! :D
Try this:
select
to_char(created_at, 'Month') as Month,
date_part('year', created_at) as Year,
count (distinct id) as Countd
from table
where
created_at > CURRENT_TIMESTAMP - INTERVAL '12 months'
Group by date_part('month', created_at), date_part('year', created_at)
Order by date_part('month', created_at), date_part('year', created_at) desc
I'm stuck - I have this nice little text searcher I've put together.
I realised I needed to count lnies in individual blocks of text that the stored procedure is stored in, so I think I've figured a way to do that.
But it's not getting every instance of a search term in a stored procedure - and I think it's the same reason... the search term is split across a text boundary.
I've looked at how to change my query - and I'm coming up blank. The skills involved to change it are beyond me!
--Text searcher.
DECLARE #searchString VARCHAR(255),
#doesNotContain VARCHAR(255),
#previewLength INTEGER,
#findStoredProcedures VARCHAR(3),
#findTableFunction VARCHAR(3),
#findScalerFunction VARCHAR(3),
#findTrigger VARCHAR(3),
#findView VARCHAR(3),
#findUserTable VARCHAR(3),
#onlyInName VARCHAR(3)
--------------------------------------------------------
-- Search criteria:
SET #searchString = 'My search Term'
SET #findStoredProcedures = 'yes'
SET #findTableFunction = 'yes'
SET #findScalerFunction = 'yes'
SET #findUserTable = 'yes'
SET #findTrigger = 'yes'
SET #findView = 'yes'
SET #doesNotContain = ''
SET #previewLength = 30
--------------------------------------------------------
SELECT DISTINCT
ISNULL(
(SELECT REPLACE(CONVERT(VARCHAR(20), (CAST(SUM(LEN(SC2.text)) AS MONEY)), 1), '.00', '')
FROM syscomments SC2 WHERE SC2.id = SO.id GROUP BY SC2.id)
, '')
AS [Object length]
,
SO.name AS [Object name]
,
CASE
WHEN SO.xtype = 'P' THEN 'Stored Procedure'
WHEN SO.xtype = 'TF' THEN 'Table Function'
WHEN SO.xtype = 'FN' THEN 'Scaler Function'
WHEN SO.xtype = 'U' THEN 'User Table'
WHEN SO.xtype = 'TR' THEN 'Trigger'
WHEN SO.xtype = 'V' THEN 'View'
END
+ ISNULL((SELECT ' - ' + name FROM sysobjects WHERE id = SO.parent_obj), '')
AS [Object type]
,
ISNULL(SUBSTRING(SC.text, CHARINDEX(#searchString, SC.text) - #previewLength, #previewLength) +
SUBSTRING(SC.text, CHARINDEX(#searchString, SC.text), #previewLength + LEN(#searchString))
, '') AS [Preview of code]
,
(SELECT
COALESCE(
SUM(LEN(SC3.text) - LEN(REPLACE(SC3.text, CHAR(13), '')) + 1) + 4
+
(
SELECT
(LEN(LEFT(SC4.text, CHARINDEX(#searchString, SC4.text))) -
LEN(REPLACE(LEFT(SC4.text, CHARINDEX(#searchString, SC4.text)), CHAR(13), '')))
FROM syscomments SC4
WHERE
SC4.id = SO.id
AND SC4.colid = SC.colid
)
,
SUM(LEN(SC3.text) - LEN(REPLACE(SC3.text, CHAR(13), '')) + 1) + 4
,
(
SELECT
(LEN(LEFT(SC4.text, CHARINDEX(#searchString, SC4.text))) -
LEN(REPLACE(LEFT(SC4.text, CHARINDEX(#searchString, SC4.text)), CHAR(13), '')) + 1)
FROM syscomments SC4
WHERE
SC4.id = SO.id
AND SC4.colid = SC.colid
)
)
FROM syscomments SC3
WHERE
SC3.id = SO.id
AND SC3.colid < SC.colid
)
AS [Line number]
FROM sysobjects SO
LEFT JOIN syscomments SC
ON SO.id = SC.id
WHERE
(
(SO.type = 'P' AND #findStoredProcedures = 'yes')
OR
(SO.type = 'TF' AND #findTableFunction = 'yes')
OR
(SO.type = 'FN' AND #findScalerFunction = 'yes')
OR
(SO.type = 'TR' AND #findTrigger = 'yes')
OR
(SO.type = 'U' AND #findUserTable = 'yes')
OR
(SO.type = 'V' AND #findView = 'yes')
)
AND SO.category = 0
AND
(
(CHARINDEX(#searchString, SC.text) > 0
AND CHARINDEX(#doesNotContain, SC.text) = 0)
OR
(SO.type = 'U'
AND CHARINDEX(#searchString, SO.name) > 0
AND CHARINDEX(#doesNotContain, SO.name) = 0)
)
ORDER BY
[Object type], [Object name], [Line number]
Your where clause seems to have an issue, this bit here
(
(CHARINDEX(#searchString, SC.text) > 0
AND CHARINDEX(#doesNotContain, SC.text) = 0)
--OR
--(SO.type = 'U'
--AND CHARINDEX(#searchString, SO.name) > 0
--AND CHARINDEX(#doesNotContain, SO.name) = 0)
SELECT user
FROM userlist zH with(nolock)
where zH.user in (case when zh.trait='1' then ('B', 'HO', 'KO', 'PL','APP','2A','2B') else ('O') end)
can this statement with the where-in-case work? i hope you get what i meant. thanks.
You can use nested case statement, like
SELECT user
FROM userlist zH with(nolock)
where 'true' =
(case when zh.trait = '1'
then
case when zH.user in ('B', 'HO', 'KO', 'PL','APP','2A','2B')
then 'true'
else 'false'
end
else
case when zH.user = 'O'
then 'true'
else 'false'
end
end)
SELECT [user]
FROM userlist zH WITH ( NOLOCK )
WHERE ( zh.trait = '1'
AND zH.[user] IN ( 'B', 'HO', 'KO', 'PL', 'APP', '2A', '2B' )
)
OR ( zh.trait <> '1'
AND zH.[user] IN ( 'O' )
)
I have a table that contains procedure data. There can be up to 49 entries for a given encounter. I want to output one row for each account and include the procedure, date and provider. I've tried writing CASE statements to this end:
Select DISTINCT [Encounter Number],
CASE When [Encounter Proc Sequence] = '1' Then [Procedure Code (Enctr)]
END as 'Proc1',
Case When [Encounter Proc Sequence] = '1' Then [Date of Service]
END as 'SvcDate1',
CASE When [Encounter Proc Sequence] = '1' Then [Surgeon]
END as 'Surgeon1',
CASE When [Encounter Proc Sequence] = '2' Then [Procedure Code (Enctr)]
END as 'Proc2',
Case When [Encounter Proc Sequence] = '2' Then [Date of Service]
END as 'SvcDate2',
CASE When [Encounter Proc Sequence] = '2' Then [Surgeon]
END as 'Surgeon2',
CASE When [Encounter Proc Sequence] = '3' Then [Procedure Code (Enctr)]
END as 'Proc3',
Case When [Encounter Proc Sequence] = '3' Then [Date of Service]
END as 'SvcDate3',
CASE When [Encounter Proc Sequence] = '3' Then [Surgeon]
END as 'Surgeon3',
CASE When [Encounter Proc Sequence] = '4' Then [Procedure Code (Enctr)]
END as 'Proc4',
Case When [Encounter Proc Sequence] = '4' Then [Date of Service]
END as 'SvcDate4',
CASE When [Encounter Proc Sequence] = '4' Then [Surgeon]
END as 'Surgeon4',
CASE When [Encounter Proc Sequence] = '5' Then [Procedure Code (Enctr)]
END as 'Proc5',
Case When [Encounter Proc Sequence] = '5' Then [Date of Service]
END as 'SvcDate5',
CASE When [Encounter Proc Sequence] = '5' Then [Surgeon]
END as 'Surgeon5',
CASE When [Encounter Proc Sequence] = '6' Then [Procedure Code (Enctr)]
END as 'Proc6',
Case When [Encounter Proc Sequence] = '6' Then [Date of Service]
END as 'SvcDate6',
CASE When [Encounter Proc Sequence] = '6' Then [Surgeon]
END as 'Surgeon6',
CASE When [Encounter Proc Sequence] = '7' Then [Procedure Code (Enctr)]
END as 'Proc7',
Case When [Encounter Proc Sequence] = '7' Then [Date of Service]
END as 'SvcDate7',
CASE When [Encounter Proc Sequence] = '7' Then [Surgeon]
END as 'Surgeon7'
from EncounterProc
where [Date of Service] between '20090101' and '20091231'
and [Procedure Code (ENCTR)] is not null
Group by [Encounter Number], [Encounter Proc Sequence],[Procedure Code (ENCTR)], [Date of
Service], Surgeon
The query output shows some duplicated rows and the query isn't placing procedure 3 (for example) in the Proc3 column.
How would I setup the PIVOT syntax for this scenario?
UPDATE:
I've read the MSDN Library documentation on PIVOT, and also the link shared in Comments. I took a stab at adapting those sources to my need as follows:
Select [Encounter Number],
[1] as Proc1,
[Date1] as SvcDate1,
[MD1] as MD1,
[2] as Proc2,
[Date2] as SvcDate2,
[MD2] as MD2,
[3] as Proc3,
[Date3] as SvcDate3,
[MD3] as MD3,
[4] as Proc4,
[Date4] as SvcDate4,
[MD4] as MD4
From
(Select * From
(SELECT [Encounter Number]
,[Procedure Code (Enctr)]
,Row_Number() OVER ( Partition By [Encounter Number] Order By
[Encounter Number], [Procedure Code (Enctr)] ) AS RowNumber
FROM EncounterProc)
PIVOT ( MAX([Procedure Code (Enctr)] ) for RowNumber IN
( [1], [Date1], [MD1],[2], [Date2], [MD2], [3], [Date3], [MD3], [4], [Date4],
[MD4]))
When I run this query, SSMS gives me the following error:
Incorrect syntax near the keyword 'PIVOT'.
Update2: Based on Stuart's comment, I've revised the query to be:
Select [Encounter Number],
p.[1] as Proc1,
p.[Date1] as SvcDate1,
p.[MD1] as MD1,
p.[2] as Proc2,
p.[Date2] as SvcDate2,
p.[MD2] as MD2,
p.[3] as Proc3,
p.[Date3] as SvcDate3,
p.[MD3] as MD3,
p.[4] as Proc4,
p.[Date4] as SvcDate4,
p.[MD4] as MD4
From
(Select * From
(SELECT [Encounter Number]
,[Procedure Code (Enctr)]
,[Date of Service]
,[Surgeon]
,Row_Number() OVER ( Partition By [Encounter Number] Order By
[Encounter Number], [Encounter Proc Sequence] ) AS RowNumber
FROM EncounterProc) p
PIVOT ( MAX([Procedure Code (Enctr)] ) for RowNumber IN
( [1], [Date1], [MD1],[2], [Date2], [MD2], [3], [Date3], [MD3], [4], [Date4],
[MD4]) )
I now receive a new error stating:
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near ')'.
which points to the closing parenthesis of the PIVOT statement.
What must be done to fix the syntax error?
Your first query is the closest one, except that you did not enclosed every case into aggreagate function. Also it seems that you do not need some columns in group by clause:
select
[Encounter Number],
Proc1 = max(CASE When [Encounter Proc Sequence] = '1' Then [Procedure Code (Enctr)] END),
SvcDate1 = max(Case When [Encounter Proc Sequence] = '1' Then [Date of Service] END),
Surgeon1 = max(CASE When [Encounter Proc Sequence] = '1' Then [Surgeon] END),
Proc2 = max(CASE When [Encounter Proc Sequence] = '2' Then [Procedure Code (Enctr)] END),
SvcDate2 = max(Case When [Encounter Proc Sequence] = '2' Then [Date of Service] END),
Surgeon2 = max(CASE When [Encounter Proc Sequence] = '2' Then [Surgeon] END),
Proc3 = max(CASE When [Encounter Proc Sequence] = '3' Then [Procedure Code (Enctr)] END),
SvcDate3 = max(Case When [Encounter Proc Sequence] = '3' Then [Date of Service] END),
Surgeon3 = max(CASE When [Encounter Proc Sequence] = '3' Then [Surgeon] END)
--- etc.
from EncounterProc
where [Date of Service] between '20090101' and '20091231'
and [Procedure Code (ENCTR)] is not null
group by [Encounter Number], [Encounter Proc Sequence]
I'm building a stored procedure that will serve as a Configuration String code reader that will take a #config varchar(255) variable.
This Configuration String determines 26 settings in our model.
Take one of the Configuration String entries:
declare #casingType varchar(50);
set #casingType=case SubString(#config, 34, 1)
when '1' then 'U-Flange - All Around w/ Stacking Flanges'
when '2' then 'U-Flange - All Around'
when '3' then 'U-Flange - No Top & Bottom'
when '4' then 'U-Flange - Flat Top & Bottom'
when '5' then 'Box Bracket - End Plates Only'
when '6' then 'Box Bracket - All Around'
when '7' then 'Slip & Drive Bracket'
when '8' then 'L Flange'
when '0' then 'No Casing'
when 'A' then '3 Sided Box - No Top & Bottom'
when 'B' then '3 Sided Box - Top & Bottom'
when 'C' then '3 Sided Box - Top or Bottom'
when 'D' then 'U-Flange w/ Stacking Plates'
when 'E' then 'U-Flange Temp Top & Bottom'
when 'F' then 'Flat Bracket'
when 'G' then 'A Coil Slab Bracket'
when 'H' then '2 Sided Box'
when 'I' then '3 Sided Box w/ Temp Top & Bottom'
when 'O' then 'One Plus One Casing'
when 'X' then 'Special'
when 'Y' then 'Auto Braze'
else 'Error' end;
What I want to return is a table of one (1) row containing the text fields of each item.
Do I create a temporary table to return or create some other type of table to return?
I hope this makes sense. It is possible that I worded it incorrectly or used inappropriate SQL jargon.
Solution: So, I got this to work, and here is the code I used to do it.
Often our Sales guys or Engineers working from the Configuration String do not know what a certain letter means, so they have to look it up. Over 70% of the time, this results in more letter code searches. So, we want to simply return all of the details associated with a particular configuration.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Joe Pool
-- Create date: 21-22 January 2013
-- Description: This returns a DataTable representation of the Model Configuration
-- =============================================
CREATE PROCEDURE sp1_Configurator(#config varchar(255)) as
BEGIN
SET NOCOUNT ON;
declare #len int;
declare #coilType varchar(50), #coilPattern varchar(50), #rowsDeep varchar(50);
declare #finHeight varchar(50), #finLength varchar(50), #finThickMat varchar(50);
declare #finPerInch varchar(50), #finTreatment varchar(50), #finCoating varchar(50);
declare #tubeWallThk varchar(50), #tubeType varchar(50), #qty varchar(50);
declare #tubeCoat varchar(50), #gauge varchar(50), #material varchar(50);
declare #casingType varchar(50), #customerCode varchar(50), #caseCoat varchar(50);
declare #arrangement varchar(50), #connType varchar(50), #connSize varchar(50);
declare #distributor varchar(50), #circuitry varchar(50), #coilApp varchar(50);
declare #agency varchar(50), #outsideCoat varchar(50);
declare #table table (
CoilType varchar(50) null, CoilPattern varchar(50) null, RowsDeep varchar(50) null,
FinHeight varchar(50) null, FinLength varchar(50) null, FinThickMat varchar(50) null,
FinPerInch varchar(50) null, FinTreatment varchar(50) null, FinCoating varchar(50) null,
TubeWallThk varchar(50) null, TubeType varchar(50) null, Qty varchar(50) null,
TubeCoat varchar(50) null, Gauge varchar(50) null, Material varchar(50) null,
CasingType varchar(50) null, CustomerCode varchar(50) null, CaseCoat varchar(50) null,
Arrangement varchar(50) null, ConnType varchar(50) null, ConnSize varchar(50) null,
Distributor varchar(50) null, Circuitry varchar(50) null, CoilApp varchar(50) null,
Agency varchar(50) null, OutsideCoat varchar(50) null
);
set #len=Len(#config)
if (#len=53) begin
set #coilType=case SubString(#config, 1, 1)
when 'C' then 'Slab'
when 'B' then '1 + 1'
when 'A' then 'A Coil (OBS)'
when 'X' then 'Special'
else 'Error' end;
set #coilPattern=case SubString(#config, 2, 1)
when '7' then '7mm Tube (.827 x .472 Staggered)'
when '6' then '5/16" Tube (1 x 5/8 Staggered)(OBS)'
when '3' then '3/8" Tube (1 x .866 Staggered)'
when 'P' then '1/2" Tube (1 1/4 x 1.08 Staggered)'
when '5' then '5/8" Tube (1 1/2 x 1.299 Staggered)'
else 'Error' end;
set #rowsDeep=SubString(#config, 3, 2);
set #finHeight=SubString(#config, 6, 5);
set #finLength=SubString(#config, 12, 6);
set #finThickMat=case SubString(#config, 19, 1)
when 'A' then '.0045 AL (OBS)'
when 'J' then '.0055 AL (OBS)'
when 'B' then '.0060 AL'
when 'C' then '.0075 AL'
when 'D' then '.0100 AL'
when 'E' then '.0045 CU (OBS)'
when 'K' then '.0050 CU (OBS)'
when 'F' then '.0060 CU'
when 'G' then '.0075 CU (OBS)'
when 'H' then '.0100 CU (OBS)'
when 'P' then '.0065 Pre-Coated'
when 'X' then 'Special'
else 'Error' end;
set #finPerInch=SubString(#config, 20, 2);
set #finTreatment=case SubString(#config, 22, 2)
when 'FS' then 'Flat / Straight (OBS)'
when 'FR' then 'Flat / Rippled (OBS)'
when 'CS' then 'Corrugated / Straight (OBS)'
when 'CR' then 'Corrugated / Rippled'
when 'SS' then 'Sine / Straight (OBS)'
when 'SR' then 'Sine / Rippled'
when 'LS' then 'Louvered / Straight (OBS)'
when 'LR' then 'Louvered / Rippled (OBS)'
when 'RL' then 'Embossed Arch / Rippled*'
when 'XX' then 'Special'
else 'Error' end;
set #finCoating=case SubString(#config, 24, 1)
when 'N' then 'See Coil Coating'
when 'A' then 'Alodine (OBS)'
when 'K' then 'Technicoat'
when 'P' then 'Paint Bond'
when 'X' then 'Special'
else 'Error' end;
set #tubeWallThk=case SubString(#config, 26, 1)
when 'A' then '.012 Smooth (5/16)(OBS)'
when 'B' then '.014 Smooth (3/8)(OBS)'
when 'C' then '.017 Smooth (1/2)'
when 'D' then '.018 Smooth (5/8)'
when 'E' then '.025 Smooth (1/2, 5/8)'
when 'F' then '.035 Smooth (1/2, 5/8)'
when 'G' then '.049 Smooth (5/8)'
when 'H' then '.012 Rifled (3/8)'
when 'K' then '.012 Rifled (7mm)'
when 'L' then '.016 Rifled (7mm) (OBS)'
when 'P' then '.06 Rifled (1/2)'
when 'X' then 'Special (.016 Rifled 3/8)(OBS)'
else 'Error' end;
set #tubeType=case SubString(#config, 27, 1)
when '1' then 'ST Flexpand'
when '2' then 'ST ALL DL Flexpand'
when '3' then 'ST w/DLST Flexpand'
when '5' then 'HP Flexpand'
when '6' then 'HP w/ST Flexpand'
when 'A' then 'ST w/DL*'
when 'B' then 'HP w/DL*'
when 'C' then 'Hairpin w/Straight*'
when 'D' then 'Hairpin 1/Hairpin .8* (OB)'
when 'E' then 'HP / DL / ST*'
when 'F' then 'HP / DL / ST / ST DL*'
when 'G' then 'HP / DL ST*'
when 'H' then 'Hairpint (HP)'
when 'I' then 'HP / ST / DL HP*'
when 'J' then '.8 HP* (OBS)'
when 'K' then 'One Short HP / One Long HP*'
when 'L' then 'HP w/Special DL*'
when 'M' then 'HP w/Special DL / ST*'
when 'N' then 'HP 1/HP .8/ST* (OBS)'
when 'O' then 'HP 1/HP .8/DL 1 HP/DL .8 HP* (OBS)'
when 'P' then 'All DL ST'
when 'Q' then 'ST w/Special DL ST*'
when 'R' then 'HP .8 / ST* (OBS)'
when 'S' then 'Straight (ST)'
when 'T' then 'Hydro Ball (HB)'
when 'U' then 'HB w/ DL*'
when 'V' then 'ST w/Spin Down*'
when 'W' then 'HP 7mm .827/.627 Angle*'
when 'Y' then 'HP 7mm All .627*'
when 'X' then 'Special*'
else 'Error' end;
set #qty=SubString(#config, 28, 2);
set #tubeCoat=case SubString(#config, 30, 1)
when 'N' then 'No Coating'
when 'T' then 'Tinned Plating (OBS)'
when 'X' then 'Special'
else 'Error' end;
set #gauge=case SubString(#config, 32, 1)
when '2' then '20 Gauge'
when '8' then '18 Gauge'
when '6' then '16 Gauge'
when '4' then '14 Gauge'
when '5' then '0.050" THK'
when '3' then '0.063" THK'
when '9' then '0.090" THK'
when 'X' then 'Special'
else 'Error' end;
set #material=case SubString(#config, 33, 1)
when 'G' then 'Galvanized'
when 'S' then 'Stainless'
when 'C' then 'Copper'
when 'A' then 'Aluminum'
when 'P' then 'Paint Bond'
when 'X' then 'Special'
else 'Error' end;
set #casingType=case SubString(#config, 34, 1)
when '1' then 'U-Flange - All Around w/ Stacking Flanges'
when '2' then 'U-Flange - All Around'
when '3' then 'U-Flange - No Top & Bottom'
when '4' then 'U-Flange - Flat Top & Bottom'
when '5' then 'Box Bracket - End Plates Only'
when '6' then 'Box Bracket - All Around'
when '7' then 'Slip & Drive Bracket'
when '8' then 'L Flange'
when '0' then 'No Casing'
when 'A' then '3 Sided Box - No Top & Bottom'
when 'B' then '3 Sided Box - Top & Bottom'
when 'C' then '3 Sided Box - Top or Bottom'
when 'D' then 'U-Flange w/ Stacking Plates'
when 'E' then 'U-Flange Temp Top & Bottom'
when 'F' then 'Flat Bracket'
when 'G' then 'A Coil Slab Bracket'
when 'H' then '2 Sided Box'
when 'I' then '3 Sided Box w/ Temp Top & Bottom'
when 'O' then 'One Plus One Casing'
when 'X' then 'Special'
when 'Y' then 'Auto Braze'
else 'Error' end;
set #customerCode=case SubString(#config, 35, 5)
when '00' then 'Standard'
when '14' then 'AAON Damper'
when '15' then 'AAON Cond.'
when '16' then 'AAON Evap.'
when 'XX' then 'Special'
else 'Error' end;
set #caseCoat=case SubString(#config, 37, 1)
when 'N' then 'See Coil Coating'
when 'A' then 'Alodine (OBS)'
when 'C' then 'Ceramic'
when 'X' then 'Special'
else 'Error' end;
set #arrangement=SubString(#config, 39, 2);
set #connType=case SubString(#config, 41, 1)
when '0' then 'No Connection'
when 'M' then 'MPT'
when 'F' then 'FPT'
when 'S' then 'Sweat'
when 'W' then 'Water Bead (OBS)'
when 'B' then 'Barbed FTG (OBS)'
when 'N' then 'Male Flare'
when 'G' then 'Female Flare'
when 'O' then 'Male O-Ring (OBS)'
when 'P' then 'Female O-Ring (OBS)'
when 'X' then 'Special'
else 'Error' end;
set #connSize=case SubString(#config, 42, 1)
when '0' then 'No Connection'
when '1' then '3/8 OD'
when '2' then '1/2 OD'
when '3' then '5/8 OD'
when '4' then '7/8 OD'
when '5' then '1-1/8 OD'
when '6' then '1-3/8 OD'
when '7' then '1-5/8 OD'
when '8' then '2-1/8 OD'
when '9' then '2-5/8 OD'
when 'A' then '3-1/8 OD'
when 'B' then '5/16 OD'
when 'C' then '3/4 OD'
when 'D' then '4-1/8 OD'
when 'E' then '3/16 OD'
when 'X' then 'Special'
else 'Error' end;
set #distributor=case SubString(#config, 44, 4)
when 'N000' then 'None Required'
when 'X001' then 'Special'
else 'Factory Assigned' end;
set #circuitry=case SubString(#config, 49, 2)
when 'SS' then 'Single Circuit'
when 'FF' then 'Full'
when 'HH' then 'Half'
when 'QQ' then 'Quarter'
when 'DD' then 'Double'
when 'DH' then '1-1/2'
when 'II' then 'Intertwined <2'
when 'RS' then 'Row Split <2'
when 'FS' then 'Face Split <2'
when '00' then 'No Circuitry'
when '01' then 'One Circuit'
when '02' then 'Two Circuits'
when '03' then 'Three Circuits'
when '04' then 'Four Circuits'
when '0S' then 'No Circuitry + SubCooler'
when '1S' then 'One Circuit + SubCooler'
when '2S' then 'Two Circuits + SubCooler'
when '3S' then 'Three Circuits + SubCooler'
when 'XX' then 'Special'
else 'Error' end;
set #coilApp=case SubString(#config, 51, 1)
when 'D' then 'Drainable Water'
when 'W' then 'Water'
when 'G' then 'Cond / SubCooler'
when 'C' then 'Condenser'
when 'E' then 'Evaporator'
when 'S' then 'Steam'
when 'N' then 'Steam Distributor'
when 'B' then 'Booster'
when 'H' then 'Heat Reclaim'
when 'P' then 'Heat Pipe (OBS)'
when 'L' then 'Glycol'
when 'O' then 'Oil (OBS)'
when 'X' then 'Special'
else 'Error' end;
set #agency=case SubString(#config, 52, 1)
when '0' then 'None'
when 'A' then 'ARI'
when 'B' then 'ARI + UL / CSA'
when 'C' then 'UL / CSA'
when 'E' then 'ETL / DOE'
else 'Error' end;
set #outsideCoat=case SubString(#config, 53, 1)
when 'N' then 'No Coating'
when 'A' then 'Ceramic'
when 'C' then 'Chromocoat'
when 'E' then 'Epoxy'
when 'G' then 'Americoat Grey (OBS)'
when 'H' then 'Heresite'
when 'K' then 'Phenolic (Technicoat)'
when 'L' then 'ElectroFin'
when 'P' then 'Phenolic (OBS)'
when 'X' then 'Special'
else 'Error' end;
insert into #table
(CoilType, CoilPattern, RowsDeep, FinHeight, FinLength, FinThickMat, FinPerInch, FinTreatment, FinCoating,
TubeWallThk, TubeType, Qty, TubeCoat, Gauge, Material, CasingType, CustomerCode, CaseCoat, Arrangement,
ConnType, ConnSize, Distributor, Circuitry, CoilApp, Agency, OutsideCoat)
values
(#coilType, #coilPattern, #rowsDeep, #finHeight, #finLength, #finThickMat, #finPerInch, #finTreatment, #finCoating,
#tubeWallThk, #tubeType, #qty, #tubeCoat, #gauge, #material, #casingType, #customerCode, #caseCoat, #arrangement,
#connType, #connSize, #distributor, #circuitry, #coilApp, #agency, #outsideCoat);
end
select * from #table;
END
GO
Here is what an actual Configuration String looks like:
CP06-51.25-051.50-B12SRN-CT00N-8G2XXN-A2S8-N000-HHWAN
Have you thought about creating a user-defined function:
create function CastingTypeFunction(#config varchar(255))
returns varchar(50)
as
begin
declare #casingType varchar(50)
set #casingType=case SubString(#config, 34, 1)
when '1' then 'U-Flange - All Around w/ Stacking Flanges'
when '2' then 'U-Flange - All Around'
when '3' then 'U-Flange - No Top & Bottom'
when '4' then 'U-Flange - Flat Top & Bottom'
when '5' then 'Box Bracket - End Plates Only'
when '6' then 'Box Bracket - All Around'
when '7' then 'Slip & Drive Bracket'
when '8' then 'L Flange'
when '0' then 'No Casing'
when 'A' then '3 Sided Box - No Top & Bottom'
when 'B' then '3 Sided Box - Top & Bottom'
when 'C' then '3 Sided Box - Top or Bottom'
when 'D' then 'U-Flange w/ Stacking Plates'
when 'E' then 'U-Flange Temp Top & Bottom'
when 'F' then 'Flat Bracket'
when 'G' then 'A Coil Slab Bracket'
when 'H' then '2 Sided Box'
when 'I' then '3 Sided Box w/ Temp Top & Bottom'
when 'O' then 'One Plus One Casing'
when 'X' then 'Special'
when 'Y' then 'Auto Braze'
else 'Error' end
return #casingType
end;
You will then pass your #config value into the function similar to this:
select dbo.CastingTypeFunction(#config)
Your value will then be returned.
See SQL Fiddle with Demo.
The other option that I can see that you have is to create a table that contains each of these values and you would join on that table to return the casingType.
The tables would be similar to this:
CREATE TABLE CasingType
([CasingTypeId] varchar(1), [CasingValue] varchar(41))
;
And then you would join it similar to this:
select
case
when c.casingtypeid is null
then 'Error'
else c.casingvalue end
from
(
select '12323212334234231211231212121qwe1212312334234234' config -- replace with your config values
) src
left join CasingType c
on SubString(src.config, 34, 1) = c.casingtypeid
See SQL Fiddle with Demo