Conversion failed when converting from character string to uniqueidentifier - sql-server

I have a query which selects a table based on the provided uniqueidentifier Id so when i provides Id to a value it returns the result but if i am passing it like below i am getting an error message. Please suggest how to use Convert or Cast in this case to resolve this issue.
Thanks
SELECT * FROM TBL WHERE ID=''

Assuming #id is a string parameter, and that the ID column is not nullable, you could do something silly like this:
SELECT * FROM TBL WHERE ID = COALESCE(NULLIF(#id, ''), ID);
But really you should be passing NULL if there is no value, not an empty string. This shouldn't be a string parameter in the first place.

According to the comments on this answer, the solution should be something like this (in pseudo-code):
if textbox1.Text.ToString().Equals('') {
SELECT * FROM TBL
}
else {
SELECT * FROM TBL WHERE ID='"+textbox1.text+"'
}
You may do the IF in the C# application.

Related

Date format is not recognized Snowflake

view in snowflake as follows;
CREATE OR REPLACE VIEW A AS(
SELECT tab1.colA,
tab1.colB,
CASE WHEN COALESCE(DATE_ACT_CREATED,'missing')='missing' THEN DATE ('1970-01- 01') ELSE TO_DATE(DATE_ACT_CREATED) END AS ACT_CREATED_DATE
FROM tab1);
Please note that because of some reasons, column DATE_ACT_CREATED was defined as varchar earlier. This view is created fine. But when I try to retrieve records from it;
SELECT * FROM viewA;
I get following error,
Date '' is not recognized
But when I take out the entry; CASE WHEN COALESCE(DATE_ACT_CREATED,'missing')='missing' THEN DATE ('1970-01- 01') ELSE TO_DATE(DATE_ACT_CREATED) END AS ACT_CREATED_DATE the error is gone.
May I know how can I handle this date error issue? Found this link enter link description here, but it couldn't help much.
Help is appreciated.
You get the error because the case when DATE_ACT_CREATED is an empty string is not handled in your CASE condition. So the TO_DATE function couldn't work.
You should try TRY_TO_DATE. If your entry couldn't be casted as a date then it returns null that will be handled with your COALESCE.
CREATE OR REPLACE viewA AS(
SELECT
tab1.colA,
tab1.colB,
COALESCE(TRY_TO_DATE(DATE_ACT_CREATED), DATE('1970-01- 01')) AS ACT_CREATED_DATE
FROM tab1);
Using the following query you will not get the error if the input is ''(Empty string), actual date, NULL keyword, or missing keyword.
CREATE OR REPLACE VIEW A
AS
SELECT COL1, COL2,
CASE WHEN COALESCE(DATE_AT_CREATED, '') = '' THEN DATE('1970-01-02')
ELSE CASE WHEN DATE_AT_CREATED = 'missing' THEN DATE('1970-01-02') ELSE TO_DATE(DATE_AT_CREATED) END
END AS ACT_CREATED_DATE
FROM TAB1;
SELECT * FROM A;

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

I need to search the ffg table using the parameter ID(int) to the column Id(int), if ID is not supplied, I would like to return all data from that table. I have tried this query below:
SELECT *
FROM TableName
WHERE Id LIKE ISNULL (#id, '%')
But this query returns the error:
Conversion failed when converting the varchar value '%' to data type
int.
You are comparing an integer with "%"and you also using LIKE which both are wrong.
You need to use this
SELECT *
FROM TableName
WHERE (Id = #id) or (#id is null)
This is what you have to do inside ISNULL:
SELECT *
FROM TableName
WHERE ID = ISNULL(#id, ID))
Here, if #id is null, the ID (the column value itself) is passed for comparison, which will always match and results in returning all data in the table.
This is because of the ISNULL rule:
replacement_value must be of a type that is implicitly convertible to the type of check_expresssion.
In your condition:
ISNULL(#id, '%')
When #id is NULL, SQL Server tries to convert '%' to INT which causes the conversion error.
To return all rows if #id is NULL, you can do this:
SELECT *
FROM TableName
WHERE
(Id = #id OR #id IS NULL)
you need to caste Id because it is integer so do like:
Where CAST(Id AS TEXT) LIKE '123%')

How to convert string to int using query (sqlserver)

I am trying to update several rows on my table and I am having a problem when it comes to converting, here's my query:
update Table1
set [Printed Notices] = '1,677'
where [Date ID] = '2013-05-20'
and so far, here's my error:
Conversion failed when converting the varchar value '1,677' to data type int.
Please help :( thank you!
You are casting the int to a string by using quotes around it. At best you will be relying on some kind of silent type conversion by the database engine, which you will not get. So just remove the quotes and it will work.
update Table1
set [Printed Notices] = 1677
where [Date ID] = '2013-05-20'
You have to get the commas out of your numeric text strings before you try to use them as integers. This t-sql command does that.
CAST(REPLACE('1,667', ',', '') AS INT)
So, you can do this....
... set [col] = CAST(REPLACE('1,667', ',', '') AS INT) ...

SQL Server Conversion error: Conversion failed when converting the nvarchar value 'XXX' to data type int

Running some pretty simple SQL here:
select *
from table
where columnA <> convert(int,columnB)
and isnumeric(columnB) = 1
Still getting this error every time:
Conversion failed when converting the nvarchar value 'XXX' to data type int.
If you're using SQL Server 2012 or more recent you could use TRY_PARSE which will return NULL when the parse fails.
SELECT TRY_PARSE('one' as int) -- NULL
, TRY_PARSE('1' as int) -- 1
, TRY_PARSE('0.1' as int) -- NULL
Returns the result of an expression, translated to the requested data type, or null if the cast fails in SQL Server. Use TRY_PARSE only for converting from string to date/time and number types.
Isnumeric has a lot of odd behavior. For example, it also considers currency signs such as $ or £, and even a hyphen (-) to be numeric.
I think you'd be better of using NOT columnB like '%[^0-9]%' to ONLY take numbers into account.
Check the comments at the bottom of the msdn page for isnumeric(), which you can find here: https://msdn.microsoft.com/en-us/library/ms186272.aspx
This may sound weird, but it breaks when do not put the ISNUMERIC check first. Try this out:
WITH [Table]
AS
(
SELECT columnA,columnB
FROM
(
VALUES (1,'2'),
(2,'XXX')
) A(columnA,columnB)
)
select *
from [Table]
where ISNUMERIC(columnB) = 1 --this works
AND columnA <> convert(int,columnB)
--where columnA <> convert(int,columnB) --this doesn't work
-- and isnumeric(columnB) = 1
I suggest you to reverse your checking like this:
SELECT *
FROM table
WHERE CONVERT(NVARCHAR, columnA) <> columnB
I got this using a combination of the answers and comments here. I used a CASE statement in my WHERE clause and also had to use LIKE instead of ISNUMERIC to account for illegal characters. I also had to use BIGINT because a few select samples were overflowing the INT column. Thanks for all of the suggestions everybody!
select * from patient
where PatientExternalID <>
(case when mrn not like '%[^0-9]%'
then convert(bigint, mrn)
else 0
end)

Resources