How to get valid value from the following query
SELECT Answer FROM table
WHERE values LIKE '%[^0-9]%'
Basically I want the data can deal for
28,000 (valid)
$20000 (valid)
Annual Amount (invalid)
? (invalid)
28.00 (valid)
Thanks
SELECT Answer
FROM table
WHERE
ISNUMERIC(values)
OR (
SUBSTRING(values, 1, 1) = '$'
AND ISNUMERIC(RIGHT(values, LEN(values) - 1)))
you could do something like:
select replace(replace(values, '$', ''), ',', '') as number from table
where dbo.RegexMatch(values, ^\$?(\d+|(\d{1,3}(,\d{3})+))(\.\d+)?$')
tweak the regex to match any conditions you need...
Related
im trying to validate a column using postgresql
where values in the column are (0000-ASZAS) four numerical values-five alphbets
SELECT invoice_number,
CASE
WHEN invoice_number = '[0-9][0-9][0-9][0-9]-[A-Z][A-Z][A-Z][A-Z][A-Z]'
THEN 'valid'
ELSE 'invalid'
END
from invoices;
also tried LIKE instead of =
sorry wrong column customer_id with alpha numeric values
invoice_number is numeric. thank you for the correction
Try ~ or similar_to. See functions-matching
WHEN invoice_number ~ '[0-9][0-9][0-9][0-9]-[A-Z][A-Z][A-Z][A-Z][A-Z]'
You may use the ~ POSIX regex operator:
SELECT invoice_number,
CASE WHEN invoice_number ~ '^[0-9]{4}-[A-Z]{5}$'
THEN 'valid'
ELSE 'invalid' END
FROM invoices;
I'm cleaning a data and want to return 0 rows so it doesn't crowd the results so this is what I did...
SELECT customer_id FROM invoices
WHERE customer_id !~ '[0-9][0-9][0-9][0-9]-[A-Z][A-Z][A-Z][A-Z][A-Z]';
w/c gives me exactly what I want. Would this be as strong/valid for cleaning compared to using like or = ?
In TSQL, the string in the database record is 'A/A/A' or 'A/B/A' (examples). I want to parse the string and for the first instance return '1'; in the 2nd instance, return '2'. That is, if all the values between the separators are the same, return a value; otherwise return another value. What is the best way to do this?
A bit blind answer:
Read the whole value in a variable. Read the first value part in another:
declare #entire nvarchar(max), #single nvarchar(max)
select/set #entire=....
set #single=left(#entire,charindex('/',#entire)-1)
Compare entire with #single replicated after removing slashes:
set #entire=replace(#entire,'/','')
select case when replicate(#single,len(#entire)/len(#single))=#entire
then 1 else 0 end as [What you want]
Something like this should work:
SELECT
x.*,
CASE
WHEN N > 1 THEN 0
ELSE 1
END Result
FROM (
SELECT
t.Column1,
t.Column2,
t.Column3,
t.SomeColumn,
COUNT(DISTINCT s.value) N
FROM dbo.YourTable t
OUTER APPLY STRING_SPLIT(t.SomeColumn,'/') s
GROUP BY
t.Column1,
t.Column2,
t.Column3,
t.SomeColumn
) x
;
Based on your simple example (no edge cases accounted for) the following should work for you:
select string, iif(replace(s,v,'')='',1,0) as Result
from t
cross apply (
values(left(string,charindex('/', string)-1),(replace(string,'/','')))
)s(v,s);
Example Fiddle
I am trying to apply a Red / Amber / Green logic put in place for reporting purposes.
COLUMN_NAME will have a string of three characters with combinations of the letters 'R', 'A' and 'G.
I am trying to apply the logic:
If COLUMN_NAME contains 'R' THEN MAJOR_ISSUE
If Column COntains 'A' and NOT 'R' THEN MINOR_ISSUE
ELSE OK
Can you please tell me how I can ammend the SQL Update statement below to achieve this?
UPDATE DBO.TABLE_NAME
SET [COLUMN_RESULT] =
(
CASE WHEN COLUMN_NAME LIKE '%R%'THEN 'MAJOR_ISSUE'
WHEN COLUMN_NAME LIKE '%A%' ***AND NOT LIKE %R%*** THEN 'MINOR ISSUE'
ELSE 'OK'
END
)
Any help would be appreciated
AND is a logical operator, you need two complete boolean expressions on either side.
boolean AND boolean
LIKE is a boolean operator, which needs a string on either side.
string LIKE string
So...
WHEN COLUMN_NAME LIKE '%A%' AND COLUMN_NAME NOT LIKE '%R%' THEN 'MINOR ISSUE'
Or, with brackets to make it more obvious...
WHEN (COLUMN_NAME LIKE '%A%') AND (COLUMN_NAME NOT LIKE '%R%') THEN 'MINOR ISSUE'
A case expression checks the conditions in order, so you don't need to check for conditions that have already been processed:
update DBO.Table_Name
set Column_Result = case
when Column_Name like '%R%' then 'MAJOR_ISSUE'
-- At this point Column_Name cannot contain an 'R'.
when Column_Name like '%A%' then 'MINOR_ISSUE'
else 'OK' end;
Note that there have been various issues where case evaluates expressions without short-circuiting, e.g. a divide-by-zero might occur when you have already checked for a zero value because the expressions are being evaluated out-of-order.
I have a table named `test' which has following structure.
category key value
name real_name:Brad,nick_name:Brady,name_type:small NOVALUE
other description cool
But I want to break key column into multiple rows based on , delimiter and value after : delimiter should be a part of value column where value is equal to NOVALUE. So output should look like:
category key value
name real_name Brad
name nick_name Brady
name name_type small
other description cool
How to write sql query for this . I am using postgresql.
Any help ? Thanks in advance.
You can use string_to_array and unnest to do this:
select ts.category,
split_part(key_value, ':', 1) as key,
split_part(key_value, ':', 2) as value
from test ts
cross join lateral unnest(string_to_array(ts.key, ',')) as t (key_value)
where ts.value = 'NOVALUE'
union all
select category,
key,
value
from test
where value <> 'NOVALUE';
SQLFiddle example: http://sqlfiddle.com/#!15/6f1e6/1
select category,
split_part(key_value, ':', 1) as key,
case when value = 'NOVALUE' then split_part(key_value, ':', 2) else value end
from test
cross join lateral unnest(string_to_array(key, ',')) as t (key_value)
I need to further refine my stored proc resultset from this post, I need to filter my resultset to display only records where emailaddr is NULL (meaning display only records that have Invoice_DeliveryType value of 'N' ).
Among numerous queries, I have tried:
select
Invoice_ID, 'Unknown' as Invoice_Status,
case when Invoice_Printed is null then '' else 'Y' end as Invoice_Printed,
case when Invoice_DeliveryDate is null then '' else 'Y' end as Invoice_Delivered,
(case when Invoice_DeliveryType <> 'USPS' then ''
when exists (Select 1
from dbo.Client c
Where c.Client_ID = SUBSTRING(i.Invoice_ID, 1, 6) and
c.emailaddr is not null
)
then 'Y'
else 'N'
end)
Invoice_ContactLName + ', ' + Invoice_ContactFName as ContactName,
from
dbo.Invoice
left outer join
dbo.fnInvoiceCurrentStatus() on Invoice_ID = CUST_InvoiceID
where
CUST_StatusID = 7
AND Invoice_ID = dbo.Client.Client_ID
AND dbo.client.emailaddr is NULL
order by
Inv_Created
but I get an error
The conversion of the nvarchar value '20111028995999' overflowed an int column
How can I get the stored procedure to only return records with DeliveryType = 'N' ?
Trying selecting the stored proc results into a temp table, then select
* from #TempTable
We could really do with a schema definition to get this problem resolved.
It appears that there is an implicit conversion occurring within one of your case statements, but without the schema def's it's difficult to track down which one.
You can't safely mix datatypes in CASE expressions, unless you are absolutely sure that any implicit conversions will work out OK you should make the conversions explicit.
Judging by the error message seeming to include something that could be a date represented as a string(20111028) plus some kind of other data ?time?(995999) it may be something to do with Invoice_DeliveryDate, but this is a shot in the dark without more details.