Convert Password varchar() to binary datatype in sql - sql

I want to take password from Table 1 to Table 2. So I have to convert.
Table 1:
Password(varchar)
Table 2:
Password(binary)
I am logging here convert varchar to binary.I try this following query,
IsNull(''''+cast(wl.password as binary)+'''', 'NULL')+')'
but no use. It is showing error like,
The data types varchar and binary are incompatible in the add operator.
give me some suggestions?

Try using convert function in sql.
Eg.
convert(varchar, ' + #password + ')
Refer:
SQL server + dynamic query + 'The data types nvarchar and bit are incompatible in the add operator.'
Hope its helpful.

Don't cast it to binary before concatenating a string, but within the string, so it get casted when dynamic query is executed.
Something like:
ISNULL('cast(''' +wl.password+ ''' as binary)','NULL')

Related

Converting nvarchar to int, converting phone with symbols with only numbers

I am trying to convert phone number from the column 'phone' from the table 'Clients'. I have tried the following syntaxes, but I still get error messages -
1. SELECT CAST(phone as int)
FROM Clients
Error: Conversion failed when converting the nvarchar value '030-3456789' to data type int
2. SELECT CONVERT(int, phone)
FROM Clients
Conversion failed when converting the nvarchar value '030-3456789' to data type int.
3. SELECT CAST(phone AS BIGINT)
FROM Clients
WHERE ISNUMERIC(phone) = 1
The query doesn't return error but there is no result, the column is empty.
It looks (from your example syntax) like you might be using SQL Server.
If that's the case and it's 2017+ you can do the following which copes with any combination of non-numeric values.
Based on your comments the following should work
select Try_Convert(bigint, Replace(Translate('(5) 789-0123','()-',' '),' ',''))
Result: 57890123
If you are using SQL Server 2016 or earlier you have to nest multiple replacements:
select Try_Convert(bigint, Replace(Replace(Replace(Replace('(5) 789-0123)','-',''),'(',''),')',''),' ',''))
Because at least some of your records cannot be covert to numeric by default, as the indicated one 030-3456789
You basically need to replace/eliminate the dash in between:
SELECT cast(replace('12-3', '-', '') as int)
Anyway, welcome to StackOverflow.

How can I get rid of having to prefix a WHERE query with 'N' for Unicode strings?

When searching for a string in our database where the column is of type nvarchar, specifying the 'N' prefix in the query nets some results. Leaving it out does not. I am trying the search for a Simplified Chinese string in a database that previously did not store any Chinese strings yet.
The EntityFramework application that uses the database, correctly retrieves the strings and the LINQ queries also work in the application. However, in SQL Server 2014 Management Studio, when I do a an SQL query for the string it does not show up unless I specify the 'N' prefix for unicode. (Even though the column is nvarchar type)
Works:
var text = from asd in Translations.TranslationStrings
where asd.Text == "嗄法吖无上几"
select asd;
MessageBox.Show(text.FirstOrDefault().Text);
Does not work:
SELECT *
FROM TranslationStrings
where Text = '嗄法吖无上几'
If I prefix the Chinese characters with 'N' it works.
Works:
SELECT *
FROM TranslationStrings
where Text = N'嗄法吖无上几'
Please excuse the Chinese characters, I just typed something random. My question is, is there something I can do to not have to include the 'N' prefix when doing a query?
Thank you very much!
As #sworkalot has mentioned below:
The default for .Net is Unicode, that's why you don't need to specify
it. This is not the case for Sql Manager.
If not specified Sql will assume that you work with asci according to
the collation specified in your DB.
Hence, when working from Sql Server you need to use N'
https://sqlquantumleap.com/2018/09/28/native-utf-8-support-in-sql-server-2019-savior-false-prophet-or-both/
Check out these examples, pay close attention to the data types and the values being assigned:
DECLARE #Varchar VARCHAR(100) = '嗄'
DECLARE #VarcharWithN VARCHAR(100) = N'嗄' -- Has N prefix
DECLARE #NVarchar NVARCHAR(100) = '嗄'
DECLARE #NVarcharWithN NVARCHAR(100) = N'嗄' -- Has N prefix
SELECT
Varchar = #Varchar,
VarcharWithN = #VarcharWithN,
NVarchar = #NVarchar,
NVarcharWithN = #NVarcharWithN
SELECT
Varchar = CONVERT(VARBINARY, #Varchar),
VarcharWithN = CONVERT(VARBINARY, #VarcharWithN),
NVarchar = CONVERT(VARBINARY, #NVarchar),
NVarcharWithN = CONVERT(VARBINARY, #NVarcharWithN)
Results:
Varchar VarcharWithN NVarchar NVarcharWithN
? ? ? 嗄
Varchar VarcharWithN NVarchar NVarcharWithN
0x3F 0x3F 0x3F00 0xC455
NVARCHAR data type stores 2 bytes for each character while VARCHAR only stores 1 (you can see this on the VARBINARY cast on the 2nd SELECT). Since chinese characters representation need 2 bytes to be stored, you have to use NVARCHAR to store them. If you try to stuff them in a VARCHAR it will be stored as ? and you will lose the original character information. This also happens on the 3rd example, because the literal doesn't have the N so it's converted to VARCHAR before actually assigning the value to the variable.
It's because of this that you need to add the N prefix when typing these characters as literals, so the SQL engine knows that you are typing characters that need 2 byte representation. So if you are doing a comparison against a NVARCHAR column always add the N prefix. You can change the database collation, but it's recommended to always use the proper data type independent of the collation so you don't have problems when using coding on different databases.
If you could explain the reason why you want to omit the N prefix we might address that, although I believe there is no work around in this particular case.
The default for .Net is Unicode, that's why you don't need to specify it.
This is not the case for Sql Manager.
If not specified Sql will assume that you work with asci according to the collation specified in your DB.
Hence, when working from Sql Server you need to use N'
https://sqlquantumleap.com/2018/09/28/native-utf-8-support-in-sql-server-2019-savior-false-prophet-or-both/

Error converting data type DBTYPE_DBTIMESTAMP to datetime2

I have this code
SELECT
TRY_CONVERT(varchar(150), "PCR_Fecha") AS "PCR_Fecha"
FROM OPENQUERY(EXTRACCION, 'SELECT * FROM EXTRACCION.Extraccion')
But i get the error
Error converting data type DBTYPE_DBTIMESTAMP to datetime2.
I know there are wrong values in PCR_Fecha (like 40218:00:00 or 14mayo09) that's why I'm trying to convert them to varchar.
I can see the data using
SELECT * FROM OPENQUERY(EXTRACCION, 'SELECT CAST(PCR_Fecha AS varchar(26)) FROM EXTRACCION.Extraccion');
The linked server is from Filemaker and there PCR_Fecha is set as a date.
Looking for an answer I found that I could define "dbtimestamp_rules=2" in connection string but I don't know how to add the rule.
Any other suggestions?
I found that I could change the data type from Filemaker and set it to text (it was date before) and that solves the problem.
With sql it was
SELECT
Convert(NVARCHAR(26),PCR_Fecha)
FROM OPENQUERY(EXTRACCION, 'SELECT CAST(PCR_Fecha AS VARCHAR(26)) as PCR_Fecha FROM EXTRACCION.Extraccion')
A cast and then convert, anyways thx :)

Using Replace And Convert with varbinary

I need a query that will find part of a string in a varbinary and remove on the requested part of the string
For example my table dbo.inventory_table has to columns CharacterIdx and Data.
Data is the target column which contains varbinary so for characteridx 101756 data is this
0x2105000000000000430000000000000000003C090000000000002C0100000200000000F83D09000000000000580200000400000000F83E09000000000000E80300000600000000F8
I want a query to find and remove
3C090000000000002C0100000200000000F83D09000000000000580200000400000000F83E09000000000000E80300000600000000F8
but leave
0x210500000000000043000000000000000000
Where CharacterIdx = 101756
I have tried
UPDATE [dbo].[Inventory_table]
SET Data = REPLACE(Data, '3C090000000000002C0100000200000000F83D09000000000000580200000400000000F83E09000000000000E80300000600000000F8', '')
WHERE CharacterIdx = 101756
But it gives me an error:
Msg 257, Level 16, State 3, Line 3
Implicit conversion from data type varchar to varbinary is not allowed.
Use the CONVERT function to run this query.
I need the string to remain varbinary though.
I believe that you need to convert it back to varbinary asreplacereturns avarcharvalue from implicit conversion. Try this:
UPDATE [dbo].[Inventory_table]
SET Data = CAST(REPLACE(Data, 0x3C090000000000002C0100000200000000F83D09000000000000580200000400000000F83E09000000000000E80300000600000000F8, 0x) AS varbinary)
WHERE CharacterIdx = 101756
On my server this changes the value to:
0x210500000000000043000000000000000000

How to compare two different data type variables in where condition of SQL query

I have one column which is integer in first table and second is varcha. Now I need to compare these 2 variables which are of different data types and retrieve data if it matches.
Please could please help me out how to work on this?
Please specify which DBMS you're using please.
Sql Server?
CAST / CONVERT
EDIT:
Well, I see you mentioned Sql*Plus above which to me says Oracle so for the sake of thoroughness. CAST appears to be what you're looking for. CONVERT does something a bit different in Oracle so you'd be doing something like
CAST(#YourStringValue AS INTEGER) = #YourOtherIntegerValue
or whatever datatype you want to cast your column to and then compare it against your other value. The syntax may be off a bit as I'm no Oracle guy but this should point you in the right direction.
If it is about MS SQL, then it will cast those for you implicitly, no need to cast or convert.
declare #IntVariable int
set #IntVariable = 3
declare #VarcharVariable varchar(10)
set #VarcharVariable = '3'
select case when #IntVariable = #VarcharVariable then 1 else 0 end
You can either cast varchar to int or int to varchar and then compare.
Short example of casting:
CAST(ColumnName as datatype)as CertainName
Here CertainName means u can show that to display as your new column name
if yo are using c# java or other strict type programming languase then you will have to convert the int type into string then you will be able to compare them if you are using PHP then no conversion will be required