Sql cast and try_cast issue for integer / currency with unknown behavior in sql server - sql-server

I have two queries as below
select TRY_CAST('12424.52‬' as money)
select TRY_CAST('12424.52' as money)
the first one returns null and second one returns correct value. How to resolve it? I believe some ascii character in the first string is causing issue. how to detect this?

Since this can help someone, i'm posting the workaround as a the answer
I was able to fix the issue with invisible pop directional formatting by converting the string to varchar and removing the unknown unicode chars (displayed as ? ) as below
select TRY_CAST(replace(convert(varchar(4000), '12424.52‬'), '?', '') as money)
As jeroen mentioned, this will work only if the unicode which is present in the string is unknown for your collation

Related

Conversion failed when converting from a character string to uniqueidentifier in SQL SERVER select statement

When I execute the query below, I get the error
Conversion failed when converting from a character string to uniqueidentifier.
SELECT * FROM TBL_IncidentDetails where IncidentId = '1822CBE-4616-4ABE-B562-D3CC925D68ED'
Below is my column data
Appreciate assistance
Error is pretty clear, that value isn't a uniqueidentifier.
If you check against a proper GUID (i.e. SELECT NEWID() -> 919053E6-7CE6-4324-9A58-A2EACA5E0F5F) notice that the first "block" has 8 characters (919053E6). Yours, however, has 7 characters (1822CBE).
On consideration, this is probably simply a typographical error.

Troubleshoot conversion failed when converting date and /or time from character string

I know this has been addressed 1000 times but something screwy is going on and I need help with ideas to troubleshoot.
Using MS SQL Server 2012
I have a date stored as INT in the YYYYMMDD format.
I need to turn that into a date and I always use this:
CONVERT(DATE,CONVERT(VARCHAR,YYYYMMDD),101)
*I know not specifying the length of the varchar is bad form but where I'm doing this has never caused an issue and just to be thorough I tried it anyway to no avail.
That conversion always works. Always, until today.
Today, this conversion doesn't work and I get the above mentioned error in title.
One thing I've done is run part of the query to look at the values to make sure I don't find something dumb like this in my values:
20170102
20170304
-->2017ABCD
20170704
What else can I do?
To expand on my initial comment
Example
Declare #YourTable table (SomeCol varchar(25))
Insert Into #YourTable values
('20170102'),
('20170304'),
('2017ABCD'),
('20170704')
Select SomeCol
,AsDate = try_convert(date,SomeCol)
From #YourTable
Returns
SomeCol AsDate
20170102 2017-01-02
20170304 2017-03-04
2017ABCD NULL
20170704 2017-07-04

How to copy varchar column with currency value to decimal in SQL Server?

I have a table with around 20K rows of data. One varchar column contains currency info in the format $xxx,xxx,xxx.00. I'd like to create an additional column that is of type decimal and copy the varchar data into it, obviously while stripping off the $ and ,'s.
I've played with cast and convert, mixed with trim and replace functions, but haven't been successful yet in getting this into a new column.
Because we're doing many future calculations on the data, we want it converted once instead of having to convert it for each calculation.
Any suggestions would be greatly appreciated!
Thank you in advance!
--Kent
Try replacing the $ and the ',' and then Convert. You should be Good
CONVERT(NUMERIC(28,4),REPLACE(REPLACE(stringColumn,'$',''),',',''))
SQL Fiddle here.
Select Cast(Replace(Replace(ColName, '$', ''), ',', '') as Money)

Conversion failed when converting the nvarchar value 'quiz.jpg' to data type int

I am getting this error in my logs and I really do not know how to solve it.
"Conversion failed when converting the nvarchar value 'quiz.jpg' to data type int."
I am currently using ColdFusion 9. It seems that for whatever reason the site is trying to parse an image file into the database or something like that.
Any help will be appreciated.
Thanks
Are you using <cfqueryparam>?
I would guess that you may have something like this:
<cfquery param value="#myVar#" cfsqltype="cf_sqltype_integer" />
the cfsqltype should be cf_sqltype_varchar.
If that is not the case, then please update your post with the offending code. That would be very helpful.
I think we would need more of the error details like the line of code on which the error occurs or maybe the whole template to be much help. In the absence of more information, Jason's answer seems likely.
This sounds to me like you may have a query where you are trying to concatenate a nvarchar column with an int column.
Such as
SELECT MyString + MyInt FROM MyTable
Unfortunatly SQL Server does not implicitly convert integers to varchar for concatenation, so you will need to convert your integer.
SELECT MyString + Convert(varchar(20), MyInt) FROM MyTable
or
SELECT MyString + Cast(MyInt AS varchar(20)) FROM MyTable

SQL Server: converting UniqueIdentifier to string in a case statement

We have a log table that has a message column that sometimes has an exception stack trace. I have some criteria that determines if the message has this. We do not want to show these messages to the customer but instead have a message like:
Internal Error Occured. Contact US
with reference code
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
where xxx etc is a guid column in the table. I am writing stored proc like this:
declare #exceptionCriteria nvarchar(50)
select #exceptionCriteria = '%<enter criteria etc>%'
select LogDate,
case
when Message like #exceptionCriteria
then 'Internal Error Occured. Reference Code: ' + str(RequestID)
else Message
end
from UpdateQueue
RequestID is a Guid datatype in SQL Server and does not convert to string here. I've seen some code on how to convert a Guid to string, but it is multi-lined and I don't think it would work in a case statement. Any ideas?
I think I found the answer:
convert(nvarchar(36), RequestID)
Here's the link where I found this info:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
It is possible to use the convert function here, but 36 characters are enough to hold the unique identifier value:
convert(nvarchar(36), requestID) as requestID
Edit: yes, as noted in the comments, char, or nchar, or any function that can properly manipulate ASCII character tables would do the trick. Then, my excuse is that I usually work in a multilingual/multialphabet environment, and the rule is to go for nvarchar, always. That's my no-brainer way of doing things, sorry. And, if one of these days, some database software starts to generate unique identifier with non-ASCII elements, I will be ready.
In my opinion, uniqueidentifier / GUID is neither a varchar nor an nvarchar but a char(36). Therefore I use:
CAST(xyz AS char(36))
Instead of Str(RequestID), try convert(varchar(38), RequestID)

Resources