SQL Select As with Apostrophe - sql

I am stuck with the following SQL line due to the apostrophe. I am using this to generate a table in Power BI that is used as the RLS link. As a result the apostrophe needs to stay in one string.
select 'adam.obrian#email.com.au' as Email, 'Adam O'Brian' as Access union all
How would I get around this?
Expecting a table as below:
Email
Access
adam.obrian#email.com
Adam O'Brian
Ignoring the Union all, this is part of a long table query.

Pleased to say I got this one sorted myself.
All it needed was a double apostrophe as below:
select 'adam.obrian#email.com.au' as Email, 'Adam O''Brian' as Access union all
Cheers

Related

single quote in column name: SQL expression working fine in SQL Developer but failing in PowerBI

I do have a piece of SQL code that gave me some problems. The pivot operation results in a table with a column name called 'INITIAL' which has the single quotes in the name. But the values in that column are integers, or NULLs. I managed to access the numbers in SQL developer by enclosing the column name with double quotes: "'INITIAL'". When copying the code to PowerBI, it is not accepted. I returns a
Expression.SyntaxError: Token Comma expected
at the location of the first use of the single quote.
Any idea how to fix this for powerBI?
PIVOT (
SUM(conversion)
FOR PHASE
IN ('INITIAL')
))
SELECT REQUEST, SUM("'INITIAL'")
FROM Pivoted
WHERE 'INITIAL' IS NOT NULL
GROUP BY REQUEST
that nearly did the trick. I misread your post and used this:
SUM(""'02_INITIAL'"")
And that works in PowerBI!
So in SQL Developer I need to use SUM("'INITIAL'")
Best is fo course to avoid quotes in column names, but since my Pivot is based on string values, I get them for free.
Many thanks for your suggestion that put me on the right track!
To escape a character you need to use double quotation ("") mark. Please try this:
PIVOT (
SUM(conversion)
FOR PHASE
IN ('INITIAL')
))
SELECT REQUEST, SUM(""'INITIAL'"")
FROM Pivoted
WHERE 'INITIAL' IS NOT NULL
GROUP BY REQUEST

VBA Access Table reference in SQL query

I have been running into trouble executing SQL code in VBA Access when I refer to certain Table names.
For example,
INSERT INTO TempTable (ClientName) SELECT DISTINCT 1_1_xlsx.ClientName FROM 1_1_xlsx'<--does not work
The code works fine when I changed the Table name from 1_1_xlsx to Stuff.
INSERT INTO TempTable (ClientName) SELECT DISTINCT Stuff.ClientName FROM Stuff '<--works
I have no idea why the first query results in a syntax error and the second code is runs fine even when they refer to the same thing. I suspect it should be the naming conventions but I could not find any concrete answers.
Also, are there any ways that I could use 1_1_xlsx as my table name? Or am I just writing my query wrong?
try this:
INSERT INTO TempTable (ClientName) SELECT DISTINCT [1_1_xlsx].ClientName FROM [1_1_xlsx]
In many SQL based databases you can't have a table name or field name that starts with a number.
I suspect this is the underlying reason for your problem. Although Access will allow it, I have seen it cause problems in the past.
The problem is the number at the beginning of the table name. That is bad -- because it confuses the parser.
This is a bad table name, but SQL allows you to define table aliases. And, in this case, you don't even need to repeat the table name. So, here are two simple solutions:
INSERT INTO TempTable (ClientName)
SELECT DISTINCT ClientName
FROM 1_1_xlsx;
Or:
INSERT INTO TempTable (ClientName)
SELECT DISTINCT t.ClientName
FROM 1_1_xlsx as t
There is no reason to use the complete table name as an alias. That just makes the query harder to write and to read.

Search for a long list of names in SQL

I have a CSV list with a few thousand names (first, last) and I would like to search for them in SQL. How can I do this easiest?
Edit: I'm using oracle obiee so I'm somewhat limited. Not sure if I can just load a csv into a temporary table since my access is limited. Will try this tomorrow at work since I just left. Using join on it sounds quite clever to my beginner ears though.
Follow these steps
Import CSV files into your SQL server of your choice.
Use the following sql statement: select * from <table name> where <last name column> = 'smith';
I ended up doing
WHERE 'first name'||'last name'||'first name' LIKE '%john%smith%'
The first name is concatenated both at beginning and end so that it mathes both 'john smith' and 'smith john'.

SQl Trim problem

I am trying to pull data from table using this select statement..
SELECT ID_NO
FROM EMPLOYEE
WHERE trim(SYN_NO) ='21IT';
SYN_NO column hold data in this format
21IT / 00065421
I want to get just first four characters and drop rest of it.. i tried trim, rtrim but it didnt work. Is there a way to do this.. Thank you
Sadly for those of us coming from SQL Server, there is no LEFT in oracle. The article I linked below talks about one person's encounter with this problem, and recommends using
SUBSTR(SYN_NO,1,4)
(see article)
Oracle SUBSTR specification
Sorry, just realized this is Oracle. You need to use SUBSTR:
SELECT ID_NO
FROM EMPLOYEE
WHERE SUBSTR(SYN_NO, 1, 4) ='21IT';
In Oracle, you'd use the SUBSTR function
SELECT id_no
FROM employee
WHERE substr(syn_no,1,4) = '21IT'

Problem select specific column name using sql statement

I found problem when trying to retrieve specific name column that using syntax/statement/command using sql.
Example I have table 'dcparam' with name some column 'SELECT', 'INSERT', 'UPDATE' in database sqlserver.
Then I trying to select using query:
SELECT SELECT,INSERT,UPDATE FROM dcparam
Well it could be solve using "*" in select, but then how if i wish only specific column.
Regard.
Add square brackets around the column name.
SELECT [SELECT], [INSERT], [UPDATE] FROM dcparam
It's probably best to reconsider your column names in the long run however...