I am getting a conversion error as explained below:
Created an insert into a Table using SHA1. The filed is binary
When I try to query the table with this field, I get an error shown below.
Do I need to convert binary datatype to Varhcar for querying?
Cannot convert parameter ''D832F2E3518C72414C9FAC5131951F3DADCBG51A'' of type [VARCHAR(40)] into expected type [BINARY(8388608)]
insert into test
select SHA1(concat(EMPNAME,'|',ETYPE)) Field1
from "TABLE1
select * from Table1 where Field1 ='D832F2E3518C72414C9FAC5131951F3DADCBG51A'
Do I need to convert binary datatype to Varhcar for querying?
Rather string literal to binary:
SELECT * FROM Table1 WHERE Field1 ='D832F2E3518C72414C9FAC5131951F3DADCBG51A'::BINARY
An open question is why Field1 column is BINARY when SHA1 returns VARCHAR.
SHA1
The data type of the output is string (VARCHAR) and can be stored in a VARCHAR column:
CREATE OR REPLACE TABLE table1(Field1 VARCHAR)
AS
SELECT SHA1(concat(EMPNAME,'|',ETYPE)) Field1
FROM (SELECT 'a' AS EMPNAME, 1 AS ETYPE) s;
SELECT * FROM Table1 WHERE Field1 ='469f9400148c19c663c1aa53ca53cbf9f2526ea6';
My question is why SHA1 returns Binary?
It returns VARCHAR(40) as stated in documentation. But during insert there is implicit conversion to Binary as Field1 is defined as BINARY.
select SHA1(concat(EMPNAME,'|',ETYPE)) Field1
from (SELECT 'a' AS EMPNAME, 1 AS ETYPE) s;
DESCRIBE RESULT LAST_QUERY_ID();
Related
I'm trying to create a simple function in Snowflake under my schema. All it does is query a table.
Something to the effect of:
CREATE FUNCTION MYNEWFUNCTION (DATE_KEY_INPUT INT)
RETURNS TABLE (COLUMN1 date, COLUMN2 int, COLUMN3 varchar(255) )
as '
SELECT
COLUMN1
,COLUMN2
,COLUMN3
FROM "TABLE"."DBO"."TABLENAME"
where column1 = date_key_input
';
But I'm getting the following error:
Declared return type 'VARCHAR(255)' for column 'COLUMN3' is incompatible with actual return type 'VARCHAR(255)'
I'm lost... its not compatible with the actual data type? What am I missing here...?
The data type from the original table is varchar(255), in the function I've set it to varchar(255) - why is it rejecting?
Also I should note the source is a view not a table, so concretely, I'm trying to create a function which creates a table from a view...
Collation Limitations in Snowflake:
At present Collation and UDFs (user-defined functions) do not always work together. For example, you cannot return a collated string value from a UDF; the server complains that the actual return type is incompatible with the declared return type. You also cannot pass a collated string value to a UDF.
Reference :https://docs.snowflake.com/en/sql-reference/collation.html
There were a couple things going on in your example:
the first issue was the returns clause defining the length of the varchar, you won't need the length
the second was the input parameter/argument not having the right datatype, or
the query was not constructed correctly (col1 was date, col2 was int).
Working example below.
CREATE TABLE my_table (col1 date, col2 integer, col3 varchar(255));
INSERT INTO my_table VALUES (current_date(), 1, 'Hello World'), (current_date(), 2, 'Goodbye');
CREATE OR REPLACE FUNCTION MYNEWFUNCTION (x integer)
RETURNS TABLE (col1 date, col2 integer, col3 varchar)
as
$$
SELECT col1, col2, col3
FROM my_table
where col2 = x
$$
;
SELECT * from TABLE(MYNEWFUNCTION(1));
How to less numeric value from data type varchar.
For example, I have table which contains value like '2019-02-15', '2019-02-20 10:00:15.000', '3476', '3478956', and I need to less -1 from all of them. How can i do that.
Below is sample query
CREATE TABLE dbo.TestTable (SampleData VARCHAR(100))
INSERT INTO dbo.TestTable ([SampleData])
VALUES ('2019-02-15')
,('2019-02-20 10:00:15.000')
,('3476')
,('3478956')
SELECT * FROM dbo.TestTable
If 2012+, you can use try_convert() within a case statement
Example
SELECT *
,RequiredData = case when try_convert(int ,SampleData) is not null then convert(varchar(100),try_convert(int,SampleData)-1)
when try_convert(date,SampleData) is not null then convert(varchar(100),dateadd(DAY,-1,try_convert(date,SampleData)) )
end
FROM dbo.TestTable
Returns
SampleData RequiredData
2019-02-15 2019-02-14
2019-02-20 10:00:15.000 2019-02-19
3476 3475
3478956 3478955
I am trying to create view by filtering some table, and include some converted to different type column into select list. View filter excludes from result set rows in which this column can not be converted to that type. Then I select rows from this view and filter rows using this converted column. And I always get error Conversion failed when converting the nvarchar value '2aaa' to data type int
SQL Fiddle
MS SQL Server 2008 Schema Setup:
create table _tmp_aaa (id int identity(1, 1), value nvarchar(max) not null)
go
insert _tmp_aaa (value) values ('1111'), ('11'), ('2aaa')
go
create view _tmp_v_aaa
as
select id, cast(value as int) as value from _tmp_aaa where value like '1%'
go
Query 1:
select * from _tmp_v_aaa where value = 11
Are there any workarounds?
Add to your view ISNUMERIC to check if string is numeric value:
CREATE VIEW _tmp_v_aaa
AS
SELECT
id,
[value] = CAST((CASE WHEN ISNUMERIC([value]) = 1 THEN [value] ELSE NULL END) AS INT)
FROM _tmp_aaa
WHERE [value] LIKE '1%'
AND ISNUMERIC([value]) = 1
I tried some tricks... Obviously the optimizer tries to hand down your where criterium where it is not yet tranformed. This is one problem to be solved with a. multi-statement function. Their biggest disadvantage is the advantage in this case: the optimizer will not look into it, but just take their result "as is":
create function fn_tmp_v_aaa()
returns #tbl table(id INT, value INT)
as
BEGIN
INSERT INTO #tbl
select id, cast(value as int) as value from _tmp_aaa where value like '1%'
RETURN;
END
select * from dbo.fn_tmp_v_aaa() where value=11;
If you look at the execution plan , predicates are passed down to the table something like....
And your query gets translated to something like .....
select id, cast(value as int) as value
from tmp_aaa
where CONVERT(INT, value,0) like '1%'
AND CONVERT(INT, value,0) = CONVERT(INT, 11,0)
Now if you run this query you will get the same error you get when you query against the view.
Conversion failed when converting the nvarchar value '2aaa' to data type int.
When the predicate CONVERT(INT, value,0) like '1%' is converted , you have INT on one side of the expressions and varchar on another, INT being the higher precedence, sql server tries to convert whole expression to INT and fails hence the error message.
I have data stored in a nvarchar(10) column that I need to convert to decimal(10,3). Each record is a full 10 characters with leading zeros. The last three numbers represent decimals. How do I convert the following example?
col1
----------
0000100001
0002507630
0090078607
0258736000
Expected Output
col1 col2
-------------------------------
0000100001 100.001
0002507630 2507.630
0090078607 90078.607
0258736000 258736.000
I tried using
Cast(covert(decimal(10, 3), col1) as Decimal(10, 3)) as col2
The output I receive is
Arithmetic overflow error converting nvarchar to data type numeric.
How do do properly convert this data?
DECLARE #t AS TABLE (a NVARCHAR(10));
INSERT INTO #t VALUES ('0000100001'),('0002507630'),('0090078607'),('0258736000');
SELECT a,CAST(CAST(a AS INT)/1000.000 AS DECIMAL(10,3))
FROM #t;
Try this:
SELECT CAST (LEFT(col1, 7) + '.' + RIGHT(col1,3) AS DECIMAL(10,3))
SQL Fiddle demo
How to remove single Quote from given select Query. It is give me Conversion failed when converting the varchar value '1,2,3' to data type int.
SELECT * FROM Member
WHERe MemberId IN ('1,2,3')
You don't have to use single quotes for integer field:
SELECT * FROM Member WHERE MemberId IN (1,2,3)
You should use quotes for varchar fields, like:
SELECT * FROM Member WHERE Name IN ('John','George','Amanda')