Sybase IQ: Converting a string to NUMERIC without error/exception - numeric

I'm using Sybase IQ and need to convert a field from string to NUMERIC. The field sometimes has characters other than digits. In those cases I want it to return 0 instead of raising exceptions. How to do so?
You may look at the statements below to understand more:
SELECT CONVERT(NUMERIC(10, 0), '');
SELECT CONVERT(NUMERIC(10, 0), '1');
SELECT CONVERT(NUMERIC(10, 0), 'a');
SELECT CONVERT(NUMERIC(10, 0), 'a1');
SELECT CONVERT(NUMERIC(10, 0), '1a');
Only the first 2 lines will work. The remaining 3 lines will raise exceptions:
Cannot covert a to a NUMERIC(10, 0)(07006,-157)
Thanks in advance!

I am not sure if there is a better way but you could try this.
SELECT CASE PATINDEX('%[a-zA-Z]%','')
WHEN 0 THEN CONVERT(NUMERIC(10,0), '')
ELSE 0 END
SELECT CASE PATINDEX('%[a-zA-Z]%','1')
WHEN 0 THEN CONVERT(NUMERIC(10, 0), '1')
ELSE 0 END
SELECT CASE PATINDEX('%[a-zA-Z]%','a')
WHEN 0 THEN CONVERT(NUMERIC(10, 0), 'a')
ELSE 0 END
SELECT CASE PATINDEX('%[a-zA-Z]%','1a')
WHEN 0 THEN CONVERT(NUMERIC(10, 0), '1a')
ELSE 0 END
SELECT CASE PATINDEX('%[a-zA-Z]%','a1')
WHEN 0 THEN CONVERT(NUMERIC(10, 0), 'a1')
ELSE 0 END
Hope it helps :)

Related

How to convert the below SQL query to work in snowflake?

SELECT
LEFT(Replace([SERIAL_NBR],'"',''),34) AS [SERIAL_NBR]
,CONVERT(datetime, Replace([INSERTED_DTM],'"',''), 103) AS [INSERTED_DTM]
,LEFT(Replace([GROUP_NAME],'"',''),1024)
,LEFT(Replace([FIRST_NAME],'"',''),50)
,LEFT(Replace([LAST_NAME],'"',''),50)
,LEFT(Replace([REASON_CODE_ID],'"',''),30)
,CASE isnumeric(Replace([VALUE],'"',''))
when 1
then CAST(Replace([VALUE],'"','') AS float)
else null
END AS [VALUE]
,LEFT(Replace([AUTOLOAD_DELIVERY_STATE_ID],'"',''),15)
,LEFT(Replace([RIDER_CLASS],'"',''), 20)
,LEFT(Replace([ADJUSTMENT_NOTES],'"',''), 1024)
FROM X
This is what I changed it to
SELECT
LEFT(Replace(SERIAL_NBR,'"',''),34) AS SERIAL_NBR
,to_timestamp(Replace(INSERTED_DTM,'"',''), 'DD/MM/YYYY') AS INSERTED_DTM
,LEFT(Replace(GROUP_NAME,'"',''),1024)
,LEFT(Replace(FIRST_NAME,'"',''),50)
,LEFT(Replace(LAST_NAME,'"',''),50)
,LEFT(Replace(REASON_CODE_ID,'"',''),30)
,CASE
WHEN is_Double(Replace(VALUE,'"','')) = 1
THEN CAST(Replace(VALUE,'"','') AS NUMBER)
ELSE NULL
END AS VALUE
,LEFT(Replace(AUTOLOAD_DELIVERY_STATE_ID,'"',''),15)
,LEFT(Replace(RIDER_CLASS,'"',''), 20)
,LEFT(Replace(ADJUSTMENT_NOTES,'"',''), 1024)
FROM stage."TL_A2_Adjustment_Note"
WHERE Replace(SERIAL_NBR,'"','') != 'A2'
It's giving me the error
001044 (42P13): SQL compilation error: error line 9 at position 9
At WHEN is_Double(Replace(VALUE,'"','')) = 1
The case statement is saying "when it can be parsed as a double/numeric do so, else use NULL"
Thus is snowflake this section:
,CASE
WHEN is_Double(Replace(VALUE,'"','')) = 1
THEN CAST(Replace(VALUE,'"','') AS NUMBER)
ELSE NULL
END AS VALUE
can use TRY_TO_DOUBLE which if it fails to parse as a float, will return null.
thus:
,TRY_TO_DOUBLE(Replace(VALUE,'"','')) as value

sql decode case when

decode (case when sd='N' and sr_corp_support='0' then sr_corp_agree else null end, '1','0','0','1','') as sr_corp_agree,
hi I am having trouble understanding this code
it seems to me when some statement is 1, we get 0
&
when some statement is 0, we get 1
but I am not too sure how exactly
could you help to explain how to interpret the case when statement into is 0 or 1
thanks.
This:
decode(
case when sd='N' and sr_corp_support='0' then sr_corp_agree else null end,
'1','0',
'0','1',
''
) as sr_corp_agree
Can be rewritten as:
CASE
WHEN sd='N' AND sr_corp_support='0' AND sr_corp_agree = '1'
THEN '0'
WHEN sd='N' AND sr_corp_support='0' AND sr_corp_agree = '0'
THEN '1'
ELSE NULL -- Same as ''
END AS sr_corp_agree
In English: If the sd and sr_corp_support column values are N and 0, respectively, then if sr_corp_agree is 1 or 0 then output the opposite 0 or 1, respectively, and for any other sd, sr_corp_support or sr_corp_agree values output an empty string (which, in Oracle, is identical to NULL).

CASE Statement - Batch

Looking at making a basic scoring system based on questions answered.
The questions that are asked, all have 3 possible answers which are identical per question.
Currently I have:
SELECT [form].[Name],
[form].[TimeTag1],
(100 - [Form].[Field2] - [Form].[Field3] - [Form].[Field4] - [Form].[Field5]) AS [Total]
FROM
(SELECT
af.Name
,af.TimeTag1
,CASE WHEN [af].[aField46] = 'Checked Ok' THEN CONVERT(INT, '0')
WHEN [af].[aField46] = 'Rectified On-Site' THEN CONVERT(INT, '5')
WHEN [af].[aField46] = 'Failed' THEN CONVERT(INT, '5')
END AS [Field2]
,CASE WHEN [af].[aField2] = 'Checked Ok' THEN CONVERT(INT, '0')
WHEN [af].[aField2] = 'Rectified On-Site' THEN CONVERT(INT, '5')
WHEN [af].[aField2] = 'Failed' THEN CONVERT(INT, '5')
END AS [Field3]
,CASE WHEN [af].[aField4] = 'Checked Ok' THEN CONVERT(INT, '0')
WHEN [af].[aField4] = 'Rectified On-Site' THEN CONVERT(INT, '5')
WHEN [af].[aField4] = 'Failed' THEN CONVERT(INT, '5')
END AS [Field4]
,CASE WHEN [af].[aField5] = 'Checked Ok' THEN CONVERT(INT, '0')
WHEN [af].[aField5] = 'Rectified On-Site' THEN CONVERT(INT, '5')
WHEN [af].[aField5] = 'Failed' THEN CONVERT(INT, '5')
END AS [Field5]
FROM vAdvF_167 af) AS [Form]
This works but I'm looking for a more efficient way of completing this task as there will be over 100 potential questions which could be answered.
Is this the main way to achieve what I want or is there a quicker/more efficient way as this will be added to SSRS Reporting once done.
Edits:
The Table cannot be changed.
Those 3 answers are the only answers ever available to a user on all Q's
I'm not sure if it's going to perform better, but a translation table will make the query more readable, at the very least:
DECLARE #Translate AS TABLE
(
string varchar(17),
number int
)
INSERT INTO #Translate VALUES
('Checked Ok', 0),
('Rectified On-Site', 5),
('Failed', 5)
SELECT [form].[Name],
[form].[TimeTag1],
(100 - [Form].[Field2] - [Form].[Field3] - [Form].[Field4] - [Form].[Field5]) AS [Total]
FROM
(SELECT
af.Name
,af.TimeTag1
,f46.number AS [Field2]
,f2.number AS [Field3]
,f4.number AS [Field4]
,f5.number AS [Field5]
FROM vAdvF_167 af
JOIN #Translate f46 ON [af].[aField46] = f46.string
JOIN #Translate f2 ON [af].[aField2] = f2.string
JOIN #Translate f4 ON [af].[aField4] = f4.string
JOIN #Translate f5 ON [af].[aField5] = f5.string) AS [Form]
If you can't change table, one of the thing you can optimize (every) case in this way (e.g.) (It can help you to generate using information_schema):
,CASE WHEN [af].[aField46] = 'Checked Ok' THEN 0
WHEN [af].[aField46] IN('Rectified On-Site', 'Failed' THEN 5
END AS [Field2]

Display column name based on status 0 or 1

I don't understand how to achieve column name based on status, I think it's not possible to display multiple column names for the same but I have this requirement to display column name either 'Success' or 'Fail' and both status never be fetched, I have to select only one at a time either 0 or 1 based on where condition, value is defined in the variable.
CREATE TABLE #sqlJobHistory(jobId INT, jobStatus INT)
INSERT INTO #sqlJobHistory VALUES
(1, 0),
(2, 0),
(3, 1),
(4, 1),
(5, 1),
(6, 0),
(7, 1)
I want to display my second column name based on status, So far I tried the commented line but :(
SELECT MAX(jobId) maxJobId,
COUNT(*) AS 'Failure' --AS CASE WHEN jobStatus = 0 THEN 'Fail' ELSE 'Success' END
FROM #sqlJobHistory
WHERE jobStatus = 0
GROUP BY jobStatus
Can you please help he what's the proper way of achieving the desired output if it is possible, if not possible then extremely sorry for wasting your valuable time.
Yes, it can be possible through dynamic query, if I understand correctly, your second column name is 'Success' if Status = 1 else 'Fail'
DECLARE #status INT, #sql VARCHAR(500)
SET #status = 1 --It can be 0 or 1 and I think you already have this variable
SET #sql ='
SELECT MAX(jobId) maxJobId,
COUNT(*) AS ' +
CASE WHEN CAST(#status AS VARCHAR)= 0 THEN 'Failure' ELSE 'Success' END + '
FROM #sqlJobHistory
WHERE jobStatus = ''' + CAST(#status AS VARCHAR) + '''
GROUP BY jobStatus'
EXEC(#sql)

SQL Server 2008 IsNumeric returns 1

I am trying to do a query using ISNUMERIC but one of the my data record return it as a numeric.
Is there any way that the query return 0 on this data without removing character function?
Declare #Temp Table(Data VarChar(18))
Insert Into #Temp Values('750419.')
Insert Into #Temp Values('7.4')
Select Data,
IsNumeric(Data) As [IsNumeric],
From #Temp
and also I've found a function named but still wont return it to 0
CREATE Function [dbo].[IsInteger](#Value VarChar(18))
Returns Bit
As
Begin
Return IsNull(
(Select Case When CharIndex('.', #Value) >= 0
Then Case When Convert(int, ParseName(#Value, 1)) <> 0
Then 0
Else 1
End
Else 1
End
Where IsNumeric(#Value + 'e0') = 1), 0)
End
Try this :
Declare #Temp Table(Data VarChar(18))
Insert Into #Temp Values('750419.')
Insert Into #Temp Values('7.4')
Select Data,
CASE
WHEN ISNUMERIC(Data) = 0 THEN 0
WHEN Data LIKE '%[^-+ 0-9.]%' THEN 0
WHEN CAST(Data AS NUMERIC(38, 0))=cast(Data AS NUMERIC(38, 10)) then 1
ELSE 0
END
[IsNumeric]
From #Temp