SQL Server 2005 Converting Data Type Error - sql-server-2005

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

Related

Column Datatypes Issue in sql server 2019 when Import Flatfile using SSIS

I have column in flatfile contain value like. 2021-12-15T02:40:39+01:00
When I tried to Insert to table whose column datatype is datetime2.
It throwing Error as :
The data conversion for column "Mycol" returned status value 2 and status text
"The value could not be converted because of a potential loss of data.".
What could be best datatype for such values.
It seems the problem is two-fold here. One, the destination column for your value should be a datetimeoffset(0) and two that SSIS doesn't support the format yyyy-MM-ddThh:mm:ss for a DT_DBTIMESTAMPOFFSET; the T causes it problems.
Therefore I suggest that you define the column, MyCol, in your Flat File Connection as a DT_STR. Them, in your data flow task, use a derived column transformation which replaces MyCol and uses the following expression to remove the T and with a space ( ):
(DT_DBTIMESTAMPOFFSET,0) (REPLACE(Mycol,"T"," "))
This will then cause the correct data type and value to be inserted into the database.

Explicitly convert datetime to varbinary

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.

[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.

Importing from Excel to SQL Server: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

I have the following Excel Workbook:
https://meocloud.pt/link/7cb3ef48-7556-4d36-ab9a-c247d3f7b29d/FichasTeste.xls/
It's formatted to be imported to a SQL Database. The server has the following details:
As you can see the mapping is correct in the importer
When i try to import the excel file, the status is always the same, no matter the modifications i do to the columns with the Date.
242: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
3621: The statement has been terminated.
What should i modify so that those rows can be imported without having that error?
Excel stores dates as integers, if you change the excel field to TEXT you'll see that, but I'm not sure why you're having this problem, you should be able to direclty import from Excel and have dates preserved, at least you can using SQL's import wizard as well as SSIS.
One workaround is using the TEXT() function in Excel to get your date into an acceptable format:
=TEXT(F2,"yyyy-mm-dd")
I like importing from Excel using OPENROWSET(), but that requires some additional setup and has it's own drawbacks:
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\YourExcelFile.xls', [Sheet1$])

IBM Netezza, How to convert a varchar to a numeric with TO_NUMBER(..., ...)

I have table called DART_STG1 in Netezza Database. The table has a varchar column. I am trying to use the below SQL to convert the varchar into a number, but it always throws an error.
Code
SELECT DISTINCT TO_NUMBER(M12,'99G99')
FROM DART_STG1
WHERE M12 IS NOT NULL;
Throws an error:
ERROR [HY000] ERROR: Bad numeric input format
What does this error mean?
The PDA (Netezza) Conversion functions page provide examples, and together with the Template patterns, you can choose the appropriate format.
However I'm getting the same error for my data set. I'm suspecting it's something about the formatting of the values in STORE_NUMBER is what Netezza doesn't like.
The error:
ERROR [HY000] ERROR: Bad numeric input format
Is caused because you feeding letters into TO_NUMBER. You are feeding it 99G99 which is not a number.
The program tries to tell you it's not a number by telling you that the numeric input format is bad, as the error very clearly says.