Sql Server IN Clause - sql-server

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')

Related

Snowflake Unsupported subquery type cannot be evaluated with UDF

Having an issue in snowflake using a UDF to select data.
My UDF:
create or replace function find_nearest_postcode(plat float, plong float)
returns varchar
language SQL
as
$$
select any_value(a.postcode) from
(select sqrt(square(c.latitude - plat)+square(c.longitude - plong)) as distance, c.postcode from postcodes c order by distance asc limit 1) as a
$$
;
This works if I call the following:
select find_nearest_postcode(51.2345::float,-0.876::float);
However, when I try to get to work on the whole table:
select *, find_nearest_postcode(latitude::float, longitude::float) from table1;
I get the dreaded "Unsupported subquery type cannot be evaluated"
Postcode table contains fields postcode, lat, long.
table1 contains just lat and long
Any ideas?

Snowflake Data Type Conversion

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();

Conversion failed when converting the vardhar value 'abc' to data type int

I am inserting data from one table to another so when inserting I got above error mentioned in title
Insert into dbo.source(
title
)
Select
Title from dbi.destination
title in dbo.source table is of INT data type and title in dbo.destination table is of Varchar data type and I have data like abc, efg, etc. in the dbo.destination table.
So how to solve this now or is it possible to convert and insert values?
You can use SQL Server try_cast() function as shown below. Here is the official documentation of TRY_CAST (Transact-SQL).
It Returns a value cast to the specified data type if the cast succeeds; otherwise, returns null.
Syntax
TRY_CAST ( expression AS data_type [ ( length ) ] )
And the implementation in your query.
INSERT INTO dbo.source (title)
SELECT try_cast(Title AS INT)
FROM dbi.destination
Using this solution you need to be sure you have set the column allow null true otherwise it will give error.
If you do not want to set the allow null then you need minor changes in select query as shown below - passing the addition criteria to avoid null values.
Select ... from ... where try_cast(Title AS INT) is not null
You must use isnumeric method of SQL for checking is data numeric or not
CONVERT(INT,
CASE
WHEN IsNumeric(CONVERT(VARCHAR(12), a.value)) = 1 THEN CONVERT(VARCHAR(12),a.value)
ELSE 0 END)
Think about your data types - obviously you cannot have a text string like 'abc' in a column that is defined to hold integers.
It makes no sense to copy a string value into an integer column, so you have to confirm how you want to handle these - do you simply discard them (what is the impact of throwing data away?) or do you replace them with some other value?
If you want to ignore them and use NULL in place then use:
INSERT dbo.Source (Title)
SELECT CASE
WHEN ISNUMERIC(Title) = 1 THEN CAST(Title as INT)
ELSE NULL
END
FROM dbo.Destination
If you want to replace the value then simply change NULL above to the value you want e.g. 0
You can use regex to root out non numeric characters
Insert into dbo.source(
title
)
Select
case when Title not like '%[^0-9]%' then null else cast(Title as int) end as Title
from dbi.destination
Just filter only numeric field from destination table like as below:
Insert into dbo.source(
title
)
Select
Title from dbi.destination
where ISNUMERIC(Title) = 1

Conversion failed when converting the varchar value 'SAT' to data type int

I have a table ConsoleGames wherein all columns are of type varchar(50). When I try to create a new table console_games by amending existing datatypes by using the query:
CREATE TABLE console_games
(
game_rank integer,
game_name varchar(1200),
platform_name varchar(1200),
game_year integer,
genre varchar(200),
publisher varchar(1200),
na_sales float,
eu_sales float,
jp_sales float,
other_sales float
)
INSERT INTO console_games
SELECT *
FROM [dbo].[RAWConsoleGames]
I get the following error message:
Msg 245, Level 16, State 1, Line 17
Conversion failed when converting the varchar value 'SAT' to data type int.
When I look into the data in the table the value 'SAT' is in a column for which I am not changing the datatype. 'SAT' value exists in the Platform column which is of varchar type and I am not trying to change the type to int.
Any help will be appreciated.
Thanks
Clearly 'SAT' is not and will never convert to an INT.
Always best to specify the columns to insert ... things change
Now, if the source data is suspect, add a try_convert(). If the conversion fails, a null value will be returned
I don't know the column names of your source, so I substituted SomeColN
INSERT INTO console_games
SELECT try_convert(integer ,SomeCol1)
,try_convert(varchar(1200),SomeCol2)
,try_convert(varchar(1200),SomeCol3)
,try_convert(integer ,SomeCol4)
,try_convert(varchar(200) ,SomeCol5)
,try_convert(varchar(1200),SomeCol6)
,try_convert(float ,SomeCol7)
,try_convert(float ,SomeCol8)
,try_convert(float ,SomeCol9)
,try_convert(float ,SomeCol10)
FROM [dbo].[RAWConsoleGames]
Just for fun, try:
Select try_convert(int,'SAT')
Select try_convert(int,'25.25')
Select try_convert(int,'25')
You should always define the list of columns you're inserting into, and you should also always define the list of columns you're selecting from. Furthermore, I'd recommend to explicitly do any type conversions instead of leaving that up to SQL Server - if you do it yourself, you know when and what you're doing.
So I'd write that statement like this:
-- **DEFINE** the list of columns you're inserting into
INSERT INTO console_games (rank, name, Platform, year, genre, publisher,
sales, eu_sales, jp_sales, other_sales)
-- **DEFINE** the list of columns you're selecting, and any conversions
SELECT
game_rank, game_name, platform_name,
CAST(game_year AS VARCHAR(50)), genre,
publisher,
CAST(na_sales AS VARCHAR(50)),
CAST(eu_sales AS VARCHAR(50)),
CAST(jp_sales AS VARCHAR(50)),
CAST(other_sales AS VARCHAR(50))
FROM
[dbo].[RAWConsoleGames]

Displaying NULL values as 'unknown' in SQL Server

I have a table that has two columns; one for Name (datatype nvarchar), the other for ID (datatype int and allows null).
I am trying to display all data from the table including those with null values but I want the query result to display the null value as 'unknown'.
I ran the following query:
Select Name, ID
Case
When ID is null then 'unknown'
When id is not null then (ID)
End
From table
The problem is I am getting this message:
Conversion failed when converting the varchar value 'unknown' to data type int
I guess you could cast your integers to a varchar
You can also use COALESCE instead of case when dealing with nulls.
Select Name, COALESCE(CAST(ID AS VARCHAR(10)),'unknown') AS ID
From table
You can use COALESCE function
Select Name, coalesce(convert(varchar(20),ID), 'unknown')
From table

Resources