I'm trying to set an HTML format if the substring is within the string using the INSTR function and it worked fine in this case.
CASE WHEN INSTR({custcol_customer_price_group},{custcol_pricing_group}) >0
AND {custcol_line_number} is not null
THEN '<font size="2" color="Green"><b></b></font>'
WHEN {custcol_line_number} is null
THEN ''
ELSE '<font size="2" color="Red"><b>Pricing Group Not Defined</b></font>' END
but now I'm trying to set the checkbox marked if the substring is not within the string and am using the function but it doesn't work
INSTR({custcol_customer_price_group},{custcol_pricing_group}) !=0
also I tried this function
INSTR({custcol_customer_price_group},{custcol_pricing_group}) <>0
Unless I misunderstood what you are saying, you should check whether INSTR result is equal to 0 - it means that the searched string doesn't exist in the source.
In other words: you said
if the substring is within the string using the INSTR function and it worked fine in this case.
and used
INSTR({custcol_customer_price_group},{custcol_pricing_group}) > 0
Now, you're saying:
if the substring not with the string
and used
INSTR({custcol_customer_price_group},{custcol_pricing_group}) != 0
--
Wrong! Should be = 0
Related
I've got a query pulling data from a table. In one particular field, there are several cases where it is a zero, but I need the four digit location number. Here is where I'm running into a problem. I've got
SELECT REPLACE(locationNbr, '0', '1035') AS LOCATION...
Two issues -
Whoever put the table together made all fields VARCHAR, hence the single quotes.
In the cases where there already is the number 1035, I get 1103535 as the location number because it's replacing the zero in the middle of 1035.
How do I select the locationNbr field and leave it alone if it's anything other than zero (as a VARCHAR), but if it is zero, change it to 1035? Is there a way to somehow use TO_NUMBER within the REPLACE?
SELECT CASE WHEN locationNbr='0' THEN '1035' ELSE locationNbr END AS LOCATION...
REPLACE( string, string_to_replace , replacement_string )
REPLACE looks for a string_to_replace inside a string and replaces it with a replacent_string. That is why you get the undesired behaviour - you are using the wrong function.
CASE WHEN condition THEN result1 ELSE result2 END
CASE checks a condition and if it is true it returns result1 and if it is not it will return result2. This is a simple example, you can write a case statement with more than one condition check.
Don't use replace(). Use case:
(case when locationNbr = '0' then '1035' else locationNbr end)
You can make use of length in Oracle:
select case when length(loacation) = 1 then REPLACE(loacation, '0', '1035') else loacation end as location
from location_test;
What I am attempting to do is separate my data by the line breaks into separate fields: Attn, Addr1Field, Addr2Field. I have found the location of both of the line breaks but the difference isn't the same for every row of data so I'm using the expression as the third option in my Substring() function. I'm getting the error Invalid length parameter passed to the LEFT or SUBSTRING function. In the attached image I hardcoded 34-20 to get my desired results but each row has the possibility to be different so I need to be able to use the expression.
Select
case
when LEFT(CONVERT(VARCHAR(MAX), soship.fmstreet),5)='ATTN:'
Then SUBSTRING(CONVERT(VARCHAR(MAX), soship.fmstreet), 7,CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet))-1)
Else ' '
End as AttnField,
case
when LEFT(CONVERT(VARCHAR(MAX), soship.fmstreet),5)='ATTN:'
Then SUBSTRING(CONVERT(VARCHAR(MAX), soship.fmstreet),CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet))+2,CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet),CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet))+1) -
CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet)))
Else ''
End as Addr1Field
My charindex was returning 0 so I wrote a Where clause to solve it.
Where
(CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet),CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet))+1) -
CHARINDEX(CHAR(13)+CHAR(10),CONVERT(VARCHAR(MAX), soship.fmstreet)) > 0)
in oracle, How to detect in case statement if the value of the field is in "0", then set it to ""?
because I have a problem detecting it when i use something like this
case
when REGEXP_LIKE (ctyid, '^[:DIGIT]') then '""'
it doesn't work, am still getting "0" in the output..i want to set it to ""
If you want to look for 0 anywhere in the string, then use LIKE operator :
WHEN ctyid LIKE '%0%' THEN NULL
That would return NULL value if 0 is found anywhere in the string.
If you want to look for the value as 0 itself, then use = operator.
WHEN ctyid = '0' THEN NULL
CASE looks easier to read, but the same expression could be written as DECODE :
DECODE(cytid, '0', NULL, cytid)
Please reference the documentation of Regular Expression Syntax in ORACLE
Right expression should be:
case
when REGEXP_LIKE (ctyid, '^[[:digit:]]') then '""' else ctyid end
OR
case
when REGEXP_LIKE (ctyid, '^\d') then '""' else ctyid end
But your sample code would match all ctyid start with a number, not only with 0 in it.
UPDATE
According to #LalitKumarB's comment, I have to append following code that I think it should be finished by the OPself.
case
when REGEXP_LIKE (ctyid, '^0') then '""' else ctyid end
I have the following column in a query.
iif(Len([Field1])=0,0,Asc(Mid([Field1] & "",Len([Field1]))))
The idea is that it should return the ASCII value of the last character in a string field.
The problem is that if Field1 is blank the statement errors with the following message: "Invalid procedure call or argument (Error 5)". If the field is blank it should return 0.
Assuming blank means either Null or an empty string, you can concatenate an empty string to [Field1], and if the combined length is 0, return 0.
The Right() function is more direct than Mid() to get the last character.
SELECT IIf(Len([Field1] & "")=0,0,Asc(Right([Field1],1)))
FROM YourTable;
By all means you have HansUp's answer to go ahead. :)
..At any point of this querying voyage, your query with IIF gets very very long... ;) here a head start with a UDfunction that uses REGEX. In your case the most important aspect is to validate the input - not much of getting the ASCII value. Since your concern mainly on last character, what if your Field1 contains alphanumeric value and if your last character happens to be digit, special character instead of String. Well, just to be safe, we could validate that in the following funciton which will respond only to Strings. Another advantage is that you can re-use this function within your db.
Perhaps this really is complicating your process :D
Option Compare Database
Option Explicit
Public Function GetASCII(strField1 As String) As Integer
Dim regex As Object
Dim strTemp As String
Set regex = CreateObject("vbscript.regexp")
'--assume strField1 = "hola", you may enter different types
'-- of values to test e.g. "hola1, hola$ , hola;
With regex
.IgnoreCase = True
.Global = True
.Pattern = "[a-zA-Z]+"
'--check for null
Select Case IsNull(strField1) '-- validates any other datatype than String
Case True
GetASCII = 0
Case Else
Select Case IsError(strField1)
Case True
GetASCII = 0
Case Else
Select Case IsEmpty(strField1)
Case True
GetASCII = 0
'--check if entire string is String
'--only (no special characters, digits)
'--you may change this to only check the
'----last character if your Field1 is alphanumeric
Case Else
Select Case .Test(strField1)
Case True
strTemp = Mid(strField1, Len(strField1), 1)
GetASCII = Asc(strTemp)
Case Else
GetASCII = 0
End Select
End Select
End Select
End Select
End With
Set regex = Nothing
End Function
Note: Thought this would be helpful in the long run :) An Access Query Primer Article.
Is there an easy way to get if string is an integer number (consists only of digits) in MS SQL 2005?
Thank you for your help.
The function ISNUMERIC returns whether a string is numeric, but will return true for non-integers.
So you could use:
WHERE ISNUMERIC(str) AND str NOT LIKE '%.%' AND str NOT LIKE '%e%' AND str NOT LIKE '%-%'
Even though the original poster was referring to SQL 2005, I found in 2008 r2 a straight where isnumeric(string) resulted in an error 4145 non-boolean type. To resolve this use:where isnumeric(string) = 1
You could use the LIKE operator:
WHERE str NOT LIKE '%[^0-9]%'
See this:
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
It is a little tricky to guarantee that a value will conform to a 4 byte integer.
Since you are using 2005 - One way is to try to convert the value within a try/catch block. That would be the best way to insure that it is actually an int. Of course you need to handle the cases when it does not in the catch block according to your requirements.
Another way to just test for only "digits" is this:
where strVal not like '%[^0-9]%'
That will miss -25. as well as allow '99999999999999999999'
So you may need to include additional criteria with this method.
Using (CAST(PATINDEX('%[^0-9]%', value) as BIT)) handles all the characters
Standard T-SQL function ISNUMERIC ( expression )
Determines whether an expression is a valid numeric type.