Replace 00 with varbinary type - sql

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.

Related

SQL : extract next character from string where multiple separators exist

Azure MSSQL Database
I have a column that contains values stored per transaction. The string can contain up to 7 values, separated by a '-'.
I need to be able to extract the value that is stored after the 3rd '-'. The issue is that the length of this column (and the characters that come before the 3rd '-') can vary.
For example:
DIM VALUE
1. NHL--WA-S-MOSG-SER-
2. VDS----HAST-SER-
3. ---D---SER
Row 1 needs to return 'S'
Row 2 needs to return '-'
Row 3 needs to return 'D'
This is by no means an optimal solution, but it works in SQL Server. 😊
TempTable added for testing purposes. Maybe it gives you a hint as of where to start.
Edit: added reference for string_split function (works from SQL Server 2016 up).
CREATE TABLE #tempStrings (
VAL VARCHAR(30)
);
INSERT INTO #tempStrings VALUES ('NHL--WA-S-MOSG-SER-');
INSERT INTO #tempStrings VALUES ('VDS----HAST-SER-');
INSERT INTO #tempStrings VALUES ('---D---SER');
INSERT INTO #tempStrings VALUES ('A-V-D-C--SER');
SELECT
t.VAL,
CASE t.PART WHEN '' THEN '-' ELSE t.PART END AS PART
FROM
(SELECT
t.VAL,
ROW_NUMBER() OVER (PARTITION BY VAL ORDER BY (SELECT NULL)) AS IX,
value AS PART
FROM #tempStrings t
CROSS APPLY string_split(VAL, '-')) t
WHERE t.IX = 4; --DASH COUNT + 1
DROP TABLE #tempStrings;
Output is...
VAL PART
---D---SER D
A-V-D-C--SER C
NHL--WA-S-MOSG-SER- S
VDS----HAST-SER- -
If you always want the fourth element then using CHARINDEX is relatively straightforward:
DROP TABLE IF EXISTS #tmp;
CREATE TABLE #tmp (
rowId INT IDENTITY PRIMARY KEY,
xval VARCHAR(30) NOT NULL
);
INSERT INTO #tmp
VALUES
( 'NHL--WA-S-MOSG-SER-' ),
( 'VDS----HAST-SER-' ),
( '---D---SER' ),
( 'A-V-D-C--SER' );
;WITH cte AS
( -- Work out the position of the 3rd dash
SELECT
rowId,
xval,
CHARINDEX( '-', xval, CHARINDEX( '-', xval, CHARINDEX( '-', xval ) + 1 ) + 1 ) + 1 xstart
FROM #tmp t
), cte2 AS
( -- Work out the length for the substring function
SELECT rowId, xval, xstart, CHARINDEX( '-', xval, xstart) - (xstart) AS xlen
FROM cte
)
SELECT rowId, ISNULL( NULLIF( SUBSTRING( xval, xstart, xlen ), '' ), '-' ) xpart
FROM cte2
I also did a volume test at 1 million rows and this was by far the fastest method compared with STRING_SPLIT, OPENJSON, recursive CTE (the worst at high volume). As a downside this method is less extensible, say you want the second or fifth items for example.

How to SELECT string between second and third instance of ",,"?

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

Order Concatenated field

I have a field which is a concatenation of single letters. I am trying to order these strings within a view. These values can't be hard coded as there are too many. Is someone able to provide some guidance on the function to use to achieve the desired output below? I am using MSSQL.
Current output
CustID | Code
123 | BCA
Desired output
CustID | Code
123 | ABC
I have tried using a UDF
CREATE FUNCTION [dbo].[Alphaorder] (#str VARCHAR(50))
returns VARCHAR(50)
BEGIN
DECLARE #len INT,
#cnt INT =1,
#str1 VARCHAR(50)='',
#output VARCHAR(50)=''
SELECT #len = Len(#str)
WHILE #cnt <= #len
BEGIN
SELECT #str1 += Substring(#str, #cnt, 1) + ','
SET #cnt+=1
END
SELECT #str1 = LEFT(#str1, Len(#str1) - 1)
SELECT #output += Sp_data
FROM (SELECT Split.a.value('.', 'VARCHAR(100)') Sp_data
FROM (SELECT Cast ('<M>' + Replace(#str1, ',', '</M><M>') + '</M>' AS XML) AS Data) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)) A
ORDER BY Sp_data
RETURN #output
END
This works when calling one field
ie.
Select CustID, dbo.alphaorder(Code)
from dbo.source
where custid = 123
however when i try to apply this to top(10) i receive the error
"Invalid length parameter passed to the LEFT or SUBSTRING function."
Keeping in mind my source has ~4million records, is this still the best solution?
Unfortunately i am not able to normalize the data into a separate table with records for each Code.
This doesn't rely on a id column to join with itself, performance is almost as fast
as the answer by #Shnugo:
SELECT
CustID,
(
SELECT
chr
FROM
(SELECT TOP(LEN(Code))
SUBSTRING(Code,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)),1)
FROM sys.messages) A(Chr)
ORDER by chr
FOR XML PATH(''), type).value('.', 'varchar(max)'
) As CODE
FROM
source t
First of all: Avoid loops...
You can try this:
DECLARE #tbl TABLE(ID INT IDENTITY, YourString VARCHAR(100));
INSERT INTO #tbl VALUES ('ABC')
,('JSKEzXO')
,('QKEvYUJMKRC');
--the cte will create a list of all your strings separated in single characters.
--You can check the output with a simple SELECT * FROM SeparatedCharacters instead of the actual SELECT
WITH SeparatedCharacters AS
(
SELECT *
FROM #tbl
CROSS APPLY
(SELECT TOP(LEN(YourString)) ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM master..spt_values) A(Nmbr)
CROSS APPLY
(SELECT SUBSTRING(YourString,Nmbr,1))B(Chr)
)
SELECT ID,YourString
,(
SELECT Chr As [*]
FROM SeparatedCharacters sc1
WHERE sc1.ID=t.ID
ORDER BY sc1.Chr
FOR XML PATH(''),TYPE
).value('.','nvarchar(max)') AS Sorted
FROM #tbl t;
The result
ID YourString Sorted
1 ABC ABC
2 JSKEzXO EJKOSXz
3 QKEvYUJMKRC CEJKKMQRUvY
The idea in short
The trick is the first CROSS APPLY. This will create a tally on-the-fly. You will get a resultset with numbers from 1 to n where n is the length of the current string.
The second apply uses this number to get each character one-by-one using SUBSTRING().
The outer SELECT calls from the orginal table, which means one-row-per-ID and use a correalted sub-query to fetch all related characters. They will be sorted and re-concatenated using FOR XML. You might add DISTINCT in order to avoid repeating characters.
That's it :-)
Hint: SQL-Server 2017+
With version v2017 there's the new function STRING_AGG(). This would make the re-concatenation very easy:
WITH SeparatedCharacters AS
(
SELECT *
FROM #tbl
CROSS APPLY
(SELECT TOP(LEN(YourString)) ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM master..spt_values) A(Nmbr)
CROSS APPLY
(SELECT SUBSTRING(YourString,Nmbr,1))B(Chr)
)
SELECT ID,YourString
,STRING_AGG(sc.Chr,'') WITHIN GROUP(ORDER BY sc.Chr) AS Sorted
FROM SeparatedCharacters sc
GROUP BY ID,YourString;
Considering your table having good amount of rows (~4 Million), I would suggest you to create a persisted calculated field in the table, to store these values. As calculating these values at run time in a view, will lead to performance problems.
If you are not able to normalize, add this as a denormalized column to the existing table.
I think the error you are getting could be due to empty codes.
If LEN(#str) = 0
BEGIN
SET #output = ''
END
ELSE
BEGIN
... EXISTING CODE BLOCK ...
END
I can suggest to split string into its characters using referred SQL function.
Then you can concatenate string back, this time ordered alphabetically.
Are you using SQL Server 2017? Because with SQL Server 2017, you can use SQL String_Agg string aggregation function to concatenate characters splitted in an ordered way as follows
select
t.CustId, string_agg(strval, '') within GROUP (order by strval)
from CharacterTable t
cross apply dbo.SPLIT(t.code) s
where strval is not null
group by CustId
order by CustId
If you are not working on SQL2017, then you can follow below structure using SQL XML PATH for concatenation in SQL
select
CustId,
STUFF(
(
SELECT
'' + strval
from CharacterTable ct
cross apply dbo.SPLIT(t.code) s
where strval is not null
and t.CustId = ct.CustId
order by strval
FOR XML PATH('')
), 1, 0, ''
) As concatenated_string
from CharacterTable t
order by CustId

Remove Characters in a String in SQL

I have a column u_manualdoc which contains the values are like this CGY DR# 7405. I want to remove the CGY DR#.
Here's the code:
select u_manualdoc, cardcode, cardname from ODLN
I want only the 7405 number. Thanks!
Try this:
--sample data you provided in comments
declare #tbl table(codes varchar(20))
insert into #tbl values
('CGY PST - 58277') , ('CGY RMC PST # 58083'), ('CGY DR # 7443'), ('CSI # 1304'), ('PO# 0568 , 0570'), ('CGY DR# 7446')
--actual query that you can apply to your table
select SUBSTRING(codes, PATINDEX('%[0-9]%', codes), len(codes)) from #tbl
The key point here is to use patindex, which searches for a pattern and returns index where such pattern occur. I specified %[0-9]% which means that we search for any digit - it will return first occurrence of a digit. Now- since this would be our starting point to substring, we pass it to such function. Third parameter of substring is length. Since we want the rest of a string, len function makes sure that we get that :)
Applying to your naming:
select SUBSTRING(u_manualdoc, PATINDEX('%[0-9]%', u_manualdoc), len(u_manualdoc)),
cardcode,
cardname
from ODLN
You should use string functions charindex,len and substring to get it.
See the code below.
select SUBSTRING(u_manualdoc,CHARINDEX('#',u_manualdoc)+1,LEN(u_manualdoc)- CHARINDEX('#',u_manualdoc))
EDIT
In addition to the other answers, you can use this simple method:
select
substring(
u_manualdoc,
len(u_manualdoc) - patindex('%[^0-9]%', reverse(u_manualdoc)) + 2,
len(u_manualdoc)
),
cardcode, cardname
from ODLN
In this example, patindex finds the first non-digit (as specified by ^[0-9]) from the right side of the string, and then uses that as the starting point of the substring.
This will work on all of your sample strings (including 'PO# 0568 , 0570 CGY DR# 7446').
Or use SQL Server Regex, which lets you use more powerful regular expressions within your queries.
TRY THIS
DECLARE #table TABLE(DirtyCol VARCHAR(100));
INSERT INTO #table
VALUES('AB ABCDE # 123'), ('ABCDE# 123'), ('AB: ABC# 123 AB: ABC# 123'), ('AB#'), ('AB # 1 000 000'), ('AB # 1`234`567'), ('AB # (9)(876)(543)');
WITH tally
AS (
SELECT TOP (100) N = ROW_NUMBER() OVER(ORDER BY ##spid)
FROM sys.all_columns),
data
AS (
SELECT DirtyCol,
Col
FROM #table
CROSS APPLY
(
SELECT
(
SELECT C+''
FROM
(
SELECT N,
SUBSTRING(DirtyCol, N, 1) C
FROM tally
WHERE N <= DATALENGTH(DirtyCol)
) [1]
WHERE C BETWEEN '0' AND '9'
ORDER BY N FOR XML PATH('')
)
) p(Col)
WHERE p.Col IS NOT NULL)
SELECT DirtyCol,
CAST(Col AS INT) IntCol
FROM data;

Strip non-numeric characters from a string

I'm currently doing a data conversion project and need to strip all alphabetical characters from a string. Unfortunately I can't create or use a function as we don't own the source machine making the methods I've found from searching for previous posts unusable.
What would be the best way to do this in a select statement? Speed isn't too much of an issue as this will only be running over 30,000 records or so and is a once off statement.
You can do this in a single statement. You're not really creating a statement with 200+ REPLACEs are you?!
update tbl
set S = U.clean
from tbl
cross apply
(
select Substring(tbl.S,v.number,1)
-- this table will cater for strings up to length 2047
from master..spt_values v
where v.type='P' and v.number between 1 and len(tbl.S)
and Substring(tbl.S,v.number,1) like '[0-9]'
order by v.number
for xml path ('')
) U(clean)
Working SQL Fiddle showing this query with sample data
Replicated below for posterity:
create table tbl (ID int identity, S varchar(500))
insert tbl select 'asdlfj;390312hr9fasd9uhf012 3or h239ur ' + char(13) + 'asdfasf'
insert tbl select '123'
insert tbl select ''
insert tbl select null
insert tbl select '123 a 124'
Results
ID S
1 390312990123239
2 123
3 (null)
4 (null)
5 123124
CTE comes for HELP here.
;WITH CTE AS
(
SELECT
[ProductNumber] AS OrigProductNumber
,CAST([ProductNumber] AS VARCHAR(100)) AS [ProductNumber]
FROM [AdventureWorks].[Production].[Product]
UNION ALL
SELECT OrigProductNumber
,CAST(STUFF([ProductNumber], PATINDEX('%[^0-9]%', [ProductNumber]), 1, '') AS VARCHAR(100) ) AS [ProductNumber]
FROM CTE WHERE PATINDEX('%[^0-9]%', [ProductNumber]) > 0
)
SELECT * FROM CTE
WHERE PATINDEX('%[^0-9]%', [ProductNumber]) = 0
OPTION (MAXRECURSION 0)
output:
OrigProductNumber ProductNumber
WB-H098 098
VE-C304-S 304
VE-C304-M 304
VE-C304-L 304
TT-T092 092
RichardTheKiwi's script in a function for use in selects without cross apply,
also added dot because in my case I use it for double and money values within a varchar field
CREATE FUNCTION dbo.ReplaceNonNumericChars (#string VARCHAR(5000))
RETURNS VARCHAR(1000)
AS
BEGIN
SET #string = REPLACE(#string, ',', '.')
SET #string = (SELECT SUBSTRING(#string, v.number, 1)
FROM master..spt_values v
WHERE v.type = 'P'
AND v.number BETWEEN 1 AND LEN(#string)
AND (SUBSTRING(#string, v.number, 1) LIKE '[0-9]'
OR SUBSTRING(#string, v.number, 1) LIKE '[.]')
ORDER BY v.number
FOR
XML PATH('')
)
RETURN #string
END
GO
Thanks RichardTheKiwi +1
Well if you really can't use a function, I suppose you could do something like this:
SELECT REPLACE(REPLACE(REPLACE(LOWER(col),'a',''),'b',''),'c','')
FROM dbo.table...
Obviously it would be a lot uglier than that, since I only handled the first three letters, but it should give the idea.