Related
I am trying to get string between second and third instance of ",," using SQL SELECT.
Apparently functions substring and charindex are useful, and I have tried them but the problem is that I need the string between those specific ",,"s and the length of the strings between them can change.
Can't find working example anywhere.
Here is an example:
Table: test
Column: Column1
Row1: cat1,,cat2,,cat3,,cat4,,cat5
Row2: dogger1,,dogger2,,dogger3,,dogger4,,dogger5
Result: cat3dogger3
Here is my closest attempt, it works if the strings are same length every time, but they aren't:
SELECT SUBSTRING(column1,LEN(LEFT(column1,CHARINDEX(',,', column1,12)+2)),LEN(column1) - LEN(LEFT(column1,CHARINDEX(',,', column1,20)+2)) - LEN(RIGHT(column1,CHARINDEX(',,', (REVERSE(column1)))))) AS column1
FROM testi
Just repeat sub-string 3 times, each time moving onto the next ",," e.g.
select
-- Substring till the third ',,'
substring(z.col1, 1, patindex('%,,%',z.col1)-1)
from (values ('cat1,,cat2,,cat3,,cat4,,cat5'),('dogger1,,dogger2,,dogger3,,dogger4,,dogger5')) x (col1)
-- Substring from the first ',,'
cross apply (values (substring(x.col1,patindex('%,,%',x.col1)+2,len(x.col1)))) y (col1)
-- Substring from the second ',,'
cross apply (values (substring(y.col1,patindex('%,,%',y.col1)+2,len(y.col1)))) z (col1);
And just to reiterate, this is a terrible way to store data, so the best solution is to store it properly.
Here is an alternative solution using charindex. The base idea is the same as in Dale K's an answer, but instead of cutting the string, we specify the start_location for the search by using the third, optional parameter, of charindex. This way, we get the location of each separator, and could slip each value off from the main string.
declare #vtest table (column1 varchar(200))
insert into #vtest ( column1 ) values('dogger1,,dogger2,,dogger3,,dogger4,,dogger5')
insert into #vtest ( column1 ) values('cat1,,cat2,,cat3,,cat4,,cat5')
declare #separetor char(2) = ',,'
select
t.column1
, FI.FirstInstance
, SI.SecondInstance
, TI.ThirdInstance
, iif(TI.ThirdInstance is not null, substring(t.column1, SI.SecondInstance + 2, TI.ThirdInstance - SI.SecondInstance - 2), null)
from
#vtest t
cross apply (select nullif(charindex(#separetor, t.column1), 0) FirstInstance) FI
cross apply (select nullif(charindex(#separetor, t.column1, FI.FirstInstance + 2), 0) SecondInstance) SI
cross apply (select nullif(charindex(#separetor, t.column1, SI.SecondInstance + 2), 0) ThirdInstance) TI
For transparency, I saved the separator string in a variable.
By default the charindex returns 0 if the search string is not present, so I overwrite it with the value null, by using nullif
IMHO, SQL Server 2016 and its JSON support in the best option here.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, Tokens VARCHAR(500));
INSERT INTO #tbl VALUES
('cat1,,cat2,,cat3,,cat4,,cat5'),
('dogger1,,dogger2,,dogger3,,dogger4,,dogger5');
-- DDL and sample data population, end
WITH rs AS
(
SELECT *
, '["' + REPLACE(Tokens
, ',,', '","')
+ '"]' AS jsondata
FROM #tbl
)
SELECT rs.ID, rs.Tokens
, JSON_VALUE(jsondata, '$[2]') AS ThirdToken
FROM rs;
Output
+----+---------------------------------------------+------------+
| ID | Tokens | ThirdToken |
+----+---------------------------------------------+------------+
| 1 | cat1,,cat2,,cat3,,cat4,,cat5 | cat3 |
| 2 | dogger1,,dogger2,,dogger3,,dogger4,,dogger5 | dogger3 |
+----+---------------------------------------------+------------+
It´s the same as #"Yitzhak Khabinsky" but i think it looks clearer
WITH CTE_Data
AS(
SELECT 'cat1,,cat2,,cat3,,cat4,,cat5' AS [String]
UNION
SELECT 'dogger1,,dogger2,,dogger3,,dogger4,,dogger5' AS [String]
)
SELECT
A.[String]
,Value3 = JSON_VALUE('["'+ REPLACE(A.[String], ',,', '","') + '"]', '$[2]')
FROM CTE_Data AS A
I want to get sub string
in between two operators like = and ,
I tried something like CHARINDEX and LEFT to get the value but i got output in terms of
CN=Khushwant Khatri
but my output should be only Khushwant Khatri
SELECT left([String_value],CHARINDEX(',',([String_value]),0)-1) from trim_string
My string look like
CN=Khushwant Khatri,OU=TestMig,DC=valjha,DC=vedantaresource,DC=local
CN=Raghav Tare,OU=EXECUTIVE,OU=EXUDR,DC=HZL01,DC=vedantaresource,DC=local
CN=D K Chodankar,OU=Users,OU=AD LotusSync,DC=SGL01,DC=vedantaresource,DC=local
as you can see string has variable length
i want only CN value my output should look like
Khushwant Khatri
Raghav Tare
D K Chodankar
You may try this. First you need to find the position of your first =, since name will start from there, then need to find the length of your name which is separated by ,. So we find the index of next , and substract it from the length of string till =. Remaining string is your name as expected.
I am considering that your first value 'CN=' may vary for some condition.
declare #str varchar(max) = 'CN=Khushwant Khatri,OU=TestMig,DC=valjha,DC=vedantaresource,DC=local'
select substring( #str, charindex('=',#str)+1, (charindex(',',#str) - (charindex('=',#str) +1) ))
As per given table structure details please find the below code snippet.
Create table trim_string ( string_value nvarchar(max) )
Insert into trim_string ( string_value )
values ( 'CN=Khushwant Khatri,OU=TestMig,DC=valjha,DC=vedantaresource,DC=local' )
, ( 'CN=Raghav Tare,OU=EXECUTIVE,OU=EXUDR,DC=HZL01,DC=vedantaresource,DC=local' )
, ( 'CN=D K Chodankar,OU=Users,OU=AD LotusSync,DC=SGL01,DC=vedantaresource,DC=local' )
----- this is your query section
; with cte as (
select string_value , substring( string_value, charindex('=',string_value)+1, (charindex(',',string_value) - (charindex('=',string_value) +1) )) newvalue
from trim_string )
---- you may store them in temptable
select * into #temp from cte
----or
---- for selection
select * from cte
----or
----- use to update
update cte set string_value = newvalue
select * from trim_string
As per the comments discussion please try this.
; with cte as (
select string_value , substring( string_value, charindex('=',string_value)+1, (charindex(',',string_value) - (charindex('=',string_value) +1) )) newvalue
from trim_string )
select * from cte
This should give you all of the strings record of your table, with Name is fetched in new column as newvalue.
Use SUBSTRING() as
SELECT SUBSTRING(S, 4, CHARINDEX(',', S)-4),
S
FROM
(
VALUES
('CN=Khushwant Khatri,OU=TestMig,DC=valjha,DC=vedantaresource,DC=local'),
('CN=Raghav Tare,OU=EXECUTIVE,OU=EXUDR,DC=HZL01,DC=vedantaresource,DC=local'),
('CN=D K Chodankar,OU=Users,OU=AD LotusSync,DC=SGL01,DC=vedantaresource,DC=local')
) T(S);
Try this:
select substring(#t,charindex('=',#t)+1,charindex(',',#t)-4)
I am not exactly sure what you want. Alse working with CHARINDEX is a bit unclear.
But I was thinking that maybe this piece of query can help you to find what you want in your string better.
DECLARE #string VARCHAR(MAX) = 'CN=Khushwant Khatri,OU=TestMig,DC=valjha,DC=vedantaresource,DC=local,
,CN=Raghav Tare,OU=EXECUTIVE,OU=EXUDR,DC=HZL01,DC=vedantaresource,DC=local
,CN=D K Chodankar,OU=Users,OU=AD LotusSync,DC=SGL01,DC=vedantaresource,DC=local'
SELECT *
FROM (
SELECT -- this part is based on what your question looks it want.
CASE WHEN LEN(X.value)>2 THEN X.value END AS Value
FROM
(
SELECT value
FROM string_split(REPLACE(#string,',','='),'=') -- might help more than CHARINDEX
) X
) Y
WHERE Y.Value IS NOT NULL
I have to make a query to a base using as a comparison a string like this 12345678, but the value to compare is this way12.345.678, if I do the following query it does not return anything.
SELECT * FROM TABLA WHERE CAMPO = '12345678'
Where CAMPO would have the value of (12.345.678), if I replace = with a like, it does not return the data either
SELECT * FROM TABLA WHERE CAMPO like '12345678%'
SELECT * FROM TABLA WHERE CAMPO like '%12345678'
SELECT * FROM TABLA WHERE CAMPO like '%12345678%'
None of the 3 previous consultations works for me, how can I make this query?
The value can be of either 7, 8 or 9 numbers and the. It has to be every 3 from the end to the beginning
Use REPLACE() function to replace all the dots '.' as
SELECT *
FROM(
VALUES ('12.345.678'),
('23.456.789')
) T(CAMPO)
WHERE REPLACE(CAMPO, '.', '') = '12345678';
Your query should be
SELECT * FROM TABLA WHERE REPLACE(CAMPO, '.', '') = '12345678';
You can compare the string without the dots to a REPLACE(StringWithDots, '.','')
I recommend you to convert the number to numeric
So you can use < and > operators and all functions that require you to have a number...
the best way to achieve this is to make sure you remove any unecessary dots and convert the commas to dots. like this
CONVERT(NUMERIC(10, 2),
REPLACE(
REPLACE('7.000,45', '.', ''),
',', '.'
)
)
I hope this will help you out.
A SARGABLE solution would be to write a function that takes your target value ('12345678') and inserts the separators ('.') every third character from right to left. The result ('12.345.678') can then be used in a where clause and benefit from an index on CAMPO.
The following code demonstrates an approach without creating a user-defined function (UDF). Instead, a recursive common table expression (CTE) is used to process the input string three characters at a time to build the dotted target string. The result is used in a query against a sample table.
To see the results from the recursive CTE replace the final select statement with the commented select immediately above it.
-- Sample data.
declare #Samples as Table ( SampleId Int Identity, DottedDigits VarChar(20) );
insert into #Samples ( DottedDigits ) values
( '1' ), ( '12' ), ( '123' ), ( '1.234' ), ( '12.345' ),
( '123.456' ), ( '1.234.567' ), ( '12.345.678' ), ( '123.456.789' );
select * from #Samples;
-- Query the data.
declare #Target as VarChar(15) = '12345678';
with
Target as (
-- Get the first group of up to three characters from the tail of the string ...
select
Cast( Right( #Target, 3 ) as VarChar(20) ) as TargetString,
Cast( Left( #Target, case when Len( #Target ) > 3 then Len( #Target ) - 3 else 0 end ) as VarChar(20) ) as Remainder
union all
-- ... and concatenate the next group with a dot in between.
select
Cast( Right( Remainder, 3 ) + '.' + TargetString as VarChar(20) ),
Cast( Left( Remainder, case when Len( Remainder ) > 3 then Len( Remainder ) - 3 else 0 end ) as VarChar(20) )
from Target
where Remainder != ''
)
-- To see the intermediate results replace the final select with the line commented out below:
--select TargetString from Target;
select SampleId, DottedDigits
from #Samples
where DottedDigits = ( select TargetString from Target where Remainder = '' );
An alternative approach would be to add a indexed computed column to the table that contains Replace( CAMPO, '.', '' ).
If the table containing IDs like 12.345.678 is big (contains many records), I would add a computed field that removes the dots (and if this ID does never contain any alphanumeric characters other than dots and has no significant leading zeros then also cast it in an INT or BIGINT) and persist it and lay an index over it. That way you loose a little time when inserting the record but are querying it with maximum speed and therefore saving processor power.
I have a query that decompiles the value stored in the varbinary type. If I remove from the varbinary value 00, I get the data that gives me the correct result. How to remove the value of 00 from this type without changing it to varchar, or how to return after changing to varchar and converting individual characters to the correct varbinary entry.
if I use swapping on varchar then I can not use the sql query:
REPLACE(CONVERT(varchar(max),val,1),'00','')
SQL Query:
DECLARE #MyTable TABLE(id int identity(1,1) NOT NULL PRIMARY KEY,val varbinary(max));
INSERT INTO #MyTable(val)
SELECT 0x4D5A9304FFFFB8408E1FBA0EB409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2450454C010355CBC65CE2210B010B0A06DE2920401022040482034085101010101090294B40B8026C280820482E74657874E4092A0220602E72737263B802440C40402E72656C6F630C62104042C0294802051C26740301C0205A050332557E040A6F050A1A2E447E040A6F050A1C2E377E040A6F050A1E2E2A7E040A6F050A1F0B2E1C7E040A6F050A1F162E0E7E040A6F050A1F293302162A172A1E0228060A2A5605FFFE3C3F786D6C2076657273696F6E3D22312E302220656E636F64696E673D227574662D3136223F3E0D0A3C4C69746543616C6C6261636B47656E657261746F7220786D6C6E733A7873693D22687474703A2F2F7777772E77332E6F72672F323030312F584D4C536368656D612D696E7374616E63652220786D6C6E733A7873643D22687474703A2F2F7777772E77332E6F72672F323030312F584D4C536368656D61223E0D0A20203C416374696F6E47726F75703E0D0A202020203C436F6E646974696F6E3E696620280A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D203429207C7C202A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D203629207C7C202A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D203829207C7C202A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D20313129207C7C202A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D20323229207C7C202A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D20343129202A290A7B0A72657475726E2066616C73653B0A7D0A656C73650A7B0A72657475726E20747275653B0A7D3C2F436F6E646974696F6E3E0D0A202020203C4E616D653E4E6F77612067727570613C2F4E616D653E0D0A20203C2F416374696F6E47726F75703E0D0A3C2F4C69746543616C6C6261636B47656E657261746F723E42534A4201010C76342E302E3330333139056C2401237E91380123537472696E6773C80208235553D2102347554944E29423426C6F62020147140901FA2533160106020206030102010A0106362F0A676106AC8C06CC8C0AF7610AFF6101010101101B0501015020963D0A01B1208618520E0111521B19522521520E29FF2A3117012E09520E2E0B322E136C2E1B750480EA040126E3071258012A013C4D6F64756C653E48796472615F526573756C742E646C6C436F6E646974696F6E736D73636F726C696253797374656D4F626A656374436F6E646974696F6E5F4E6F77615F67727570612E63746F7243646E4879647261487964726143616C6C6261636B417373656D626C794465736372697074696F6E41747472696275746553797374656D2E52756E74696D652E436F6D70696C65725365727669636573436F6D70696C6174696F6E52656C61786174696F6E7341747472696275746552756E74696D65436F6D7061746962696C69747941747472696275746548796472615F526573756C7452756E74696D65436F6E66696775726174696F6E44696374696F6E6172796765745F4E756D65724F70657261746F72616465736372697074696F6E0320252267F49CC8B641B90894AB9E62023108B77A5C561934E08903020321084B3653AA0A875D040926010E0E0E0E0E0E04210108030612190328390110546573746F776150726F66696C7465720841545543484F4C5303312E3A323031392E302E302E3A32392D30342D323031390801081E0101540216577261704E6F6E457863657074696F6E5468726F777301B829CE2920C0295F436F72446C6C4D61696E6D73636F7265652E646C6CFF2520111018810130814858405C025C023456535F56455253494F4E5F494E464FBD04EFFE013F0402440156617246696C65496E666F24045472616E736C6174696F6EB4BC0101537472696E6746696C65496E666F98010130303030303462302C020146696C654465736372697074696F6E20380146696C6556657273696F6E302E302E302E30441101496E7465726E616C4E616D6548796472615F526573756C742E646C6C2802014C6567616C436F70797269676874204C11014F726967696E616C46696C656E616D6548796472615F526573756C742E646C6C34080150726F6475637456657273696F6E302E302E302E30380801417373656D626C792056657273696F6E302E302E302E302CE039
UNION ALL
SELECT 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000504500004C01030055CBC65C0000000000000000E00002210B010B00000A00000006000000000000DE290000002000000040000000000010002000000002000004000000000000000400000000000000008000000002000000000000030040850000100000100000000010000010000000000000100000000000000000000000902900004B00000000400000B802000000000000000000000000000000000000006000000C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000080000000000000000000000082000004800000000000000000000002E74657874000000E409000000200000000A000000020000000000000000000000000000200000602E72737263000000B80200000040000000040000000C0000000000000000000000000000400000402E72656C6F6300000C0000000060000000020000001000000000000000000000000000004000004200000000000000000000000000000000C02900000000000048000000020005001C260000740300000100000000000000C02000005A050000000000000000000000000000000000000000000000000000000000000000000000000000000000000330020055000000000000007E0400000A6F0500000A1A2E447E0400000A6F0500000A1C2E377E0400000A6F0500000A1E2E2A7E0400000A6F0500000A1F0B2E1C7E0400000A6F0500000A1F162E0E7E0400000A6F0500000A1F293302162A172A1E02280600000A2A0000000000000056050000FFFE3C003F0078006D006C002000760065007200730069006F006E003D00220031002E0030002200200065006E0063006F00640069006E0067003D0022007500740066002D003100360022003F003E000D000A003C004C00690074006500430061006C006C006200610063006B00470065006E0065007200610074006F007200200078006D006C006E0073003A007800730069003D00220068007400740070003A002F002F007700770077002E00770033002E006F00720067002F0032003000300031002F0058004D004C0053006300680065006D0061002D0069006E007300740061006E00630065002200200078006D006C006E0073003A007800730064003D00220068007400740070003A002F002F007700770077002E00770033002E006F00720067002F0032003000300031002F0058004D004C0053006300680065006D00610022003E000D000A00200020003C0041006300740069006F006E00470072006F00750070003E000D000A0020002000200020003C0043006F006E0064006900740069006F006E003E0069006600200028000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D0020003400290020007C007C00200020000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D0020003600290020007C007C00200020000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D0020003800290020007C007C00200020000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D00200031003100290020007C007C00200020000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D00200032003200290020007C007C00200020000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D002000340031002900200020000A0029000A007B000A00720065007400750072006E002000660061006C00730065003B000A007D000A0065006C00730065000A007B000A00720065007400750072006E00200074007200750065003B000A007D003C002F0043006F006E0064006900740069006F006E003E000D000A0020002000200020003C004E0061006D0065003E004E006F00770061002000670072007500700061003C002F004E0061006D0065003E000D000A00200020003C002F0041006300740069006F006E00470072006F00750070003E000D000A003C002F004C00690074006500430061006C006C006200610063006B00470065006E0065007200610074006F0072003E00000042534A4201000100000000000C00000076342E302E33303331390000000005006C00000024010000237E0000900100003801000023537472696E677300000000C80200000800000023555300D0020000100000002347554944000000E00200009400000023426C6F620000000000000002000001471400000901000000FA25330016000001000000060000000200000002000000060000000300000001000000020000000100000000000A00010000000000060036002F000A00670061000600AC008C000600CC008C000A00F70061000A00FF0061000000000001000000000001000100010010001B00000005000100010050200000000096003D000A000100B12000000000861852000E000100110052001B00190052002500210052000E002900FF002A00310017012E00090052000E002E000B0032002E0013006C002E001B007500048000000000000000000000000000000000EA0000000400000000000000000000000100260000000000E30700000000000000000000120058000000000000000000010000002A0100000000003C4D6F64756C653E0048796472615F526573756C742E646C6C00436F6E646974696F6E73006D73636F726C69620053797374656D004F626A65637400436F6E646974696F6E5F4E6F77615F6772757061002E63746F720043646E48796472610048796472610043616C6C6261636B417373656D626C794465736372697074696F6E4174747269627574650053797374656D2E52756E74696D652E436F6D70696C6572536572766963657300436F6D70696C6174696F6E52656C61786174696F6E734174747269627574650052756E74696D65436F6D7061746962696C6974794174747269627574650048796472615F526573756C740052756E74696D6500436F6E66696775726174696F6E44696374696F6E617279006765745F4E756D65724F70657261746F7261006465736372697074696F6E0000000003200000000000252267F49CC8B641B90894AB9E6202310008B77A5C561934E0890300000203200001084B3653AA0A875D04092006010E0E0E0E0E0E0420010108030612190320000839010010546573746F776150726F66696C746572000841545543484F4C5303312E300A323031392E302E302E300A32392D30342D3230313900000801000800000000001E01000100540216577261704E6F6E457863657074696F6E5468726F777301B82900000000000000000000CE290000002000000000000000000000000000000000000000000000C02900000000000000005F436F72446C6C4D61696E006D73636F7265652E646C6C0000000000FF250020001000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001000000018000080000000000000000000000000000001000100000030000080000000000000000000000000000001000000000048000000584000005C02000000000000000000005C0234000000560053005F00560045005200530049004F004E005F0049004E0046004F0000000000BD04EFFE00000100000000000000000000000000000000003F000000000000000400000002000000000000000000000000000000440000000100560061007200460069006C00650049006E0066006F00000000002400040000005400720061006E0073006C006100740069006F006E00000000000000B004BC010000010053007400720069006E006700460069006C00650049006E0066006F0000009801000001003000300030003000300034006200300000002C0002000100460069006C0065004400650073006300720069007000740069006F006E000000000020000000300008000100460069006C006500560065007200730069006F006E000000000030002E0030002E0030002E003000000044001100010049006E007400650072006E0061006C004E0061006D0065000000480079006400720061005F0052006500730075006C0074002E0064006C006C00000000002800020001004C006500670061006C0043006F0070007900720069006700680074000000200000004C00110001004F0072006900670069006E0061006C00460069006C0065006E0061006D0065000000480079006400720061005F0052006500730075006C0074002E0064006C006C0000000000340008000100500072006F006400750063007400560065007200730069006F006E00000030002E0030002E0030002E003000000038000800010041007300730065006D0062006C0079002000560065007200730069006F006E00000030002E0030002E0030002E00300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000C000000E03900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
SELECT CONVERT(VARCHAR(max),val) As NormalConvert
FROM #MyTable
I must admit, that I didn't really get what you tried to achieve. This might be a case of an xy problem...
But - as far as I can understand - you want to transform this varbinaries to readable text, by getting rid of unreadable characters. One trick might be this:
DECLARE #MyTable TABLE(id int identity(1,1) NOT NULL PRIMARY KEY,val varbinary(max));
INSERT INTO #MyTable(val)
SELECT 0x4D5A9304FFFFB8408E1FBA0EB409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2450454C010355CBC65CE2210B010B0A06DE2920401022040482034085101010101090294B40B8026C280820482E74657874E4092A0220602E72737263B802440C40402E72656C6F630C62104042C0294802051C26740301C0205A050332557E040A6F050A1A2E447E040A6F050A1C2E377E040A6F050A1E2E2A7E040A6F050A1F0B2E1C7E040A6F050A1F162E0E7E040A6F050A1F293302162A172A1E0228060A2A5605FFFE3C3F786D6C2076657273696F6E3D22312E302220656E636F64696E673D227574662D3136223F3E0D0A3C4C69746543616C6C6261636B47656E657261746F7220786D6C6E733A7873693D22687474703A2F2F7777772E77332E6F72672F323030312F584D4C536368656D612D696E7374616E63652220786D6C6E733A7873643D22687474703A2F2F7777772E77332E6F72672F323030312F584D4C536368656D61223E0D0A20203C416374696F6E47726F75703E0D0A202020203C436F6E646974696F6E3E696620280A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D203429207C7C202A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D203629207C7C202A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D203829207C7C202A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D20313129207C7C202A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D20323229207C7C202A2852756E74696D652E436F6E66696775726174696F6E44696374696F6E6172792E4E756D65724F70657261746F7261203D3D20343129202A290A7B0A72657475726E2066616C73653B0A7D0A656C73650A7B0A72657475726E20747275653B0A7D3C2F436F6E646974696F6E3E0D0A202020203C4E616D653E4E6F77612067727570613C2F4E616D653E0D0A20203C2F416374696F6E47726F75703E0D0A3C2F4C69746543616C6C6261636B47656E657261746F723E42534A4201010C76342E302E3330333139056C2401237E91380123537472696E6773C80208235553D2102347554944E29423426C6F62020147140901FA2533160106020206030102010A0106362F0A676106AC8C06CC8C0AF7610AFF6101010101101B0501015020963D0A01B1208618520E0111521B19522521520E29FF2A3117012E09520E2E0B322E136C2E1B750480EA040126E3071258012A013C4D6F64756C653E48796472615F526573756C742E646C6C436F6E646974696F6E736D73636F726C696253797374656D4F626A656374436F6E646974696F6E5F4E6F77615F67727570612E63746F7243646E4879647261487964726143616C6C6261636B417373656D626C794465736372697074696F6E41747472696275746553797374656D2E52756E74696D652E436F6D70696C65725365727669636573436F6D70696C6174696F6E52656C61786174696F6E7341747472696275746552756E74696D65436F6D7061746962696C69747941747472696275746548796472615F526573756C7452756E74696D65436F6E66696775726174696F6E44696374696F6E6172796765745F4E756D65724F70657261746F72616465736372697074696F6E0320252267F49CC8B641B90894AB9E62023108B77A5C561934E08903020321084B3653AA0A875D040926010E0E0E0E0E0E04210108030612190328390110546573746F776150726F66696C7465720841545543484F4C5303312E3A323031392E302E302E3A32392D30342D323031390801081E0101540216577261704E6F6E457863657074696F6E5468726F777301B829CE2920C0295F436F72446C6C4D61696E6D73636F7265652E646C6CFF2520111018810130814858405C025C023456535F56455253494F4E5F494E464FBD04EFFE013F0402440156617246696C65496E666F24045472616E736C6174696F6EB4BC0101537472696E6746696C65496E666F98010130303030303462302C020146696C654465736372697074696F6E20380146696C6556657273696F6E302E302E302E30441101496E7465726E616C4E616D6548796472615F526573756C742E646C6C2802014C6567616C436F70797269676874204C11014F726967696E616C46696C656E616D6548796472615F526573756C742E646C6C34080150726F6475637456657273696F6E302E302E302E30380801417373656D626C792056657273696F6E302E302E302E302CE039
UNION ALL
SELECT 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000504500004C01030055CBC65C0000000000000000E00002210B010B00000A00000006000000000000DE290000002000000040000000000010002000000002000004000000000000000400000000000000008000000002000000000000030040850000100000100000000010000010000000000000100000000000000000000000902900004B00000000400000B802000000000000000000000000000000000000006000000C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000080000000000000000000000082000004800000000000000000000002E74657874000000E409000000200000000A000000020000000000000000000000000000200000602E72737263000000B80200000040000000040000000C0000000000000000000000000000400000402E72656C6F6300000C0000000060000000020000001000000000000000000000000000004000004200000000000000000000000000000000C02900000000000048000000020005001C260000740300000100000000000000C02000005A050000000000000000000000000000000000000000000000000000000000000000000000000000000000000330020055000000000000007E0400000A6F0500000A1A2E447E0400000A6F0500000A1C2E377E0400000A6F0500000A1E2E2A7E0400000A6F0500000A1F0B2E1C7E0400000A6F0500000A1F162E0E7E0400000A6F0500000A1F293302162A172A1E02280600000A2A0000000000000056050000FFFE3C003F0078006D006C002000760065007200730069006F006E003D00220031002E0030002200200065006E0063006F00640069006E0067003D0022007500740066002D003100360022003F003E000D000A003C004C00690074006500430061006C006C006200610063006B00470065006E0065007200610074006F007200200078006D006C006E0073003A007800730069003D00220068007400740070003A002F002F007700770077002E00770033002E006F00720067002F0032003000300031002F0058004D004C0053006300680065006D0061002D0069006E007300740061006E00630065002200200078006D006C006E0073003A007800730064003D00220068007400740070003A002F002F007700770077002E00770033002E006F00720067002F0032003000300031002F0058004D004C0053006300680065006D00610022003E000D000A00200020003C0041006300740069006F006E00470072006F00750070003E000D000A0020002000200020003C0043006F006E0064006900740069006F006E003E0069006600200028000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D0020003400290020007C007C00200020000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D0020003600290020007C007C00200020000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D0020003800290020007C007C00200020000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D00200031003100290020007C007C00200020000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D00200032003200290020007C007C00200020000A002800520075006E00740069006D0065002E0043006F006E00660069006700750072006100740069006F006E00440069006300740069006F006E006100720079002E004E0075006D00650072004F00700065007200610074006F007200610020003D003D002000340031002900200020000A0029000A007B000A00720065007400750072006E002000660061006C00730065003B000A007D000A0065006C00730065000A007B000A00720065007400750072006E00200074007200750065003B000A007D003C002F0043006F006E0064006900740069006F006E003E000D000A0020002000200020003C004E0061006D0065003E004E006F00770061002000670072007500700061003C002F004E0061006D0065003E000D000A00200020003C002F0041006300740069006F006E00470072006F00750070003E000D000A003C002F004C00690074006500430061006C006C006200610063006B00470065006E0065007200610074006F0072003E00000042534A4201000100000000000C00000076342E302E33303331390000000005006C00000024010000237E0000900100003801000023537472696E677300000000C80200000800000023555300D0020000100000002347554944000000E00200009400000023426C6F620000000000000002000001471400000901000000FA25330016000001000000060000000200000002000000060000000300000001000000020000000100000000000A00010000000000060036002F000A00670061000600AC008C000600CC008C000A00F70061000A00FF0061000000000001000000000001000100010010001B00000005000100010050200000000096003D000A000100B12000000000861852000E000100110052001B00190052002500210052000E002900FF002A00310017012E00090052000E002E000B0032002E0013006C002E001B007500048000000000000000000000000000000000EA0000000400000000000000000000000100260000000000E30700000000000000000000120058000000000000000000010000002A0100000000003C4D6F64756C653E0048796472615F526573756C742E646C6C00436F6E646974696F6E73006D73636F726C69620053797374656D004F626A65637400436F6E646974696F6E5F4E6F77615F6772757061002E63746F720043646E48796472610048796472610043616C6C6261636B417373656D626C794465736372697074696F6E4174747269627574650053797374656D2E52756E74696D652E436F6D70696C6572536572766963657300436F6D70696C6174696F6E52656C61786174696F6E734174747269627574650052756E74696D65436F6D7061746962696C6974794174747269627574650048796472615F526573756C740052756E74696D6500436F6E66696775726174696F6E44696374696F6E617279006765745F4E756D65724F70657261746F7261006465736372697074696F6E0000000003200000000000252267F49CC8B641B90894AB9E6202310008B77A5C561934E0890300000203200001084B3653AA0A875D04092006010E0E0E0E0E0E0420010108030612190320000839010010546573746F776150726F66696C746572000841545543484F4C5303312E300A323031392E302E302E300A32392D30342D3230313900000801000800000000001E01000100540216577261704E6F6E457863657074696F6E5468726F777301B82900000000000000000000CE290000002000000000000000000000000000000000000000000000C02900000000000000005F436F72446C6C4D61696E006D73636F7265652E646C6C0000000000FF250020001000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001000000018000080000000000000000000000000000001000100000030000080000000000000000000000000000001000000000048000000584000005C02000000000000000000005C0234000000560053005F00560045005200530049004F004E005F0049004E0046004F0000000000BD04EFFE00000100000000000000000000000000000000003F000000000000000400000002000000000000000000000000000000440000000100560061007200460069006C00650049006E0066006F00000000002400040000005400720061006E0073006C006100740069006F006E00000000000000B004BC010000010053007400720069006E006700460069006C00650049006E0066006F0000009801000001003000300030003000300034006200300000002C0002000100460069006C0065004400650073006300720069007000740069006F006E000000000020000000300008000100460069006C006500560065007200730069006F006E000000000030002E0030002E0030002E003000000044001100010049006E007400650072006E0061006C004E0061006D0065000000480079006400720061005F0052006500730075006C0074002E0064006C006C00000000002800020001004C006500670061006C0043006F0070007900720069006700680074000000200000004C00110001004F0072006900670069006E0061006C00460069006C0065006E0061006D0065000000480079006400720061005F0052006500730075006C0074002E0064006C006C0000000000340008000100500072006F006400750063007400560065007200730069006F006E00000030002E0030002E0030002E003000000038000800010041007300730065006D0062006C0079002000560065007200730069006F006E00000030002E0030002E0030002E00300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000C000000E03900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
;WITH SingleCharacters AS
(
SELECT t.id
,Nmbr
,OneChar
FROM #MyTable t
CROSS APPLY(SELECT TOP(DATALENGTH(t.val)/2) ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM master..spt_values v1 CROSS JOIN master..spt_values v2) A(Nmbr)
CROSS APPLY(SELECT CAST(SUBSTRING(t.val,(-1+Nmbr*2),2) AS VARCHAR(1))) B(OneChar)
)
SELECT sc.id
,(
SELECT sc2.OneChar AS [*]
FROM SingleCharacters sc2
WHERE sc2.id=sc.id
AND ASCII(sc2.OneChar) BETWEEN 32 AND 126
ORDER BY sc2.Nmbr
FOR XML PATH(''),TYPE).value('.','varchar(max)')
FROM SingleCharacters sc
GROUP BY sc.id;
The idea in short:
The characters are taken from the VARBINARY with SUBSTRING at any odd position we pick two bytes. This is casted to VARCHAR(1).
The final SELECT will use a GROUP BY together with a correlated sub-query. Starting with v2017 you should use STRING_AGG() for the same. This will re-concatenate alle characters with ASCII-values between 32 and 126, hence most of the plain latin characters.
For the given binaries the result is:
ML!hspormcno erni O oePL\! #)#( .et`rrD#.eoB)t 2~Do.~*ooo)***?m eso=10 noig"t-6?<iealakeeao mn:s=ht:/w.3og20/MShm-ntne mn:s=ht:/w.3og20/MShm" <cinru> Cniini RnieCniuainitoayNmrprtr =4 |*RnieCniuainitoayNmrprtr =6 |*RnieCniuainitoayNmrprtr =8 |*RnieCniuainitoayNmrprtr =1)| (utm.ofgrtoDcinr.ueOeaoa= 2 |*RnieCniuainitoayNmrprtr =4)*eunflele{rtr re<Cniin <aeNw rp<Nm> /cinru>/iealakeeao>SBv..01$#Srns#SGI#lb%/ga = R!1.lMdl>yr_eutdlodtossolbytmbetodto_oaguacoCnyrHdaalaksebyecitoAtiueytmRnieCmieSrieCmiaineaainAtiueutmCmaiiiytrbtHdaRslRnieofgrtoDcinrgtNmrprtrdsrpin "Ab1\6&9etwPoitrAUHL.21...2-421TrpoEcpinhos )CrlMimcredl%X\\4SVRINIFDVrienornltoSrnFlIf0040FlDsrpin8FlVrin...Dnenlaeyr_eutdlLgloyih OiiaFlnmHdaRsl.l4rdcVrin...8sebyVrin...,
M#!Ti rga antb u nDSmd.$PLU#KH.et .sc#.eo#HtZU~Do.~*ooo)***V<?xml version="1.0" encoding="utf-16"?><LiteCallbackGenerator xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ActionGroup> <Condition>if ((Runtime.ConfigurationDictionary.NumerOperatora == 4) || (Runtime.ConfigurationDictionary.NumerOperatora == 6) || (Runtime.ConfigurationDictionary.NumerOperatora == 8) || (Runtime.ConfigurationDictionary.NumerOperatora == 11) || (Runtime.ConfigurationDictionary.NumerOperatora == 22) || (Runtime.ConfigurationDictionary.NumerOperatora == 41) ){return false;}else{return true;}</Condition> <Name>Nowa grupa</Name> </ActionGroup></LiteCallbackGenerator>BJv..01l$#8#tig#S#UD#lbG%6/gaaaP=RRR%!R)*1.R.2.l.u&X*Mdl>HdaRsl.lodtosmcriytmOjcodto_oagua.trCnyryralaksebyecitoAtiueSse.utm.oplrevcsCmiaineaainAtiueRnieoptbltAtiueHdaRslutmofgrtoDcinre_ueOeaoadsrpin %g\6 9TsoarfleAUHL.090002-421TrpoEcpinhos_oDlanmcredl0HX\\4VS_VERSION_INFO?DVarFileInfo$TranslationStringFileInfo000004b0,FileDescription 0FileVersion0.0.0.0DInternalNameHydra_Result.dll(LegalCopyright LOriginalFilenameHydra_Result.dll4ProductVersion0.0.0.08Assembly Version0.0.0.0
This looks not so bad in my eyes.
Hint: You can use some kind of CASE list or a mapping table to replace certain ASCII values with the appropriate character.
How to get the SUM of values of one column in table. Now I want get the SUM of 8 and 0 if I use query write below.
SELECT SUM(Items) FROM dbo.CustomSplit(#AssigneeProgress,',')
this gives an error
Operand data type varchar is invalid for sum operator.
My code is:
DECLARE #AssigneeProgress AS VARCHAR(100)
SELECT
#AssigneeProgress = STUFF((SELECT ',' + CAST(ISNULL((((TblAssignments.PlannedDuration * 100) / 300) * TblAssignments.Progress) / 100,0) AS VARCHAR(100))
FROM TblTasks,TblAssignments
WHERE TblTasks.TaskId = TblAssignments.AssignmentEntityId
AND TblAssignments.AssignmentEntity = 'Task'
AND AssignmentStatus NOT IN ('Deleted','Cancelled')
AND TblTasks.TaskId = 63 FOR XML PATH('')), 1, 1,'')
SELECT * FROM dbo.CustomSplit(#AssigneeProgress,',')
This query gives me the result in table like
Items
8
0
Does your method CustomSplit return a table having the column Items of type varchar?
If yes, you can convert the column:
SELECT SUM(cast(Items as int)) FROM dbo.CustomSplit(#AssigneeProgress,',')
Try to cast the Items that you get back from your split function to an INT type before summing:
SELECT
SUM(CAST(Items AS INT))
FROM
dbo.CustomSplit(#AssigneeProgress,',')
The CustomSplit function most likely returns varchar(x) items....