Conversion of variable characters - sql

I use this statement within a query to get a blank result with a WHEN/THEN statement but keep getting the following error message:
" The conversion of a varchar data type to a datetime data type resulted in an out-of-range value." Below is the statement:
CASE
WHEN (A.APRDAT+'-'+A.APRBAT)= 01/01/1800
THEN ' '
ELSE
Convert(NVARchar(20),A.APRDAT)+'-'+Convert(NVARchar(20),A.APRBAT)
END AS AUTH_ID,
Could I get some assistance in getting this to work as design..thanks

Date literals need to be in single quotes, and you'll need to CONVERT the string result:
WHEN CONVERT(DATETIME,A.APRDAT+'-'+A.APRBAT) = '01/01/1800'
THEN ' '
ELSE
Convert(NVARchar(20),A.APRDAT)+'-'+Convert(NVARchar(20),A.APRBAT)

Related

CASE statement where conditional includes an IN statement redshift

CASE
WHEN code IN ('FJS354', 'JDF334')
THEN 'Lower_form'
ELSE 0
END AS format
This returns an error in Redshift
invalid input syntax for integer: "Lower_form"
I know if I change 'Lower_form' to an integer it will work however I want this column to be a string. Is there a way to do this?
I want this column to be a string.
All branches of a case expression must return the same datatype. You are giving two literal values whose datatype is not the same (string vs integer): the database makes the decision to turn them both to integers - which is not what you want.
Rremove the ambiguity by being explicit about the datatype you want to return. That is, make this literal 0 a string:
CASE WHEN code in ('FJS354','JDF334')
THEN 'Lower_form'
ELSE '0'
END as format

Data conversion error when using floor function in sql

please help me on this.
i am trying to check if .two decimal value not in a column using this.
FLOOR(LOG10(REVERSE(ABS(M.Description_12_Value)+1)))+1 <> 2
but i am getting error
Conversion failed when converting the varchar value '8.5' to data
type int.
Thanks
One of the columns you are comparing, either Description_2_Value/Description_3_Value/Description_10_Value is returning that 8.5 value and hence the query is throwing the error while comparing '8.5' > something. You need to cast the value RHS value in your condition to a varchar to fix it. See working example below:
if ('2.5' > '2')
begin
print 'success'
end

SQL Server : conversion error numeric to varchar case

I have this query:
CASE ClaimsFees.TBA
WHEN 0 THEN CAST(IndemnityReserve AS NUMERIC(9,2))
ELSE 'TBA'
END AS 'Reserve Indemnity'
but I always get this error:
Error converting data type varchar to numeric
I have tried to convert TBA as numeric but I can't do this, I also can't convert all the results to varchar because when I transfer to Excel file the number 1.325,27 becomes 132.527,00 with the ##,##0.00 format.
Is there a method in SQL Server that I can use to solve this?
The column can only have one type, and as 'TBA' can only be a string, you'll need to make the numeric a string too.
CASE ClaimsFees.TBA WHEN 0
then cast(cast(IndemnityReserve as NUMERIC(9,2)) as varchar(12))
else 'TBA'
end as 'Reserve Indemnity',

Error: "Argument data type float is invalid for argument 1 of parse function." when doing try_parse() in SQL

What is wrong with this sql statement:
select TRY_PARSE(
([RegionA] +
[RegionB] +
[RegionC] +
[RegionZ] +
[Top1] +
[Top2]) as float using 'nl-BE') Total
FROM [dbo].[Regions]
I keep getting the error:
Argument data type float is invalid for argument 1 of parse function.
The columns have float data type with 'en-US' culture. I want the result to be in 'nl-BE' culture.
TRY_PARSE works on text data; it looks like all of your columns are numeric, so it is performing addition, not string concatenation. If they are numeric: you do not need to parse. If they are intended to be text, you may need to convert them individually - but again, I suspect parsing is wrong in this case.

SQL Server, DATEDIFF error, formatting of date fields?

I have an error on my datediff statement. Comment out the datediff & the 'Nothing clicked' bit works fine. I think it's related to the format of the date variables.
error message..
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value 'Nothing clicked' to data type int.
select
case
when stVs.DateLastAction is null then 'Nothing clicked'
else DATEDIFF(MI,StVs.DateSessionStarted,stVs.DateLastAction)
end as test
From Stats_VisitorSessions StVs
try:
select
case
when stVs.DateLastAction is null then 'Nothing clicked'
else CONVERT(varchar(30),DATEDIFF(MI,StVs.DateSessionStarted,stVs.DateLastAction))
-- ^^^^^^^^^^^^^^^^^^^^ ^
end as test
From Stats_VisitorSessions StVs
TSQL is having a hard time resolving if the test result set column is numeric or string.
A CASE expression can only return one datatype. It cannot return both a numeric and string data type. In the OP's case, it is deciding on a numeric data type, and then it processes more rows, and finds that it needs to be a string, and it fails. See Data type precedence.
The problem here are different datatypes in the CASE expression. Add a CAST and everything should work:
select
case
when stVs.DateLastAction is null then 'Nothing clicked'
else CAST(DATEDIFF(MI,StVs.DateSessionStarted,stVs.DateLastAction) AS varchar)
end as test
From Stats_VisitorSessions StVs
Another option would be to return NULL instead of the message and let the application handle that case.