How to select on strange column name in SQLite terminal? - sql

sqlite> select * from questions;
data_id data_text data_image parent_id data_order data_id:1 data_text:1
---------- ---------- ---------- ---------- ---------- ---------- -----------
23 google 5 5 favorites
Hi, I'm testing query. But I want to select just the 'data_text:1' column.
I don't know how to select column.

Surround the name of the column with double-quotes:
select "data_text:1" from questions;
SQLite follows SQL-92 syntax where double quotes are used to quote identifiers (column/table names). Single quotes are used to quote literal values like strings 'I am a string!' or X'ABCD' (blob data).
Happy coding.
I recommend simple column names that do not require to be quoted in SQL and do not clash with reserved words as it make life easier.

Surround the name of the column with ``
`data_text:1`
Known as a 'backtick' you usually find it above TAB and left of 1 on keyboards.
Alternatively you can get it with the combination : (alt gr + 7)

Surround the name of the column with double-quotes
select "data_text:1" from questions;
#pst helps to explain it in a comment. I'm paraphrasing him here:
SQLite follows SQL-92 so a double quote (which is an identifier quote) will work

Related

How we can display ASCII code for single quote in SQL?

I got the answer but can someone please explain me how and why we need to use extra single quote????
Select ASCII ('''') from dual;
Because you have to escape it.
string literal begins with a single quote
another one terminates the string ...
... unless it is immediately followed by another single quote which "escapes" it
For example: this is what you have:
SQL> Select ASCII ('''') from dual;
ASCII('''')
-----------
39
If you omit one of single quotes, you'll get an invalid string:
SQL> Select ASCII (''') from dual;
ERROR:
ORA-01756: quoted string not properly terminated
Only two single quotes? That's OK, but - it is an empty string, not what you wanted (a single quote):
SQL> Select ASCII ('') from dual;
ASCII('')
----------
Alternatively, instead of that many single quotes, you can use the q-quoting mechanism like this:
SQL> select ascii(q'[']') from dual;
ASCII(Q'[']')
-------------
39
SQL>
Basically, you'd enclose string (that contains single quotes) into brackets. That example (with only one single quote) looks stupid, but - have a look at this:
SQL> select 'L''Oreal' example1,
2 q'[L'Oreal]' example2 --> this
3 from dual;
EXAMPLE EXAMPLE
------- -------
L'Oreal L'Oreal
See? Within the brackets you use single quotes "normally", without escaping them. In more complex situations, that mechanism really, really helps.

Oracle SQL Group BY via column substr

SELECT SUBSTR(PRODID,1, 4) AS [PROD4], COUNT(*) AS [NumberOfRows]
FROM [sch].[ProdTable]
GROUP BY SUBSTR(PRODID,1, 4)
We're writing a simple select that would count how many of our products have the same first 4 characters. Our Product IDs are 10 digits/characters.
When running this, however, we get:
SQL Error [936] [42000]: ORA-00936: missing expression
Any idea how to make this work?
The problem is with the square brackets. Oracle does not support that syntax. These identifiers probably do not require quoting anyway, so:
SELECT SUBSTR(PRODID,1, 4) PROD4, COUNT(*) NumberOfRows
FROM sch.ProdTable
GROUP BY SUBSTR(PRODID,1, 4)
If you really need to quote the identifiers (say, if the table name was created as a case-sensitive name, or you do want mixed-case column aliases), then you can use double quotes:
SELECT SUBSTR(PRODID,1, 4) PROD4, COUNT(*) "NumberOfRows"
FROM sch."ProdTable"
GROUP BY SUBSTR(PRODID,1, 4)
Just expanding a bit on #GMB's answer.
[sch].[ProdTable]
In Oracle, that's an incorrect syntax to refer an object. Don't enclose them in [] square brackets:
FROM sch.ProdTable
However, if you actually want to use square brackets for naming your objects(which would be really ugly) you could use quoted identifier. It begins and ends with double quotation marks ". This also makes then case sensitive, and must always be used with double quotation marks whenever you refer to that object.
create table "[t]" as select 'hi' as "[str]" from dual;
select * from "[t]";
[str]
-----
hi
In your SQL, unless your table is created that way, you don't need to that. You could still name your column's alias with square brackets using quoted-identifier:
select 'hi' as "[str]" from dual;
[str]
-----
hi

Oracle SQL Commas format?

I am trying to have my numbers have commas
i.e.
4333 ---> 4,333
I came up with this
TO_CHAR(COUNT(*),'$9,999.99') AS TOTAL_APPS
basically Im counting everything in the db and want the commas present, this is already in a select statement and according to oracle docs that is the structure for commas, what is the issue?
So, what's wrong with what you came up with? Doesn't it do what you wanted?
Though, it looks like you found it somewhere on the Internet and applied to your situation because
This is what you have:
4333
This is what you want:
4,333
This is what you have (with '$9,999.99' format mask), i.e. there's a dollar sign as well as decimals which you - apparently - don't want:
SQL> select to_char(4333, '$9,999.99') result from dual;
RESULT
----------
$4,333.00
If you change the format mask to this:
SQL> select to_char(4333, '9G999', 'nls_numeric_characters = .,') result from dual;
RESULT
------
4,333
you might get what you wanted.
Why did I use it like this? G is the "thousands" separator. It can be different in different databases; someone uses a comma, someone else uses a dot, etc. NLS_NUMERIC_CHARACTERS says which character is actually being used for the G mark, so it could have been e.g.
SQL> select to_char(4333, '9G999', 'nls_numeric_characters = ^=') result from dual;
RESULT
------
4=333
An alternative approach that will only work in SQLPlus is to use SQLPlus formatting.
In this example, a simple query return a large number. With no formatting we just get the plain number back.
SQL> select 373746764 from dual;
373746764
----------
373746764
Let's give it an alias, still no formatting:
SQL> select 373746764 tn from dual;
TN
----------
373746764
Now let's use teh COL formatting command in SQL*PLUS:
SQL> col tn form '999,999,990.00'
SQL> select 373746764 tn from dual;
TN
---------------
373,746,764.00
So,. in your case, you could create a script that has all these COL column_alias commands which you can call and ensure your scripts have appropriate column aliases to match.
for example a script called formating.sql:
COL total_apps form '999,999,990'
COL total_cost form '999,999,990.00'
Then you report.sql scriopt can have:
#formatting.sql
SELECT COUNT(*) TOTAL_APPS FROM the_table;
I think this will work with SQL Developer as well, but as I said this approach is for SQL*Plus.
For more widespread results then TO_CHAR on each query is the way to go.

Alternative to enquote_literal for string containing single quote

I am bound to use dbms_assert.enquote_literal to enquote string. The string is schema name which is unknown to me as it is coming as a parameter to my function. The only thing I know is that a schema name may contain single quote. For such strings enquote_literal fails with ORA-06502: PL/SQL: numeric or value error. Is there any alternative that I can use in place of enquote_literal that gives the same output as enquote_literal.
Not a good solution, but an easy solution is
REPLACE(dbms_assert.enquote_literal(REPLACE(text,'''','''''')),
'''''','''');
Input Text
hello'world
Output Text
'hello'world'
If you don't need the quote to appear even once
dbms_assert.enquote_literalreplace(text,'''',''));
Try 'q' quotes Read here.
select q'['<you string>']' from dual;
Demo
SQL> select q'['Hello'workddl']' Col from dual;
COL
--------------
'Hello'workddl'

sqlite: alias column name can't contains a dot "."

(sorry for my poor english)
If you try this select operation over a sqlite database:
SELECT column AS 'alias 1' FROM table;
You get the expected column name:
alias 1
--------
result 1
result 2
but if your alias contains a dot "." ... you get a wrong column name:
SELECT column AS 'alias.1' FROM table;
1
--------
result 1
result 2
(all behind the dot is ommited in the column name)
Wow...
It's weird...
anyone can help me?
thank you very much
UPDATE:
maybe it's just a bug in SQLiteStudio (the software where I'm testing my queries) and in QT (they both doesn't expect dots in alias names but sqlite does)
Enclose your alias in double quotes.
SELECT 'test' AS "testing.this"
Output:
| testing.this |
test
Updated:
Double quotes are used to enclose identifiers in SQL, not single quotes. Single quotes are only for strings. In this case you are trying to ensure that "testing.this" is used as is and not confused as testing.this (testing table this column).
http://www.sqlite.org/faq.html#q24
Use backticks
SELECT column AS `alias.1` FROM table;
Or double quotes (ANSI standard) per the other answer
SELECT column AS "alias.1" FROM table;
Both verified in SQLite Manager for FireFox
Definitely working properly:
C:\Windows>sqlite3.exe
SQLite version 3.7.8 2011-09-19 14:49:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .mode column
sqlite> .headers on
sqlite> SELECT 'hello' AS 'alias.1';
alias.1
----------
hello
sqlite>
If you're using the SQLite 3 then the following query works just fine with various types used for the Alias column names.
See the result below the query:
select '1' as 'my.Col1', '2' as "my.Col2", '3' as [my.Col3], '4' as [my Col4] , '5' as 'my Col5'
I've found a "fix"...
SELECT column AS '.alias.1' FROM
table;
alias.1
--------
result 1
result 2
just another dot in the begining...
of course I don't like this solution...
any other idea??
Please try below, it works on Hive
select 1 as `xxx.namewith.dot`
xxx. means any word you want to input with dot notation on latest
namewith.dot means any alias name with dot notation on it