Explicitly convert datetime to varbinary - sql

I am attempting to convert a datetime column to varbinary(100), but haven't had any luck. My problem is very similar to Convert datetime to varbinary inside update query, but I still wasn't able to develop a solution.
Here is my code:
OPEN SYMMETRIC KEY SymKey_TheDate_SYMMETRIC
DECRYPTION BY CERTIFICATE Certificate_TheDate_Encryption
UPDATE PROFILE_DATA
SET A29_FDATE = EncryptByKey (Key_GUID('SymKey_TheDate_SYMMETRIC'), CONVERT(varbinary(100), A29_FDATE))
FROM PROFILE_DATA
GO
CLOSE SYMMETRIC KEY SymKey_TheDate_SYMMETRIC
Here is the error I get when trying to convert A29_FDATE from datetime to varbinary(100):
Implicit conversion from data type varbinary to datetime2 is not allowed. Use the CONVERT function to run this query.

Your error is not for datetime to varbinary(100), but for varbinary to datetime2. The function EncryptByKey() returns a varbinary that is assigned to a date field A29_FDATE. The output of EncryptByKey() needs explicit conversion if you are going to attempt it.
Something like:
...
SET A29_FDATE = convert(datetime2(0), EncryptByKey(...))
...

Re-read your error:
Implicit conversion from data type varbinary to datetime2 is not allowed. Use the CONVERT function to run this query.
You get a varbinary value just fine. The error is converting it to datetime2. The statement is converting it to datetime2 when it tries to set the field 'A29_FDATE` to the varbinary value. I assume from the field name it is a datetime2 field. I don't think you can or should convert this to a datetime2 field.
If you are in the process of encrypting the field in your table, you will have to create a new column with the type varbinary, probably A29_FVARBIN (yuck), and update that to the result of your query. Then you can drop the A29_FDATE field if you want.

Related

Database export fails when I try to export this format '2020-04-13 14:13:54'

When I try to execute this insert
INSERT INTO BCS_EXPEDIENTES_REGISTRADOS (FOLIO, DOCUMENTO, FECHA_REGISTRO_DPS, CANT_PAGINAS)
VALUES ('24', 'Suc4437_X722INSURGEN_20200305033042.tiff', '2020-04-13 14:13:54', '79')
I get an error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The process is:
I have a value example: 04/13/2020 09:13:41
I convert this value to this format: =format([G_RECEPCION], "yyyy-MM-dd HH:mm:ss") to 2020-04-13 14:13:54
But when I execute the INSERT, it throws that error.
Any ideas for this case? I need to export the datetime in this format on SQL Server yyyy-MM-dd HH:mm:ss
Let me start by saying that datetime has no concept of display format.
The display format is only relevant when we talk about the string representations of datetime values.
Then, lets take a look at the format you're using: yyyy-MM-dd HH:mm:ss (known as ODBC canonical).
When converting strings of this format to DateTime, the result of the conversion is culture dependent.
This means that when operating in some languages (like English) SQL Server will attempt to use yyyy-MM-dd as date, but in other languages (like German) it will attempt to use yyyy-dd-MM as date.
This is the the reason you get a conversion error.
Importent note: Unless you explicitly set the language (or DateFormat, for that matter), SQL Server will use the default language of the login - so for some users conversion might fail while for other users it will succeed.
Another note is that this problem only exists with DateTime, but not with DateTime2 - converting this format to DateTime2 will always be interpreted as yyyy-MM-dd.
So, considering all this information, you have three options here:
Stop using DateTime, use DateTime2 instead.
Instead of using the unsafe ODBC canonical format use the safe ISO8601 format whenever dealing with string representation of datetime values: yyyy-mm-ddThh:mi:ss.mmm.
Explicitly set language or date format (to ymd) before your insert statement.
I would recommend combining the first two and avoid using the third if possible.
DateTime2 is a better data type than DateTime, and ISO 8601 is the universal standard and is supported throughout different platforms and languages as an unambiguous datetime format.

[Excel Destination [28]] Error: An error occurred while setting up a binding for the column. The binding status was "DT_NTEXT"

I'm working on ssis package which exports data from SQL Server to Excel. I had a problem converting non-unicode to unicode string data types. So I created a derived Column task and converted to Unicode string [DT_WSTR] 4 columns which have a type Varchar(40) in SQL Server table. It worked with these columns. But I also have a Description column of type varchar(max) and I tried to convert it to Unicode text stream [DT_NTEXT]. It did not work.
If your source is SQL Server (as you said), you can convert it directly in your SQL Query
SELECT
CONVERT(NVARCHAR(40), 'att1')
,CONVERT(NTEXT, 'att2')
Convert your VARCHAR into NVARCHAR
Convert your TEXT into NTEXT
it's faster.
P.S. To test it (Do not forget to delete or reset your previous OLE DB Input component) -> It will be forced to reevaluate your datatype
Does it help you?
The only thing that worked was to cast a Description column in Stored Procedure as varchar(1000). I checked the max length of this field and it was about 300 characters. So I made it varchar(1000) and in Derived column Unicode string [DT_WSTR]. This was a workaround, but I still want to know how to make it in ssis package without converting data type in Stored Procedure.

Should we explicitly convert datetime to date or let SQL Server handle it

I'm using SQL Server 2008. I have declared a variable as Date. My table has a Datetime column which I'm fetching. When I assign the datetime to date variable, I only get a Date. Everything works good. But are there any performance implications of assigning DateTime to a Date variable without Convert/Cast and letting SQL Server handle it? Or is it advisable to Convert/Cast before we assign?
Conversion from Datetime to Date is type of implicit conversion and it is defined in SQL server conversion so you do not need to be worry about conversion.
Obviously SQL Server would be using best bay of conversion for this so there is no need to add extra overhead of conversion. Because the conversion method we use has been defined for explicit conversion.

SQL Server 2005 Converting Data Type Error

I am trying to import varchar data into a varchar data type field but I am getting a conversion error of varchar cannot be converted to date type bit? How is that when both fields are already varchar type?
Conversion failed when converting the varchar value 'JOHN KUCHTA' to data type bit.
If you are importing from CSV, for example, you likely have a missing column in one row, or some of the data is incorrect. Is at least one of the fields you are trying to import of type bit?
I looks like you are inserting into the wrong column or you have mismatched the columns

Implicit conversion from varchar to Varbinary how to?

I got the following error trying to convert from a varchar to varbinary..
Implicit conversion from data type
varchar to varbinary(max) is not
allowed. Use the CONVERT function to
run this query.
and this is the alter command that I tried.
alter table foo Alter column bar varBINARY(max)
So how do I use the convert function for something like this? I did a bit of searching via google and had no luck.
Thanks.
You have to rebuild the table.
Use SSMS designer to change the type: this will generate a script with CONVERT.