Convert varchar to hexadecimal in sql server - sql-server

I want a function to convert a varchar to hexadecimal in sql server. Please help me.
P.S. I will use unhex() in Hive to try to get the original value back. This is because my data contains special characters and backslash and the HIVE external table does not recognise it

You can try to use CONVERT function:
SELECT CONVERT(VARBINARY(MAX), 'myText')
Output:
0x6D7954657874

you can use
select try_convert(varbinary,varcharcolumn)

You should use try_convert to avoid errors when the conversion fails.. Also, varbinary(n) is a better alternative to varbinary(max) as the latter would set the page size to 2GB, which might be excessive.
Hope this helps!

Related

Why does ISNUMERIC() state a Zip Code, which is a varchar data type, as numeric in SQL server?

From my understanding, and I am probably wrong here, but doesn't the ISNUMERIC() return a 1 if whatever we are looking at is a numeric data type? And since most zip codes are saved as varchar data types, shouldn't it then return a 0? I looked up the documentation and that's what it says there to me, what am I missing here? I get zip codes are numbers, but because they are saved as a string shouldn't that make a difference? Thanks in advance.
ISNUMERIC() is specifically to look at strings, not at things already stored as numbers.
However, I don't recommend using it. For instance, '3.1e5' is considered numeric.
Instead, use try_convert():
try_convert(int, zip)
This returns NULL if the column cannot be converted.

DT_NTEXT to DT_STR Conversion

I have a field JSONStructure which has NT_TEXT Data Type and have to do a replace function on that.But, It looks like I cannot do a replace function on column having DT_NText DataType. I tried using Data conversion in SSIS But my JSONStructure can have more than 8000 characters and It is not working.
Can someone suggest me the best way to do it.
Thanks in Advance.
I think you'll need to use a Script Component, acting as a Transformation, and you'll need to specify that the column is read/write and then use C#/VB.NET string methods to perform the string manipulation

Error converting Varchar to Decimal SQL Server

I have a staging table loaded with data from a SAS dataset containing 5M records. All the columns are varchar. I am trying to convert a couple of columns to decimal(32,10). But it generates an error. I tried cast, I tried convert and even splitting the data up before and after decimal - same result.
I looked at the IsNumeric flag of the column and there are 0 records <> 1 meaning the data is numeric.
case
when wtd_count = '.' THEN NULL
when wtd_count = '' THEN NULL
else convert(decimal(32, 10), wtd_count)
end
Error:
Msg 8114, Level 16, State 5, Line 99
Error converting data type varchar to numeric.
So I'm wondering what else I can do to convert the data to decimal? Any idea?
Any help will greatly be appreciated.
If you are in SQL Server 2012 and above try to use try_parse or try_convert
ISNUMERIC is not reliable for what you're doing. It will flag values with things like monetary symbols and commas in them as valid.
It seems quite likely that there is some non-numeric data present. TRY_CONVERT or TRY_PARSE are your friend here. As an FYI, SQL Server version 11.0.x is SQL Server 2012, so you should be able to use these.
I also find it hard to believe that converting to numeric works, but not decimal. I can find no information that suggests the actual implementation of these two data types is different, and as such neither should work.
I would do some more in depth analysis of your data to make sure it looks like you're expecting it to.
After read your case statement i suppose that you have coma separated values. I'm pretty sure that you should use: CONVERT(DECIMAL(32,10),REPLACE(wtd_count,',','.'))

Cyrillics in LINQ to SQL database

I have comma separated file with cyrillics characters. When I read from it using StreamReader the characters are OK. I'm writing the file in LINQ to SQL database, however the column with cyrillics is written with ?????? .The column is nvarchar type. Does somebody had the same problem?
i think that is a encoding problem, maybe the SQL DB has not the properly library to encoding the Cyrillics.
take a look here: http://dev.mysql.com/doc/refman/5.0/en/charset-cyrillic-sets.html
try Encoding.Utf8:
new StreamReader("THE PATH", Encoding.UTF8);
You may have to change the collattion of you database
http://msdn.microsoft.com/en-us/library/ms180175.aspx

SQL LIKE Operator doesn't work with Asian Languages (SQL Server 2008)

Dear Friends,
I've faced with a problem never thought of ever. My problem seems too simple but I can't find a solution to it.
I have a sql server database column that is of type NVarchar and is filled with standard persian characters. when I'm trying to run a very simple query on it which incorporates the LIKE operator, the resultset becomes empty although I know the query term is present in the table. Here is the very smiple example query which doesn't act corectly:
SELECT * FROM T_Contacts WHERE C_ContactName LIKE '%ف%'
ف is a persian character and the ContactName coulmn contains multiple entries which contain that character.
Please tell me how should I rewrite the expression or what change should I apply. Note that my database's collation is SQL_Latin1_General_CP1_CI_AS.
Thank you very much
Also, if those values are stored as NVARCHAR (which I hope they are!!), you should always use the N'..' prefix for any string literals to make sure you don't get any unwanted conversions back to non-Unicode VARCHAR.
So you should be searching:
SELECT * FROM T_Contacts
WHERE C_ContactName COLLATE Persian_100_CI_AS LIKE N'%ف%'
Shouldn't it be:
SELECT * FROM T_Contacts WHERE C_ContactName LIKE N'%ف%'
ie, with the N in front of the comparing string, so it treats it like an nvarchar?

Resources