SQL Server : query if table exist - sql

I have the following query:
SELECT
COUNT(*) over () as countNum,
[F1] AS STANDARDandOBJ,
[F2] AS CLUSTER,
[F3] AS OBJECTIVE,
[F4] AS EXTRA0
IF COL_LENGTH([tmpExcelDB].[dbo].['Blahsing$'], [F5]) IS NOT NULL
BEGIN
print 'exists'
END
ELSE
BEGIN
print 'Nope'
END,
CONCAT([F1], [F2]) AS combined
FROM
[tmpExcelDB].[dbo].['Blahsing$']
WHERE
LOWER(F3) NOT LIKE 'course tools-%'
But it seems that I have an error of:
Incorrect syntax near ','.
Which is pointing to row:
,CONCAT([F1], [F2]) AS combined
How does this need to be formatted in order to work?

You can't use IF inside a SELECT, you need a CASE expression. Also, it doesn't make sense to use PRINT inside a column:
SELECT
COUNT(*) over () as countNum
,[F1] AS STANDARDandOBJ
,[F2] AS CLUSTER
,[F3] AS OBJECTIVE
,[F4] AS EXTRA0
,CASE
WHEN COL_LENGTH('[tmpExcelDB].[dbo].[''Blahsing$'']', '[F5]') IS NOT NULL
THEN 'exists'
ELSE 'Nope'
END
,CONCAT([F1], [F2]) AS combined
FROM [tmpExcelDB].[dbo].['Blahsing$']
WHERE
LOWER(F3) NOT LIKE 'course tools-%';

SELECT
COUNT(*) over () as countNum
,[F1] AS STANDARDandOBJ
,[F2] AS CLUSTER
,[F3] AS OBJECTIVE
,[F4] AS EXTRA0 ,
( CASE When COL_LENGTH([tmpExcelDB].[dbo].['Blahsing$'], [F5]) IS NOT NULL
THEN
'exists'
ELSE
'Nope'
END),
CONCAT([F1], [F2]) AS combined
FROM [tmpExcelDB].[dbo].['0812 Orientation to Nursing$']
WHERE LOWER(F3)NOT LIKE 'course tools-%'

Couple things:
IF...ELSE is not supported in a select list, use CASE instead
PRINT is not supported in a select list, just the literal string value is needed
See the CASE Documentation for a relevant example.

Related

teradata - invalid select expression list

Trying to make a select query on teradata, but i get this message error:
syntax error: invalid select expression list
I can't fix it. How can I formulate the query appropriately?
SELECT BILS01_GRADO, BILS01_CODICE_COM, BILS01_PROT, BILS01_PROG_OGG_IMP,BILS01_PROG_REC,
CASE WHEN BILS01_DATA_PRES_ISTSOSP<TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP=0 THEN BILS01_DATA_PRES_ISTSOSP
WHEN BILS01_DATA_PRES_ISTSOSP=TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP>0 THEN DATA_CONTROVERSIA2
WHEN BILS01_DATA_PRES_ISTSOSP=TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP=0 THEN TO_DATE('9999-12-31','YYYY-MM-DD')
WHEN BILS01_DATA_PRES_ISTSOSP<
(
CASE WHEN
(
CASE WHEN BILS01_DATA_SPED<to_date('9999-12-31','YYYY-MM-DD') THEN BILS01_DATA_SPED
WHEN BILS01_DATA_SPED=to_date('9999-12-31','YYYY-MM-DD') OR BILS01_DATA_RIC<to_date('9999-12-31','YYYY-MM-DD')THEN BILS01_DATA_RIC
WHEN BILS01_DATA_ACQ<to_date('9999-12-31','YYYY-MM-DD') THEN BILS01_DATA_ACQ ELSE BILS01_DATA_PROT END AS DATA_CONTROVERSIA
)
<TO_DATE('1972-01-01','YYYY-MM-DD') THEN TO_DATE('1972-01-01','YYYY-MM-DD') ELSE DATA_CONTROVERSIA END AS DATA_CONTROVERSIA2
)
THEN BILS01_DATA_PRES_ISTSOSP ELSE DATA_CONTROVERSIA2 END AS A) AS OUT_DATA_RICH_SOSP
FROM zucow.BILS01
--GROUP BY BILS01_GRADO, BILS01_CODICE_COM, BILS01_PROT, BILS01_PROG_OGG_IMP,
-- BILS01_PROG_REC, OUT_DATA_RICH_SOSP

SQL to get whole words till end from the keyword

Consider I have text from the field (notes) as below:
Please check http://example.com
I want a SQL query which fetches the link part only. That is to find keyword http and print till the last.
Output:
http://example.com
Also if the text field doesnt have any link, can we print NA?
CASE WHEN sys like '%Clo%'
THEN RIGHT( i.notes,LEN(i.notes) - CHARINDEX('http',i.notes,1)+1)
ELSE "No Link Available" END AS Cl_Link
For SQL Server you can consider this below logic-
DECLARE #T VARCHAR(MAX) = 'Please check http://example.com'
SELECT RIGHT(#T,LEN(#T) - CHARINDEX('http:',#T,1)+1)
For MySQL-
SET #T = 'Please check http://example.com';
SELECT RIGHT(#T,LENGTH(#T) - POSITION("http:" IN #T)+1)
In case of select query using table, queries will be-
-- SQL Server
SELECT RIGHT(column_name,LEN(column_name) - CHARINDEX('http:',column_name,1)+1) FROM your_table_name
-- MySQL
SELECT RIGHT(column_name,LENGTH(column_name) - POSITION("http:" IN column_name)+1) FROM your_table_name
To apply 'NA' when no link available, please use the below logic-
DECLARE #T VARCHAR(MAX) = 'Please check http://example.com'
SELECT
CASE
WHEN CHARINDEX('http:',#T,1) >= 1 THEN RIGHT(#T,LEN(#T) - CHARINDEX('http:',#T,1)+1)
ELSE 'NA'
END
Below is the query for your reference, executed on mysql:
SELECT substr(columnname,locate('http',columnname)) as link
FROM `tablename` where column=value
For MySQL try this:
select REGEXP_SUBSTR(text_column, 'http\:\/\/([\\w\\.\\S]+)') from mytest

ERROR: division by zero + PostgreSQL + Rails

I'm getting the following error when I run this database view in psql (ERROR: division by zero). Even I can't access my table in rails console If anyone has any solution for this error so please comment on it will be very helpful thanks in advance.
select
(exam_id || '00' || quarter)::bigint as id,
account_id,
exam_id,
quarter,
quarter_label,
count(id) as assessments,
max(banding_percentage) as top_assessment_percentage,
min(banding_percentage) as bottom_assessment_percentage,
round(avg(banding_percentage),2) as avg_assessment_percentage
from report_assessments
group by account_id, exam_id, quarter, quarter_label;
To avoid running into division by zero exceptions you should use nullif, so that you can catch this 0 and replace it by null, e.g.
WITH j (v1,v2) AS (
VALUES (1.0,0.0),(10.0,3.0)
)
SELECT v1/nullif(v2,0) AS div FROM j;
div
--------------------
3.3333333333333333
(2 Zeilen)
You Need to do something like this.
It works in my case:
Negotiation.find(
:all,
conditions: conditions_hash('negotiation'),
select:
'CASE
sum(banding_percentage)
WHEN
0
THEN
NULL
ELSE
sum(total_percentage) / sum(banding_percentage)
END as error_check'
).first

Incorrect syntax near 'End' , not letting run the query nor create procedure

Not letting me create the procedure, nor not letting run this query please help.
It's not letting run the query .. this is just a section of query
You had a few things wrong, you didnt need the parens, you were missing an End Statement, and your indents made it hard to read, and you are missing your FROM statement, so I put FROM and added tableName you need to fill this in.
create proc [dbo].[a_test]
as
begin
SELECT top 10
file_ty = '5',
cno= rtrim(ltrim(adm.AccountNumber)),
Column3='',
Column4='',
Admit_Date =
CASE
WHEN RegistrationType_MisRegTypeID <>'IN'
THEN ''
ELSE CONVERT(varchar(10),replace(convert(varchar(10),adm.AdmitDateTime,101),'/ ',''))
END,
Point_Of_Origin =
CASE
WHEN RegistrationType_MisRegTypeID='IN' then
CASE
WHEN AdmitSource='CIR' then '2'
WHEN AdmitSource='BSH' then '2'
ELSE ''
END
END
FROM dbo.TableName
END

Does SQL Server Compact support multiple return case parameters

I know the following is legitimate SQL query, but why this cannot be interpreted in SQL Server Compact? (I'm using SQL Server Compact view)
Select
Case AStatus
When 1 then 'Success', 'AStatus', 'Expected:1'
When 0 then 'Faliure', 'AStatus', 'Recived: 0'
end
From Statuses
Where LocalPath= 'c:\Status
I get something like:
Query 1 : There was an error parsing the query [Token line number=3, Token line offset=22, Token in Error=,]
And when writing something like the following it works:
Select
Case AStatus
When 1 then 'Success'
When 0 then 'Faliure'
end
From Statuses
Where LocalPath= 'c:\Status
I think that's the only valid way to get three columns from case:
Select
Case AStatus
When 1 then 'Success'
When 0 then 'Faliure'
END,
Case AStatus
When 1 then 'AStatus'
When 0 then 'AStatus'
END,
Case AStatus
When 1 then 'Expected:1'
When 0 then 'Recived: 0'
END
From Statuses
Where LocalPath= 'c:\Status'
EDIT:
Another way. Not much shorter, but seems more flexible:
Select
Astatus,
x.*
From Statuses s
CROSS APPLY (
select 'Success' as c1,'AStatus' as c2,'Expected:1' as c3 where AStatus=1 union all
select 'Failure' ,'AStatus','Recived:0' where AStatus=0
) x
Where LocalPath= 'c:\Status'
You get +1 for making me come up with that, I already know where I'll use it :).
Your statement is missing the END Cluase in the CASE statement I dont know if this is the only error try this 1st
Select
Case AStatus
When 1 then 'Success', 'AStatus', 'Expected:1'
When 0 then 'Faliure', 'AStatus', 'Recived: 0'
END
From Statuses
Where LocalPath= 'c:\Status