Error while using case with substring query while multiply with number - sql

I have an issue while am trying to pull and create specific column output in SQL
select
left(PostalCode,2) as area
,case substring(PostalCode,6,1)
when substring(PostalCode,6,1) = 0 then '100'
when substring(PostalCode,6,1) > 0 then (substring(PostalCode,6,1))*100
else 'NotDigit'
end + substring(PostalCode,6,1) as [location]
from province_table
PostalCode
1K02BC19
1K03B018
1K04B519
I tried many ways to restructure the query above and am always unable to get a result
expected result
Location
NoDigit
100
500
looking forwards to your kind help
Thanks

select
left(PostalCode,2) as area
,case when isnumeric(substring(PostalCode,6,1)) = 1 and substring(PostalCode,6,1) = 0 then cast('100' as varchar(max))
when isnumeric(substring(PostalCode,6,1)) = 1 and substring(PostalCode,6,1) > 0 then cast(substring(PostalCode,6,1)*100 as varchar(max))
else 'NotDigit' end as result
from province_table
area
result
1K
NotDigit
1K
100
1K
500
Fiddle

Related

Count(*) return 1 or zero

In one of the usecase I need a query which should return 1 based on condition also if not match it should return 0
In Descpriont column if the 'SAP' count is exactly 1 then the query should return 1 else it should return 0
Note : There might be a chance that SAP could be present any number of times in Description column.
Could someone help me out here !!
Thanks.
I tried below query :
SELECT 1 from TableName where Description ='SAP' having count(*)>1
It is returning 1 but not return 0 if the count is more than 1 or no match found.
Use CASE WHEN to decide whether to show 0 or 1.
select case when count(*) = 1 then 1 else 0 end as sap_count_is_1
from mytable
where description = 'SAP';
use case when
select
case when sum(case when description='SAP' then 1 else 0 end)=1 then 1 else 0 end from table_name

Changing when to if statement in SQL server

How do I change the following code to an if statement that returns a boolean 0 or 1 value? My end results I would like to have, is one column listing the interest rate of 2, and my results column with a 0 or 1 if the condition is true.
(Case when new_interestratevariability = 2
and (new_interestrateindex = 1 or new_interestrateindex = 2 or new_interestrateindex = 3 or new_interestrateindex = 4 or new_interestrateindex = 6)
and new_crms_dt = #Curr_Date
then 0 else 1 end) as CIEDIT_VAL_96,
Currently, I am getting something like below:
Results Table
To filter rows, use a Where clause. The Case statement in the Select clause will modify the value shown on the row.
Select *
from table
Where new_interestratevariability = 2
and new_interestrateindex IN (1,2,3,4,6)
and new_crms_dt = #Curr_Date
Found my answer, it was as simple as adding "not in" instead of just "in". Thanks everyone
(Case when new_interestratevariability = 2
and (new_interestrateindex not in(1,2,3,4,6))
and new_crms_dt = #Curr_Date
then 1 else 0 end) as CIEDIT_VAL_96,

Select with pattern from table in SQLite

I have a table with a column that contains this values
patern_number
0936
09154
123456
And I have a number which can be anything.
What I want is a select which returns 1 when input number start with one of pattern else return 0
example
input number result
093628987 1
0915 0
0222 0
091546666 1
So can anyone help me?
Try this:
SELECT CASE WHEN EXISTS(
SELECT 1 FROM patern_table
WHERE inputNumber Like patern_number || '%') THEN 1
ELSE 0 END As result
SQL Fiddle Demo

sql how to use case?

Hello i have 2 tables 1 with btwvalues and 1 with orders
I am trying to select the final rate (
if extrahours =1 then finalhourscost = extrahoursrate
else finalhourcost = hoursrate)
i have made an example
http://www.sqlfiddle.com/#!2/dd8d9/17
So what i want is create a row finalhourscost depending on both tables. finalhourcost is not an existing row.
What have i done wrong , as you see it returns 1 instead of 40
SELECT extrahours,
extrahourrate,
hourrate,
CASE extrahours WHEN 0 THEN extrahourrate ELSE hourrate END AS finalrate
FROM bill JOIN cost ON bill.costid = cost.id

SQL query for displaying record with maximum number of non-empty fields

I am looking for a query/set of SQL queries that will give me the record ID of the record which has the "maximum number of non-empty/non-null fields".
I was looking into count() and max() functions, but they seem to be solving problems for the same column, but not for the same row (which is what I am looking for).
Please help.
You could order by the amount of non-empty fields:
select top 1 Record_ID
from YourTable
order by
case when isnull(col1,'') <> '' then 1 else 0 end +
case when isnull(col2,'') <> '' then 1 else 0 end +
case when isnull(col3,'') <> '' then 1 else 0 end +
...
case when isnull(colN,'') <> '' then 1 else 0 end
This is SQL Server syntax. If you're using another database, please amend your question.