I am using the below case statement
***, CASE WHEN Attributes LIKE ',Size = %,' Then right(Attributes, len(Attributes) - charindex('Size ', Attributes)) ELSE '' END as Size***
To get the below result
,Size = XXL ,Fits to Chest Size = 48 to 50 in,,Closure Type = Snap Button ,Material = Cotton ,Color = Khaki ,Sleeve Length = 33 in,Lining Material = Polyester Thread ,,Fabric Weight = 9 oz,,,Resists = Flame ,,,,,,,,,,,,,,,,,,,,,,,,,,
I want to remove everything and only have Size = XXL
Any help is appreciated.
If this is not an ETL process, then you should really consider changing your data model so you that you do not have to parse your Attribute column every time.
Below is a short solution with
STRING_SPLIT() (SQL Server 2016 and later)
TRIM() (SQL Server 2017 and later)
Sample data
create table CsvData
(
Attribute nvarchar(1000)
);
insert into CsvData (Attribute) values
('Foo = 10,Bar = 20,,,,,Size = XXL ,Fits to Chest Size = 48 to 50 in,,Closure Type = Snap Button ,Material = Cotton ,Color = Khaki ,Sleeve Length = 33 in,Lining Material = Polyester Thread ,,Fabric Weight = 9 oz,,,Resists = Flame ,,,,,,,,,,,,,,,,,,,,,,,,,,');
Solution
select trim(s.value) as SizeAttribute
from CsvData cd
cross apply string_split(cd.Attribute, ',') s
where left(s.value, 4) = 'Size';
Result
SizeAttribute
-------------
Size = XXL
Fiddle to see it in action.
Try This:
DECLARE #CsvData TABLE(Attribute nvarchar(1000));
insert into #CsvData (Attribute) values
('Foo = 10,Bar = 20,,,,,Size = XXL ,Fits to Chest Size = 48 to 50 in,,Closure Type =
Snap Button ,Material = Cotton ,Color = Khaki ,Sleeve Length = 33 in,Lining Material =
Polyester Thread ,,Fabric Weight = 9 oz,,,Resists = Flame ,,,,,,,,,,,,,,,,,,,,,,,,,,');
DECLARE #xml_value AS XML,
#string_value AS VARCHAR(2000),
#delimiter_value AS VARCHAR(15)
SET #string_value=(SELECT Attribute FROM #CsvData)
SET #delimiter_value =','
SET #xml_value = Cast(( '<studentname>'+ Replace(#string_value, #delimiter_value, '</studentname><studentname>') + '</studentname>' ) AS XML)
SELECT x.m.query('.').value('.', 'VARCHAR(15)') AS VALUE
FROM #xml_value.nodes('/studentname') AS x(m)
WHERE x.m.query('.').value('.', 'VARCHAR(15)') LIKE 'Size%'
Related
I am using mariadb 10.3 and I'm trying to create a procedure that modifies all characters of a string, to each CHAR ASCII code, 10 position ahead.
I'm having trouble to find any functions to approach this problem, thanks.
For example given the string 'man':
ASCII codes: m = 109, a = 97, n = 110
Plus 10: 119 = w, 107 = k, 120 = x
So the function should return: 'wkx'
This works:
DECLARE longitud smallint;
DECLARE nuevaCadena varchar(100);
set longitud = LENGTH(cadena);
set nuevaCadena = '';
FOR i IN 1 .. longitud DO
set nuevaCadena = concat(nuevaCadena,char(ascii(substring(cadena,i)) + 10));
END FOR;
How do I add the values in a field based on the same values in another field using FOR loop?
Types:
Begin of ty_final,
doctype type char5,
posnr type char5,
total type quan5,
End of ty_final.
DATA(lt_final) = VALUE ty_final(
FOR line IN lt_history
WHERE ( hist_type = 'U' )
( doctype = line-hist_type
total = line-quantity
posnr = line-po_item ) ).
What I have in LT_HISTORY:
HIST_TYPE POSNR QUANTITY
U 10 5
U 10 2
U 20 3
U 20 -3
What I need in LT_FINAL:
DOCTYPE POSNR QUANTITY
U 10 7
U 20 0
I am trying to use GROUP BY to get the sum of the values in TOTAL field based on POSNR and DOCTYPE fields. It's just that I am not sure where exactly I need to add GROUP BY in my FOR loop. REDUCE makes my head spin. So I was trying out as simple as possible.
Below is minimal reproducible example, which uses ABAP Unit (Ctrl+Shift+F10 to run it). I commented your code and replaced it with the solution:
CLASS ltc_test DEFINITION FOR TESTING
DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
METHODS test FOR TESTING.
ENDCLASS.
CLASS ltc_test IMPLEMENTATION.
METHOD test.
TYPES: BEGIN OF ty_line,
doctype TYPE c LENGTH 1,
posnr TYPE i,
quantity TYPE i,
END OF ty_line,
ty_table TYPE STANDARD TABLE OF ty_line WITH DEFAULT KEY.
DATA(lt_history) = VALUE ty_table(
( doctype = 'U' posnr = 10 quantity = 5 )
( doctype = 'U' posnr = 10 quantity = 2 )
( doctype = 'U' posnr = 20 quantity = 3 )
( doctype = 'U' posnr = 20 quantity = -3 ) ).
DATA(lt_final) = VALUE ty_table(
* FOR line IN lt_history
* WHERE ( doctype = 'U' )
* ( doctype = line-doctype
* quantity = line-quantity
* posnr = line-posnr ) ).
FOR GROUPS grp_line OF line IN lt_history
WHERE ( doctype = 'U' )
GROUP BY ( doctype = line-doctype posnr = line-posnr )
( doctype = grp_line-doctype
quantity = REDUCE #( INIT t = 0 FOR <line> IN GROUP grp_line
NEXT t = t + <line>-quantity )
posnr = grp_line-posnr ) ).
cl_abap_unit_assert=>assert_equals( act = lt_final exp = VALUE ty_table(
( doctype = 'U' posnr = 10 quantity = 7 )
( doctype = 'U' posnr = 20 quantity = 0 ) ) ).
ENDMETHOD.
ENDCLASS.
I have a validation in WHERE clause like:
...
AND (#BDOnly = 0
OR [DT].[Abbreviation] = #IsBDChecked)
AND (#CDOnly = 0
OR [DT].[Abbreviation] = #IsCDChecked)
AND (#PPOOnly = 0
OR [DT].[Abbreviation] = #IsPPOChecked)
AND (#FBOMOnly = 0
OR [DT].[Abbreviation] = #IsFBOMChecked)
AND (#APOnly = 0
OR [DT].[Abbreviation] = #IsAPChecked)
AND (#COOnly = 0
OR [DT].[Abbreviation] = #IsCOChecked)
So each AND clause check if boolean is bit value is 0 or 1, if it's 0 just do any but if it's 1 it do a filter. My problem is
if I'm sending two with value 1, I mean
#BDOnly = 1 and #CDOnly = 1 it only filter one of them instead get two filters I mean:
[DT].[Abbreviation] = #IsBDChecked
and
[DT].[Abbreviation] = #IsCDChecked
What am I doing wrong? Regards
I'm going to take a guess here because there's not enough data. If you want to return a row where Abbreviation matched #IsBDChecked only when #BDOnly is set, and also another row where Abbreviation matches #IsCDChecked when #CDOnly is set, try this:
(#BDOnly = 1
AND [DT].[Abbreviation] = #IsBDChecked)
OR (#CDOnly = 1
AND [DT].[Abbreviation] = #IsCDChecked)
OR (#PPOOnly = 1
AND [DT].[Abbreviation] = #IsPPOChecked)
OR (#FBOMOnly = 1
AND [DT].[Abbreviation] = #IsFBOMChecked)
OR (#APOnly = 1
AND [DT].[Abbreviation] = #IsAPChecked)
OR (#COOnly = 1
AND [DT].[Abbreviation] = #IsCOChecked)
Small representative case:
DECLARE #ADOnly BIT = 0
, #BDOnly BIT = 1
, #CDOnly BIT = 1
, #IsADChecked NVARCHAR(100) = 'A'
, #IsCDChecked NVARCHAR(100) = 'C'
, #IsBDChecked NVARCHAR(100) = 'B'
;WITH myTable AS
(
SELECT * FROM (VALUES ('A'), ('B'), ('C')) X(Abbreviation)
)
SELECT *
FROM myTable
WHERE (#ADOnly = 1 AND Abbreviation = #IsADChecked)
OR (#BDOnly = 1 AND Abbreviation = #IsBDChecked)
OR (#CDOnly = 1 AND Abbreviation = #IsCDChecked)
Yields:
Abbreviation
------------
B
C
I want to read the position using this FM HRWPC_RPT_COSTCENTER_EVALPATH where the cost center is given.
There are 3 result tables. from which table I can read the position value ?
here how I call the FM:
DATA i_hrrootob TYPE TABLE OF hrrootob.
DATA w_hrrootob LIKE LINE OF i_hrrootob.
DATA i_object_tab TYPE TABLE OF objec.
DATA w_object_tab LIKE LINE OF i_object_tab.
data i_STRUC TYPE TABLE OF STRUC.
w_hrrootob-otype = 'K'.
w_hrrootob-objid = w_orgdata-costcenter_key-costcenter.
APPEND w_hrrootob TO i_hrrootob.
CALL FUNCTION 'HRWPC_RPT_COSTCENTER_EVALPATH'
EXPORTING
depth = 0
evpath = 'KOSTDIUP'
* PLVAR = 01
* BEGDA = SY-DATUM
* ENDDA = SY-DATUM
* LEVEL = 1
TABLES
root_objects = i_hrrootob
result_objec = i_object_tab
result_struc = i_STRUC
EXCEPTIONS
NO_OBJECTS_FOUND = 1
OTHERS = 2
.
I got it by myself.
The result table result_objec has the value in the field stext, where the obtype ='S'
MY QUERY IS
DECLARE #AutoApprove BIT
SET #AutoApprove = (
SELECT AutoApprove
FROM dbo.CommentBox_Setting
WHERE UserModuleID = #myModuleID
AND PortalID = #portalID
AND CultureCode = #cultureCode
)
From this i will get whether 1 OR 0 (TRUE OR FALSE) furthermore i have
SELECT * FROM ComentBox_Comment
WHERE UpperModuleID = #UpperModuleID
AND ModuleID = #myModuleID
AND portalID = #portalID
AND cultureCode = #cultureCode
AND //Here i need to check condition
(IF(#AutoApprove=0){ THEN isapprove=1}else {do not check})
Note here isapprove is table filedName
I know ,i can do this with long query i need short and easy way.
Help me out.
Try something like
AND CASE WHEN #AutoApprove=0 THEN isapprove ELSE 1 END = 1
This will check isapprove = 1 if #AutoApprove = 0, or 1=1(ignore) otherwise.
CASE (Transact-SQL)