How to convert Base64 to a comma separated binary - sql
I'm modifying the explaination so its clearer.
I had this data in an image column within sql server.
This is the original data
0x00B0A04500B898450070954500A89345006893450028944500109545005095450088944500D8924500D0904500288F4500608E4500A08E4500988F4500D0904500B091450000924500C09145001891450040904500788F4500D88E4500608E4500108E4500F88D4500F88D4500008E4500F88D4500E88D4500108E4500C08E45004090450038924500B0934500E8924500C08D4500A882450000624500A0334500C0FD44008093440080CC43000069C300403AC400A090C40060BCC40080E3C4002003C500E010C500D019C500901DC500B01DC500A01CC500901CC500401EC500F01FC500601EC500A015C5004003C50060CEC4006087C40000E7C30000134300C02E44000092440040C24400A0E94400C004450090114500901B45004023450040294500602E450030334500F0374500203C4500603F4500404145001042450060424500104345008044450050464500B0474500E0474500A0464500C044450020444500F0464500F04E4500E05C45000070450018834500388E4500E8974500E89E450098A24500F8A24500B0A0459AD90443AE870543B85E0543E1BA044348A10343333302437B94004348E1FD4214AEFA420080F7429A19F442713DF0428FC2EB427B94E64229DCE042CDCCDA427B94D442713DCE42AEC7C7429A19C142B81EBA42CDCCB242B81EAB427B14A34214AE9A4285EB91420AD7884285EB7E428FC26B42713D5842713D44425C8F2F4200001A428FC20342C3F5DA41C3F5B041D7A38C41AE47614100003C411F852741A4701D416666164114AE0B41CDCCF440295CC74052B8964014AE574014AE1740EC51D83F3D0A973FEC51383F295C8F3E00000000AE47E13D3D0A573F52B80E400AD78340B81EC540000000417B141641D7A324413D0A2F415C8F3A4100004C4148E1664100008641AE479D416666B84152B8D6410000F84100000E42F6282142713D3542CDCC49429A995E421F857342CD4C84421F058F4285EB994266E6A4428FC2AF42AE47BA420A57C44285EBCD42D723D742B81EE0420AD7E842F628F14214AEF842E1FAFE4248E10143146E0343C3350443F66804431F4504433D0A044314EE03433D0A0443486104439AD90443
I used then used this query to get these results
Select CONVERT(VARCHAR(MAX),(CONVERT(varbinary(MAX), 'the image column')) ,2) from [thetable]
00B0A04500B898450070954500A89345006893450028944500109545005095450088944500D8924500D0904500288F4500608E4500A08E4500988F4500D0904500B091450000924500C09145001891450040904500788F4500D88E4500608E4500108E4500F88D4500F88D4500008E4500F88D4500E88D4500108E4500C08E45004090450038924500B0934500E8924500C08D4500A882450000624500A0334500C0FD44008093440080CC43000069C300403AC400A090C40060BCC40080E3C4002003C500E010C500D019C500901DC500B01DC500A01CC500901CC500401EC500F01FC500601EC500A015C5004003C50060CEC4006087C40000E7C30000134300C02E44000092440040C24400A0E94400C004450090114500901B45004023450040294500602E450030334500F0374500203C4500603F4500404145001042450060424500104345008044450050464500B0474500E0474500A0464500C044450020444500F0464500F04E4500E05C45000070450018834500388E4500E8974500E89E450098A24500F8A24500B0A0459AD90443AE870543B85E0543E1BA044348A10343333302437B94004348E1FD4214AEFA420080F7429A19F442713DF0428FC2EB427B94E64229DCE042CDCCDA427B94D442713DCE42AEC7C7429A19C142B81EBA42CDCCB242B81EAB427B14A34214AE9A4285EB91420AD7884285EB7E428FC26B42713D5842713D44425C8F2F4200001A428FC20342C3F5DA41C3F5B041D7A38C41AE47614100003C411F852741A4701D416666164114AE0B41CDCCF440295CC74052B8964014AE574014AE1740EC51D83F3D0A973FEC51383F295C8F3E00000000AE47E13D3D0A573F52B80E400AD78340B81EC540000000417B141641D7A324413D0A2F415C8F3A4100004C4148E1664100008641AE479D416666B84152B8D6410000F84100000E42F6282142713D3542CDCC49429A995E421F857342CD4C84421F058F4285EB994266E6A4428FC2AF42AE47BA420A57C44285EBCD42D723D742B81EE0420AD7E842F628F14214AEF842E1FAFE4248E10143146E0343C3350443F66804431F4504433D0A044314EE03433D0A0443486104439AD90443
And using online converters i can get it to a binary/commma separated list.
11010011,01000000,01110100,00000011,01001110,00111001,11010011,01000000,01111100,11110111,11001110,00111001,11010011,01001110,11110100,11110111,10011110,00111001,11010011,01000000,00111100,11110111,01111110,00111001,11010011,01001110,10111100,11110111,01111110,00111001,11010011,01001101,10111100,11110111,10001110,00111001,11010011,01001101,01110100,11110111,10011110,00111001,11010011,01001110,01110100,11110111,10011110,00111001,11010011,01001111,00111100,11110111,10001110,00111001,11010011,01000000,11111100,11110111,01101110,00111001,11010011,01000000,11110100,11110111,01001110,00111001,11010011,01001101,10111100,11110000,01011110,00111001,11010011,01001110,10110100,11110000,01001110,00111001,11010011,01000000,00110100,11110000,01001110,00111001,11010011,01001111,01111100,11110000,01011110,00111001,11010011,01000000,11110100,11110111,01001110,00111001,11010011,01000000,01110100,11110111,01011110,00111001,11010011,01001101,00110100,11110111,01101110,00111001,11010011,01000000,10110100,11110111,01011110,00111001,11010011,01001101,01111100,11110111,01011110,00111001,11010011,01001110,00110100,11110111,01001110,00111001,11010011,01001110,11111100,11110000,01011110,00111001,11010011,01000000,11111100,11110000,01001110,00111001,11010011,01001110,10110100,11110000,01001110,00111001,11010011,01001101,01110100,11110000,01001110,00111001,11010011,01000001,01111100,11110000,00111110,00111001,11010011,01000001,01111100,11110000,00111110,00111001,11010011,01001101,00110100,11110000,01001110,00111001,11010011,01000001,01111100,11110000,00111110,00111001,11010011,01000001,00111100,11110000,00111110,00111001,11010011,01001101,01110100,11110000,01001110,00111001,11010011,01000000,10110100,11110000,01001110,00111001,11010011,01001110,00110100,11110111,01001110,00111001,11010011,01001101,11111100,11110111,01101110,00111001,11010011,01000000,01110100,11110111,01111110,00111001,11010011,01000001,00111100,11110111,01101110,00111001,11010011,01000000,10110100,11110000,00111110,00111001,11010011,01000000,00111100,11110011,01101110,00111001,11010011,01001101,00110100,11101011,01101110,00111001,11010011,01000000,00110100,11011111,01111110,00111001,11010011,01000000,10110100,00010100,00111110,00111000,11010011,01001111,00110100,11110111,01111110,00111000,11010011,01001111,00110100,00001000,00101110,00110111,11010011,01001101,00110100,11101011,11010000,10110111,11010011,01001110,00110100,11011100,00000000,10111000,11010011,01000000,00110100,11110111,01000000,10111000,11010011,01001110,10110100,00000100,00100000,10111000,11010011,01001111,00110100,00010011,01110000,10111000,11010011,01001101,10110100,11010011,01110000,10111001,11010011,01000001,00110100,11010111,01000000,10111001,11010011,01000000,11110100,11010111,11010000,10111001,11010011,01001111,01110100,11010100,00110000,10111001,11010011,01000000,01110100,11010100,00110000,10111001,11010011,01000000,00110100,11010100,00100000,10111001,11010011,01001111,01110100,11010100,00100000,10111001,11010011,01001110,00110100,11010100,01000000,10111001,11010011,01000001,01110100,11010100,01010000,10111001,11010011,01001110,10110100,11010100,01000000,10111001,11010011,01000000,00110100,11010111,10010000,10111001,11010011,01001110,00110100,11010011,01110000,10111001,11010011,01001110,10110100,00001000,01000000,10111000,11010011,01001110,10110100,11110011,10110000,10111000,11010011,01001101,00110100,00010011,10110000,10110111,11010011,01001101,00110100,11010111,01111110,00110111,11010011,01000000,10110100,11011000,01001110,00111000,11010011,01001101,00110100,11110111,01101110,00111000,11010011,01001110,00110100,00001011,01101110,00111000,11010011,01000000,00110100,00010011,11011110,00111000,11010011,01000000,10110100,11010011,10001110,00111001,11010011,01001111,01110100,11010111,01011110,00111001,11010011,01001111,01110100,11010100,00011110,00111001,11010011,01001110,00110100,11011011,01111110,00111001,11010011,01001110,00110100,11011011,11011110,00111001,11010011,01001110,10110100,11011000,01001110,00111001,11010011,01001101,11110100,11011111,01111110,00111001,11010011,01000001,01110100,11011111,10111110,00111001,11010011,01001101,10110100,11011100,00101110,00111001,11010011,01001110,10110100,11011100,01011110,00111001,11010011,01001110,00110100,11100011,01011110,00111001,11010011,01001101,01110100,11100011,01101110,00111001,11010011,01001110,10110100,11100011,01101110,00111001,11010011,01001101,01110100,11100011,01111110,00111001,11010011,01001111,00110100,11100011,10001110,00111001,11010011,01001110,01110100,11100011,10101110,00111001,11010011,01000000,01110100,11100011,10111110,00111001,11010011,01000001,00110100,11100011,10111110,00111001,11010011,01000000,00110100,11100011,10101110,00111001,11010011,01000000,10110100,11100011,10001110,00111001,11010011,01001101,10110100,11100011,10001110,00111001,11010011,01000001,01110100,11100011,10101110,00111001,11010011,01000001,01110100,11100000,01001110,00111001,11010011,01000001,00110100,11100100,00101110,00111001,11010011,01001101,00110100,11101111,01001110,00111001,11010011,01001101,01111100,11110011,01111110,00111001,11010011,01001101,11111100,11110000,01001110,00111001,11010011,01000001,00111100,11110111,10111110,00111001,11010011,01000001,00111100,11110100,01001110,00111001,11010011,01001111,01111100,00000011,01101110,00111001,11010011,01000001,01111100,00000011,01101110,00111001,11010011,01000000,01110100,00000011,01001110,00111001,11110100,00000000,11111101,11010011,10001110,00110111,00000000,01001111,00111011,11010011,10011110,00110111,00000111,11001110,01000100,11010011,10011110,00110111,00010011,01010000,01000000,11010011,10001110,00110111,11100011,11000000,00110101,11010011,01111110,00110111,11011111,01111101,11110111,11010011,01101110,00110111,11101100,00011111,01111000,11010011,01001110,00110111,11100011,11000001,00110101,00010100,00111110,00110110,11010111,10000000,00000100,00010100,00001110,00110110,11010011,01001111,00110100,00010111,10111110,00110110,11110100,00001101,01111101,00010111,10001110,00110110,11101111,01011101,11000011,00010111,01001110,00110110,11110000,01010000,10110110,00010000,00011110,00110110,11101100,00011111,01111000,00010011,10101110,00110110,11011011,11010000,11000010,00010011,01001110,00110110,00001000,00110000,10000010,00001100,00001110,00110110,11101100,00011111,01111000,00001111,10001110,00110110,11101111,01011101,11000011,00001000,01001110,00110110,00000000,01000000,10111011,00001011,10111110,00110110,11110100,00001101,01111101,00001011,01011110,00110110,00000111,11001101,01000100,00000100,00001110,00110110,00001000,00110000,10000010,00000111,01101110,00110110,00000111,11001101,01000100,00000000,00011110,00110110,11101100,00011101,01111000,00000011,01111110,00110110,11010111,10000000,00000100,11110100,00001110,00110110,11110011,10010001,00000001,11110111,01011110,00110110,11010000,00000000,11111011,11110011,11001110,00110110,11110011,10010001,00000001,11101100,01001110,00110110,11110000,01010000,10110110,11101000,00011110,00110110,11101111,01011101,11000011,11100111,11001110,00110110,11101111,01011101,11000011,11100011,10001110,00110110,11100100,00101111,00000101,11011000,01011110,00110110,11010011,01001101,00110100,11010100,00001110,00110110,11110000,01010000,10110110,11010011,01111110,00110110,00001011,01110001,01111001,00001100,00001110,00110101,00001011,01110001,01111001,00000111,01001110,00110101,00001111,10110000,00110111,11110000,00101110,00110101,00000000,01001110,00111011,11101011,01011110,00110101,11010011,01001101,00110100,11011100,00101110,00110101,11010100,01011111,00111001,11011011,10111110,00110101,00000011,10001110,11110100,11010100,00111110,00110101,11101011,10101110,10111010,11010111,10101110,00110101,11010111,10000000,00000100,11010000,00011110,00110101,00001000,00110000,10000010,00010111,10001110,00110100,11011011,11011110,01000010,00001011,10111110,00110100,11100111,01100000,01111100,11110111,10101110,00110100,11010111,10000000,00000100,11100111,10111110,00110100,11010111,10000000,00000100,11010111,10111110,00110100,00010000,00101110,01110101,00001111,11001101,11000101,11011100,00111101,00000000,11110111,10111101,11000101,00010000,00101110,01110101,11011111,11001101,11000101,11011011,11011110,01000010,11110000,01011101,11000100,11010011,01001101,00110100,11010011,01001101,00110100,00000000,01001110,00111011,00010011,01011101,11000011,11011100,00111101,00000000,11100111,10111101,11000101,11100111,01100000,01111100,11010000,01001110,00110100,11010000,00000000,11111011,11110011,01111110,00110100,00000111,11001101,01000100,00001011,10011110,00110100,11010011,01001101,00110100,11010011,01001110,00110101,11101100,00011101,01111000,11010111,10101110,00110101,00001111,10110000,00110111,11011011,10001110,00110101,11011100,00111101,00000000,11011000,01011110,00110101,11100100,00101111,00000101,11011100,00001110,00110101,11010011,01001101,00110100,11100000,00101110,00110101,11100011,11000001,00110101,11101011,10101110,00110101,11010011,01001101,00110100,11110011,10101110,00110101,00000000,01001110,00111011,11110100,00111110,00110101,11101011,10101110,10111010,00000111,11001110,00110101,11100111,01100000,01111100,00001111,10101110,00110101,11010011,01001101,00110100,00010111,11001110,00110101,11010011,01001101,00110100,11010000,01001110,00110110,00010111,10101101,10111100,11011011,01011110,00110110,11101111,01011101,11000011,11011111,10011110,00110110,00001000,00110000,10000010,11100011,11011110,00110110,11110100,00001111,01111101,11100100,01001110,00110110,11010100,01011111,00111001,11101111,01111110,00110110,00001000,00111110,00000010,11110011,10001110,00110110,11010100,01011101,00111001,11110000,01011110,00110110,11110011,10010001,00000001,11110111,11011110,00110110,11101011,10100001,00111010,00000011,10001110,00110110,11110000,01010000,10110110,00000000,01011110,00110110,00000000,01001110,00111011,00000100,00001110,00110110,11010000,00001110,01111011,00001011,10001110,00110110,11110011,10010001,00000001,00001000,00111110,00110110,00001111,10111101,10110111,00001111,10111110,00110110,00000111,11001101,01000100,00010011,01001110,00110110,11010000,00000000,11111011,00010011,11001110,00110110,00010111,10101101,10111100,00010111,01011110,00110110,11010111,10000000,00000100,00010111,11001110,00110110,00010011,01010001,01000000,00010100,01001110,00110110,11100011,11000001,00110101,11010011,01011110,00110111,11010111,10001110,10000100,11010011,01111110,00110111,00001011,01111101,11111001,11010011,10001110,00110111,00010111,10101110,10111100,11010011,10001110,00110111,11010100,01011110,00111001,11010011,10001110,00110111,11011100,00111101,00000000,11010011,10001110,00110111,11010111,10000001,00000100,11010011,01111110,00110111,11011100,00111101,00000000,11010011,10001110,00110111,11100011,11001110,10110101,11010011,10001110,00110111,11110100,00000000,11111101,11010011,10001110,00110111
From here i need to convert each 8bit word into a decimal.
11010011 = 211, 01000000 = 64, ..........
The new list doesn't need to show the original 8 bits. Its only there for more detail
Where i am struggling is how to convert it natively within SQL since this is a fairly large table. My attempts have been a bunch of flops and i have hit a mental block. Any help is appreciated.
-----update-----
so what i have learnt is that i have is a hex string of comma separated values that need converted to their integer equivalent. I have tried to convert hex to in but it appears to be trying to convert the entire string at once.
SELECT CONVERT(INT, CONVERT(VARBINARY, '00B0A04500B898450070954500A89345006893450028944500109545005095450088944500D8924500D0904500288F4500608E4500A08E4500988F4500D0904500B091450000924500C09145001891450040904500788F4500D88E4500608E4500108E4500F88D4500F88D4500008E4500F88D4500E88D4500108E4500C08E45004090450038924500B0934500E8924500C08D4500A882450000624500A0334500C0FD44008093440080CC43000069C300403AC400A090C40060BCC40080E3C4002003C500E010C500D019C500901DC500B01DC500A01CC500901CC500401EC500F01FC500601EC500A015C5004003C50060CEC4006087C40000E7C30000134300C02E44000092440040C24400A0E94400C004450090114500901B45004023450040294500602E450030334500F0374500203C4500603F4500404145001042450060424500104345008044450050464500B0474500E0474500A0464500C044450020444500F0464500F04E4500E05C45000070450018834500388E4500E8974500E89E450098A24500F8A24500B0A0459AD90443AE870543B85E0543E1BA044348A10343333302437B94004348E1FD4214AEFA420080F7429A19F442713DF0428FC2EB427B94E64229DCE042CDCCDA427B94D442713DCE42AEC7C7429A19C142B81EBA42CDCCB242B81EAB427B14A34214AE9A4285EB91420AD7884285EB7E428FC26B42713D5842713D44425C8F2F4200001A428FC20342C3F5DA41C3F5B041D7A38C41AE47614100003C411F852741A4701D416666164114AE0B41CDCCF440295CC74052B8964014AE574014AE1740EC51D83F3D0A973FEC51383F295C8F3E00000000AE47E13D3D0A573F52B80E400AD78340B81EC540000000417B141641D7A324413D0A2F415C8F3A4100004C4148E1664100008641AE479D416666B84152B8D6410000F84100000E42F6282142713D3542CDCC49429A995E421F857342CD4C84421F058F4285EB994266E6A4428FC2AF42AE47BA420A57C44285EBCD42D723D742B81EE0420AD7E842F628F14214AEF842E1FAFE4248E10143146E0343C3350443F66804431F4504433D0A044314EE03433D0A0443486104439AD90443', 2))
results in
-1790640048
How do I parse the hex first then do the conversion??
Given a string of comma-delimited binary digits, one approach is to split the string into rows to get each binary value and then convert each to an integer, then re-aggregate into a delimited string.
First it would be useful to have a function to convert to an integer, one such function could use a simple tally table to split each digit into a row and then sum the bits raised to the correct power of 2.
create function BinaryToInt(#bin varchar(8))
returns table as
return
with seq(n) as (select n from (values (1), (2), (3), (4), (5), (6), (7), (8))t(n)),
bin as (
select Cast(Substring(Reverse(#bin), n, 1) as int) v, n
from seq
where n <= Len(#bin)
)
select Sum(Iif((v = 1), Power(2, n-1), 0)) [Result]
from bin;
go
And then to make use of it by splitting the source string (using OpenJson to ensure correct ordering)
with sampledata as (
select '11010011,01000000,01110100,00000011,01001110,00111001,11010011,01000000,01111100' bin
)
select String_Agg(Result, ',') within group (order by seq)
from sampledata
cross apply (
select j.[value], Convert(tinyint, j.[key]) Seq
from OpenJson(Concat('["', replace(bin, ',', '","'), '"]')) j
)j
cross apply dbo.BinaryToInt(j.[value]);
See Demo Fiddle
Sample data: 11010011,01000000,01110100,00000011,01001110,00111001,11010011,01000000,01111100
Output
Related
How do I convert decimal encoded hex to ascii in SQL Server 2012
What I have is a string of integer values that represent Letters and Numbers in pairs. I would like to use SQL to do the conversion from '12371 12595 8224' to 'S031 ' I've gotten to here... CAST(CAST(CAST(SUBSTRING(DataString,1,CHARINDEX(CHAR(9),DataString)-1) as bigint) AS VARBINARY(2)) AS VARCHAR(2)) but that gets me '0S' Note: The CHAR(9) is because the integers are tab delimited.
So here's one way you can get your desired output. Because you're using 2012 there are some useful functions for splitting and aggregating strings that are not available to you. This makes use of a tally/numbers table - simulated here with values() - but you would have a permanent table of numbers to use. This splits the string into rows, and then simply extracts the high/low bytes and uses for xml path to aggregate back into a string. See this fiddle declare #v varchar(50)='12371 12595 8224', #delimiter varchar(1)=' '; with v as ( select Convert(int,Substring(#delimiter+#v+#delimiter, n.n+1, CharIndex(#delimiter, #delimiter+#v+#delimiter, n.n+1)-n.n-1)) as v from /* replace this with tally / numbers table */ (select * from (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20))n(n))n where n.n <= len(#delimiter+#v+#delimiter)-1 and substring(#delimiter+#v+#delimiter, n.n, 1) = #delimiter ) select Concat(Char(v & 0x00ff), char(v/256)) from v for xml path(''), type
I dug a bit deeper into the datastring and found that I only had to decode the first 6 integers. So I went ugly. Considering how far I was already, the missing piece was the Reverse function. REVERSE(CAST(CAST(SUBSTRING(DataString,1,5) as bigint) AS VARBINARY(2)))+ REVERSE(CAST(CAST(SUBSTRING(DataString,7,5) as bigint) AS VARBINARY(2)))+ REVERSE(CAST(CAST(SUBSTRING(DataString,13,4) as bigint) AS VARBINARY(2)))+ REVERSE(CAST(CAST(SUBSTRING(DataString,18,4) as bigint) AS VARBINARY(2)))+ REVERSE(CAST(CAST(SUBSTRING(DataString,23,5) as bigint) AS VARBINARY(2)))+ REVERSE(CAST(CAST(SUBSTRING(DataString,29,5) as bigint) AS VARBINARY(2))) AS [Output Code] Had I needed to go through all of the integers in the string, a While loop would have worked fine.
SQL procedure algorithm generating numbers in HEX (using strings)
I'm trying to make an algorithm in SQL (stored procedure) which will generate numbers in a non-decimal number system (we can use HEX here) and write them down into file. The only issue is that, I need to use only strings to do that. I have declared my set of characters: '0123456789ABCDEF' and now I need to make a loop in witch I generate next element and save it into file.
Maybe you are looking for a decimal -> hex conversion? with data (n) as ( values (90) union all select n + 1 from data where n < 100 ), alphabet (a) as ( values ('0123456789ABCDEF') ), dechex (orig,n,hx) as ( select n, n / 16,cast(substr(a,mod(n, 16) + 1, 1) as varchar(10)) from data cross join alphabet union all select orig,n / 16,substr(a,mod(n, 16) + 1, 1) concat hx from dechex cross join alphabet where n > 0 ) select orig,hx from dechex where n = 0 order by orig
sql extract rightmost number in string and increment
i have transaction codes like "A0004", "1B2005","20CCCCCCC21" I need to extract the rightmost number and increment the transaction code by one "AA0004"----->"AA0005" "1B2005"------->"1B2006" "20CCCCCCCC21"------>"20CCCCCCCC22" in SQL Server 2012. unknown length of string right(n?) always number dealing with unsignificant number of string and number length is out of my league. some logic is always missing. LEFT(#a,2)+RIGHT('000'+CONVERT(NVARCHAR,CONVERT(INT,SUBSTRING( SUBSTRING(#a,2,4),2,3))+1)),3
First, I want to be clear about this: I totally agree with the comments to the question from a_horse_with_no_name and Jeroen Mostert. You should be storing one data point per column, period. Having said that, I do realize that a lot of times the database structure can't be changed - so here's one possible way to get that calculation for you. First, create and populate sample table (Please save us this step in your future questions): DECLARE #T AS TABLE ( col varchar(100) ); INSERT INTO #T (col) VALUES ('A0004'), ('1B2005'), ('1B2000'), ('1B00'), ('20CCCCCCC21'); (I've added a couple of strings as edge cases you didn't mention in the question) Then, using a couple of cross apply to minimize code repetition, I came up with that: SELECT col, LEFT(col, LEN(col) - LastCharIndex + 1) + REPLICATE('0', LEN(NumberString) - LEN(CAST(NumberString as int))) + CAST((CAST(NumberString as int) + 1) as varchar(100)) As Result FROM #T CROSS APPLY ( SELECT PATINDEX('%[^0-9]%', Reverse(col)) As LastCharIndex ) As Idx CROSS APPLY ( SELECT RIGHT(col, LastCharIndex - 1) As NumberString ) As NS Results: col Result A0004 A0005 1B2005 1B2006 1B2000 1B2001 1B00 1B01 20CCCCCCC21 20CCCCCCC22 The LastCharIndex represents the index of the last non-digit char in the string. The NumberString represents the number to increment, as a string (to preserve the leading zeroes if they exists). From there, it's simply taking the left part of the string (that is, up until the number), and concatenate it to a newly calculated number string, using Replicate to pad the result of addition with the exact number of leading zeroes the original number string had.
Try This DECLARE #test nvarchar(1000) ='"A0004", "1B2005","20CCCCCCC21"' DECLARE #Temp AS TABLE (ID INT IDENTITY,Data nvarchar(1000)) INSERT INTO #Temp SELECT #test ;WITH CTE AS ( SELECT Id,LTRIM(RTRIM((REPLACE(Split.a.value('.' ,' nvarchar(max)'),'"','')))) AS Data ,RIGHT(LTRIM(RTRIM((REPLACE(Split.a.value('.' ,' nvarchar(max)'),'"','')))),1)+1 AS ReqData FROM ( SELECT ID, CAST ('<S>'+REPLACE(Data,',','</S><S>')+'</S>' AS XML) AS Data FROM #Temp ) AS A CROSS APPLY Data.nodes ('S') AS Split(a) ) SELECT CONCAT('"'+Data+'"','-------->','"'+CONCAT(LEFT(Data,LEN(Data)-1),CAST(ReqData AS VARCHAR))+'"') AS ExpectedResult FROM CTE Result ExpectedResult ----------------- "A0004"-------->"A0005" "1B2005"-------->"1B2006" "20CCCCCCC21"-------->"20CCCCCCC22"
STUFF(#X ,LEN(#X)-CASE PATINDEX('%[A-Z]%',REVERSE(#X)) WHEN 0 THEN LEN(#X) ELSE PATINDEX('%[A-Z]%',REVERSE(#X))-1 END+1 ,LEN(((RIGHT(#X,CASE PATINDEX('%[A-Z]%',REVERSE(#X)) WHEN 0 THEN LEN(#X) ELSE PATINDEX('%[A-Z]%',REVERSE(#X))-1 END)/#N)+1)#N) ,((RIGHT(#X,CASE PATINDEX('%[A-Z]%',REVERSE(#X)) WHEN 0 THEN LEN(#X) ELSE PATINDEX('%[A-Z]%',REVERSE(#X))-1 END)/#N)+1)#N) works on number only strings 99 becomes 100 mod(#N) increments
SQL Summing digits of a number
i'm using presto. I have an ID field which is numeric. I want a column that adds up the digits within the id. So if ID=1234, I want a column that outputs 10 i.e 1+2+3+4. I could use substring to extract each digit and sum it but is there a function I can use or simpler way?
You can combine regexp_extract_all from #akuhn's answer with lambda support recently added to Presto. That way you don't need to unnest. The code would be really self explanatory if not the need for cast to and from varchar: presto> select reduce( regexp_extract_all(cast(x as varchar), '\d'), -- split into digits array 0, -- initial reduction element (s, x) -> s + cast(x as integer), -- reduction function s -> s -- finalization ) sum_of_digits from (values 1234) t(x); sum_of_digits --------------- 10 (1 row)
If I'm reading your question correctly you want to avoid having to hardcode a substring grab for each numeral in the ID, like substring (ID,1,1) + substring (ID,2,1) + ...substring (ID,n,1). Which is inelegant and only works if all your ID values are the same length anyway. What you can do instead is use a recursive CTE. Doing it this way works for ID fields with variable value lengths too. Disclaimer: This does still technically use substring, but it does not do the clumsy hardcode grab WITH recur (ID, place, ID_sum) AS ( SELECT ID, 1 , CAST(substring(CAST(ID as varchar),1,1) as int) FROM SO_rbase UNION ALL SELECT ID, place + 1, ID_sum + substring(CAST(ID as varchar),place+1,1) FROM recur WHERE len(ID) >= place + 1 ) SELECT ID, max(ID_SUM) as ID_sum FROM recur GROUP BY ID
First use REGEXP_EXTRACT_ALL to split the string. Then use CROSS JOIN UNNEST GROUP BY to group the extracted digits by their number and sum over them. Here, WITH my_table AS (SELECT * FROM (VALUES ('12345'), ('42'), ('789')) AS a (num)) SELECT num, SUM(CAST(digit AS BIGINT)) FROM my_table CROSS JOIN UNNEST(REGEXP_EXTRACT_ALL(num,'\d')) AS b (digit) GROUP BY num ;
Remove ASCII Extended Characters 128 onwards (SQL)
Is there a simple way to remove extended ASCII characters in a varchar(max). I want to remove all ASCII characters from 128 onwards. eg - ù,ç,Ä I have tried this solution and its not working, I think its because they are still valid ASCII characters? How do I remove extended ASCII characters from a string in T-SQL? Thanks
The linked solution is using a loop which is - if possible - something you should avoid. My solution is completely inlineable, it's easy to create an UDF (or maybe even better: an inline TVF) from this. The idea: Create a set of running numbers (here it's limited with the count of objects in sys.objects, but there are tons of example how to create a numbers tally on the fly). In the second CTE the strings are splitted to single characters. The final select comes back with the cleaned string. DECLARE #tbl TABLE(ID INT IDENTITY, EvilString NVARCHAR(100)); INSERT INTO #tbl(EvilString) VALUES('ËËËËeeeeËËËË'),('ËaËËbËeeeeËËËcË'); WITH RunningNumbers AS ( SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS Nmbr FROM sys.objects ) ,SingleChars AS ( SELECT tbl.ID,rn.Nmbr,SUBSTRING(tbl.EvilString,rn.Nmbr,1) AS Chr FROM #tbl AS tbl CROSS APPLY (SELECT TOP(LEN(tbl.EvilString)) Nmbr FROM RunningNumbers) AS rn ) SELECT ID,EvilString ,( SELECT '' + Chr FROM SingleChars AS sc WHERE sc.ID=tbl.ID AND ASCII(Chr)<128 ORDER BY sc.Nmbr FOR XML PATH('') ) AS GoodString FROM #tbl As tbl The result 1 ËËËËeeeeËËËË eeee 2 ËaËËbËeeeeËËËcË abeeeec Here is another answer from me where this approach is used to replace all special characters with secure characters to get plain latin