SQL procedure algorithm generating numbers in HEX (using strings) - sql

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

Related

How to convert Base64 to a comma separated binary

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

Generate random numbers, letters or characters within a range

I'm in the middle of a data anonymization for SQL Server.
I have this 3 formulas that help me create what I want:
SELECT CHAR(cast((90 - 65) * rand() + 65 AS INTEGER)) -- only letters
SELECT CAST((128 - 48) * RAND() + 48 AS INTEGER) -- only numbers
SELECT CHAR(CAST((128 - 48) * RAND() + 48 AS INTEGER)) -- letters, numbers, symbols
However, this only can create 1 number or 1 letter or 1 symbol.
I want to have the freedom that allows me to create a random string or number of the length I want. Like 3 or 5 numbers, 3 or 5 letters, 3 or 5 between numbers, letters or symbols.
I also have found something very close to what I want:
SELECT LEFT(CAST(NEWID() AS VARCHAR(100)), 3) -- letters and numbers
this is a very smart idea because uses NEWID() and it allows me to create a random sequence of numbers and letters of the length I want (3 in this case). But symbols are missing.
I need 3 different SELECT:
One for numbers only
One for letters only
One for numbers, letters and symbols
With the freedom of choice about the length of the data.
Some work required for a complete solution but here's the workings of an idea you might want to experiment with further, if you still need it:
declare #type varchar(10)='letters', #length tinyint=5;
with chars as (
select top(59) 31 + Row_Number() over (order by (select 1)) n from master.dbo.spt_values
), s as (
select top (#length) Char(n.n) c
from chars n
where #type='all'
or (#type='symbols' and n between 33 and 47)
or (#type='numbers' and n between 48 and 57)
or (#type='letters' and n between 65 and 90)
order by newid()
)
select String_Agg(s.c,'')
from s
Recursive query might work with rand() function:
declare #desiredlength tinyint=5;
With builder As (
Select *
From (Values (0, '', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')) initial (length, randstr, pool)
Union All
Select length+1, randstr + substring(pool,cast(rand()*len(pool)+1 AS int),1), pool
From builder
Where length<#desiredlength
)
Select randstr From builder
Where length=#desiredlength
rand() in a single select returns the same random number in each row of a select. But here in a recursive select you're in a grey area where each recursion might be treated like a separate query.
Obviously you can tailor the pool definition to be any character set you want and the rest of the code will choose from whatever's there.

How to auto generate a ID with random numbers in sql server

For example I had a column named 'ID'
I want to get the output as
ID
---
ABCXX708
ABCXX976
ABCXX654
ABCXX081
In short ABCXX should be common for every row but the remaining 3 numbers should be random and integer..
with t (n) as (select 0 union all select n+1 from t where n <100)
select 'ABC'
+ format(n,'00')
+ cast(cast(rand(cast(newid() as varbinary(100)))*10 as int) as char(1))
from t
Alternative solution
with t (n) as (select 0 union all select n+1 from t where n <100)
select 'ABC'
+ right ('0' + cast(n as varchar(2)),2)
+ cast(cast(rand(cast(newid() as varbinary(100)))*10 as int) as char(1))
from t
You can write like this
select 'ABCXX'+CAST(FLOOR(RAND()*(1000-100)+100) as varchar(3)) 'id'
With the RAND() function you can get Random numbers. And For the 'ABCXX' you can follow your previous logic.
SELECT CAST(RAND()*10.00 AS INT)
The above RAND() function will give values between 0.0 to 1.0 in decimals every time you hit the Statement. To make it for a single digit Multiply with 10 and Cast it to INT to remove the next decimal values.
Reference " MSDN
Since SQL Server 2012 you have FORMAT function and SEQUENCE object. Hence the below query will work.
First you need to create a Sequence object.
CREATE SEQUENCE DemopSeq
START WITH 1
INCREMENT BY 1;
Then the following query will generate results as per your requirement.
SELECT CONCAT('ABC',FORMAT(NEXT VALUE FOR DemopSeq, '00'),ABS(Checksum(NewID()) % 10))
Hope this helps.

Teradata : Sum up values in a column

Problem Statement
Example is shown in below image :
The last 2 rows have the patterns like "1.283 2 3" in a single cell. The numbers are seperated by space in the column. We need to add those nos and represent in the format given in Output.
So, the cell having "1.283 2 3" must be converted to 6.283
Challenges facing :
The column values are in string format.
Add nos after casting them into integer
Donot want to take data in UNIX box and manipulate the same.
In TD14 there would be a built-in table UDF named STRTOK_SPLIT_TO_TABLE, before you need to implement your own UDF or use a recursive query.
I modified an existing string splitting script to use blanks as delimiter:
CREATE VOLATILE TABLE Strings
(
groupcol INT NOT NULL,
string VARCHAR(991) NOT NULL
) ON COMMIT PRESERVE ROWS;
INSERT INTO Strings VALUES (1,'71.792');
INSERT INTO Strings VALUES (2,'71.792 1 2');
INSERT INTO Strings VALUES (3,'1.283 2 3');
WITH RECURSIVE cte
(groupcol,
--string,
len,
remaining,
word,
pos
) AS (
SELECT
GroupCol,
--String,
POSITION(' ' IN String || ' ') - 1 AS len,
TRIM(LEADING FROM SUBSTRING(String || ' ' FROM len + 2)) AS remaining,
TRIM(SUBSTRING(String FROM 1 FOR len)) AS word,
1
FROM strings
UNION ALL
SELECT
GroupCol,
--String,
POSITION(' ' IN remaining)- 1 AS len_new,
TRIM(LEADING FROM SUBSTRING(remaining FROM len_new + 2)),
TRIM(SUBSTRING(remaining FROM 1 FOR len_new)),
pos + 1
FROM cte
WHERE remaining <> ''
)
SELECT
groupcol,
-- remove the NULLIF to get 0 for blank strings
SUM(CAST(NULLIF(word, '') AS DECIMAL(18,3)))
FROM cte
GROUP BY 1
This might use a lot of spool, hopefully you're not running that on a large table.

sql FInding strings with duplicate characters

I have a list of strings:
HEAWAMFWSP
TLHHHAFWSP
AWAMFWHHAW
AUAWAMHHHA
Each of these strings represent 5 pairs of 2 character combinations (i.e. HE AW AM FW SP)
What I am looking to do in SQL is to display all strings that have duplication in the pairs.
Take string number 3 from above; AW AM FW HH AW. I need to display this record because it has a duplicate pair (AW).
Is this possible?
Thanks!
Given current requirements, yes this is dooable. Here's a version which uses a recursive CTE (text may need to be adjusted for vendor idiosyncracies), written and tested on DB2. Please note that this will return multiple rows if there is more than 2 instances of a pair in a string, or more than 1 set of duplicates.
WITH RECURSIVE Pair (rowid, start, pair, text) as (
SELECT id, 1, SUBSTR(text, 1, 2), text
FROM SourceTable
UNION ALL
SELECT rowid, start + 2, SUBSTR(text, start + 2, 2), text
FROM Pair
WHERE start < LENGTH(text) - 1)
SELECT Pair.rowid, Pair.pair, Pair.start, Duplicate.start, Pair.text
FROM Pair
JOIN Pair as Duplicate
ON Duplicate.rowid = Pair.rowid
AND Duplicate.pair = Pair.pair
AND Duplicate.start > Pair.start
Here's a not very elegant solution, but it works and only returns the row once no matter how many duplicate matches. The substring function is for SQLServer, not sure what it is for Oracle.
select ID, Value
from MyTable
where (substring(Value,1,2) = substring(Value,3,4)
or substring(Value,1,2) = substring(Value,5,6)
or substring(Value,1,2) = substring(Value,7,8)
or substring(Value,1,2) = substring(Value,9,10)
or substring(Value,3,4) = substring(Value,5,6)
or substring(Value,3,4) = substring(Value,7,8)
or substring(Value,3,4) = substring(Value,9,10)
or substring(Value,5,6) = substring(Value,7,8)
or substring(Value,5,6) = substring(Value,9,10)
or substring(Value,7,8) = substring(Value,9,10))