I am working with a database that encodes strings to hex and then pads the entire string with 0's.
Like this:
Origional String: PartitionTest
JUST Hex-encoded Strring: 0x506172746974696f6e54657374
The output I see: 0x50006100720074006900740069006F006E005400650073007400
Is there anyway for me to remove the spaces and decode the final string back to text? AND, how would I then encode that text back to the padded string? I am already converting the string to hex with
SELECT
MASTER.dbo.fn_varbintohexstr(CAST('PartitionTest' AS VARBINARY))
but I don't have a clue how to pad that result out.
Thank you!
EDIT:
As input is varbinary, need to first convert it into varchar to use STUFF.
Declare #input varbinary(128) = 0x50006100720074006900740069006F006E005400650073007400
Declare #Temp as VARCHAR(350)
Set #Temp = CONVERT(VARCHAR(350), #input ,2)
declare #length int
set #length = len(#Temp)
select #length
declare #i int = 3
WHILE #i < #length/2+2
BEGIN
Set #Temp = Stuff(#Temp, #i, 2, '')
set #i = #i +2
END
select '0x' + #Temp
Related
I have a varchar that always come into this format:
'PB' + multiple Leading 0 + Number + Non-Number Character(s).
For example: PB000013452S, PB000013452S3s2fss.
How do I parse the varchar value to get the "Number" (13452) in this case?
Use PATINDEX to find the position of the first number (that isn't 0) and then PATINDEX again to find the position of the first non-numerical character afterwards. Then use SUBSTRING to extract the number:
SELECT SUBSTRING(V.YourString,PI.I,PATINDEX('%[^0-9]%',STUFF(V.YourString,1,PI.I-1,''))-1)
FROM (VALUES('PB000013452S'),('PB000013452S3s2fss'))V(YourString)
CROSS APPLY (VALUES(PATINDEX('%[1-9]%',V.YourString)))PI(I)
I write an algorithme for your problem you can try it and i tested befor it works perfectly but i stored numbers in a table and if you want to concatenate them you can use cursor
declare #x varchar(30) = 'PB000013452S3s2fss' /*your string here*/
declare #_len int = len(#x) /*length of your string */
declare #array table (num varchar(30)) /*table for collecte number*/
declare #c int =1 /*counter*/
declare #_char varchar(1) /* to store one char from your string */
declare #result varchar(30)=''
while #_len>0
begin
set #_char = SUBSTRING(#x,#c,1)
if(#_char in ('1','2','3','4','5','6','7','8','9'))
begin
while #_len>0
begin
set #_char = SUBSTRING(#x,#c,1)
if(#_char in ('0','1','2','3','4','5','6','7','8','9'))
begin
insert into #array values (#_char)
set #c = #c+1
set #_len = #_len-1
end
else
set #_len = 0
end
end
set #c = #c+1
set #_len = #_len-1
end
select * from #array
I have a Field value in SQL Server's table which contains the path of file, that path is delimited with - character ,What I want is to select the sub-string from beginning (zero index) to last occurrence of that delimited character.
DECLARE #path NVARCHAR(500)
SELECT #path = 'F:\Fruit Seeds-Category Oil\Quality- Fine Seeds',
I need to extract sub-string "F:\Fruit Seeds-Category Oil\Quality" only
My Query
SELECT LEFT(#path, LEN(#path) - CHARINDEX('-',REVERSE(#path)))
What is the best way to do this?
This finds the last index of "-" in the string:
DECLARE #path NVARCHAR(500) = 'F:\Fruit Seeds-Category Oil\Quality- Fine Seeds';
DECLARE #i INT = 1;
DECLARE #ix INT = 0;
WHILE #i > 0
BEGIN
SET #ix = #i;
SET #i = CHARINDEX('-', #Path, #i + 1);
END
SELECT LEFT(#path, #ix - 1)
You can clearly put it inside a function for reuse.
I have a column of hexadecimal values in a table. I want to add a hex value to all values in that table. If it were a simple int I would run something like this:
UPDATE myTable
SET num = num + 4000
Is there any way to do this simply using hexadecimal arithmetic? Or do I have to convert the column value to decimal, convert the value I want to add to decimal, add them, and convert the value back to hex? (And if so, what's the simplest way to do that?)
(NOTE: We are currently using sql server 2000.)
use something like :
print convert(varbinary(4),0 + 0x002E + 0x001D)
it should give you a result like :
0x0000004B
the zero in the equation fools it to believe its all numbers so it calculates the value.
Assuming that num is actually a string representation of the hexadecimal number, I think you can convert it to an integer by using a couple of User Defined Functions:
-- Based on Feodor's solution on
-- http://blog.sqlauthority.com/2010/02/01/sql-server-question-how-to-convert-hex-to-decimal/
CREATE FUNCTION fn_HexToInt(#str varchar(16))
RETURNS BIGINT AS BEGIN
SELECT #str=upper(#str)
DECLARE #i int, #len int, #char char(1), #output bigint
SELECT #len=len(#str),#i=#len, #output=case WHEN #len>0 THEN 0 END
WHILE (#i>0)
BEGIN
SELECT #char=substring(#str,#i,1)
, #output=#output
+(ASCII(#char)
-(case when #char between 'A' and 'F' then 55
else case when #char between '0' and '9' then 48 end
end))
*power(16.,#len-#i)
, #i=#i-1
END
RETURN #output
END
-- Example conversion back to hex string - not very tested
CREATE FUNCTION fn_IntToHex(#num int)
RETURNS VARCHAR(16) AS BEGIN
DECLARE #output varchar(16), #rem int
SELECT #output = '', #rem=0
WHILE (#num > 0)
BEGIN
SELECT #rem = #num % 16
SELECT #num = #num / 16
SELECT #output = char(#rem + case when #rem between 0 and 9 then 48 else 55 end) + #output
END
RETURN #output
END
select dbo.fn_HexToInt ('7FFF') -- = 32767
select dbo.fn_IntToHex(32767) -- = 7FFF
So you can try
UPDATE myTable
SET num = dbo.fn_IntToHex(dbo.fn_HexToInt(num) + 4000)
You can use the prefix 0x
eg
Select 0x3F + 2
returns 65
So
UPDATE myTable
SET num = num + 0x4000
(This works in SQL 2008 - I'm not sure if it's new since SQL 2000 - let me know!)
If you have two 0x values, they get concatenated by the + operator, so use convert to convert one of them to an int
How do I
Take:
RJI#\\\\Cjserver\TrialWorks\CaseFiles\10000269\Pleadings\RJI - 10005781.doc
Find Constant expression '\\Cjserver\' and take everything to the right of the expression so the correct pointer would be:
\\\\Cjserver\TrialWorks\CaseFiles\10000269\Pleadings\RJI - 10005781.doc
I know some kind of combinaton of RIGHT and CHARINDEX should do it.
DECLARE #input NVarChar(1000) =
'RJI#\\Cjserver\TrialWorks\CaseFiles\10000269\Pleadings\RJI - 10005781.doc',
#match NVarChar(100) =
'\\Cjserver';
DECLARE #position Int = CHARINDEX(#match, #input);
SELECT SUBSTRING(#input, #position, 1000);
I'm just using 1000 for some arbitrarily large value. You should probably size this more appropriately to your data.
You want to use Substring, starting one after the index of your target, and take the length of the entire string less the charindex of your target
declare #string varchar(1000)
set #string = 'xxxxxxxxyzzzzzzzz'
select substring(#string, charindex('y', #string) +1,
len(#string) - charindex('y', #string))
zzzzzzzz
In this case I want everything after the y
DECLARE #String VARCHAR(100)
SET #String = 'RJI#\\Cjserver\TrialWorks\CaseFiles\10000269\Pleadings\RJI - 10005781.doc'
SELECT RIGHT(#String,LEN(#String)-PATINDEX('%\\Cjserver\%',#String)+1)
I have a binary field in SQL Server which I want to read one byte at time in a SQL function. In code I would use a byte array. Is there an equivalent in SQL?
I couldn't find anything with google.
The SUBSTRING function should be sufficient. A quick example, assuming table MyTable with column SomeData, binary(10) not null:
DECLARE
#OneByte binary(1)
,#Loop int
SET #Loop = 0
WHILE #Loop < 10
BEGIN
SET #Loop = #Loop + 1
SELECT #OneByte = substring(SomeData, #Loop, 1)
from MyTable
-- Process accordingly
END
There are fancier set-based ways to do this, but for short values this should be adequate.
You could loop through the binary field using SUBSTRING.
declare #BinaryColumn binary(5)
set #BinaryColumn = convert(binary,'abcde')
declare #Counter int, #ColumnLength int
set #Counter = 1
set #ColumnLength = LEN(#BinaryColumn)
while (#Counter <= #ColumnLength) begin
select SUBSTRING(#BinaryColumn, #Counter, 1)
set #Counter = #Counter + 1
end /* while */
Varbinary as a type will act as a byte array, and you can read an individual byte from it using substring.