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

(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

Related

how to use db column name in regex_replace() - presto

I am new to presto, I am looking to use regex_replace on a particular db column instead of a string.
E.g: Replace all entries from a column "Description" that starts with digit and followed by space from "table1"
Can someone please help with an example?
I tried this :
select(regexp_replace(col("Description"), '\d+\s')) from table1
but getting error: "Function not found", function "Col" is not registered.
Thanks in advance!
Its enough to put column name and a replacement string should be added. So,
col("Description") -> Description
-> ,'replace_string'
Final query should be something like this :
select regexp_replace(Description, '\d+\s','replace_string' ) from table1 b
I think this does what you want:
SELECT
REGEXP_REPLACE(`Description`, '\d+\s')
FROM `table1`
They're optional in SQL, but you use backticks for column and table names, apostrophes for strings.

Find phone numbers with unexpected characters using SQL

I posted the same question below for SQL in Oracle here and was provided the SQL info within that works.
However, I now need to perform the same in a DB2 database and if I attempt to run the same SQL it errors out.
I need to find rows where the phone number field contains unexpected characters.
Most of the values in this field look like:
123456-7890
This is expected. However, we are also seeing character values in this field such as * and #.
I want to find all rows where these unexpected character values exist.
Expected:
Numbers are expected
Hyphen with numbers is expected (hyphen alone is not)
NULL is expected
Empty is expected
This SQL works in Oracle:
...
WHERE regexp_like(phone_num, '[^ 0123456789-]|^-|-$')
When using the same SQL above in DB2, the statement errors out.
I found it easiest to answer your question by phrasing a regex which matches the positive cases. Then, we can just use NOT to find the negative cases. DB2 supports a REGEXP_LIKE function:
SELECT *
FROM yourTable
WHERE
NOT REGEXP_LIKE(phone_num, '^[0-9]+(-?[0-9]+)*$') AND
COALESCE(phone_num, '') <> '';
Here is a demo of the regex:
Demo
For newer version of db2, regexp is the way to go. If you are on an older version (perhaps why you get an error), you can replace all accepted chars with '' and check if the result is an empty string. Can't check right now, but from memory, it would be
WHERE TRANSLATE(phone_num, '', '0123456789-')<>''
EDIT:
For what it's worth your regexp works for V11 so you probably have an older version of Db2. Example of translate and regexp side by side:
]$ db2 "with t(s) as ( values '123456-7890', '12345*-7890' )
select s, 'regexp' as method from t
where regexp_like(s, '[^ 0123456789-]|^-|-$')
union all
select s, 'translate' as method
from t where TRANSLATE(s, '', '0123456789-')<>''"
S METHOD
----------- ---------
12345*-7890 translate
12345*-7890 regexp
2 record(s) selected.

SQL : REGEX MATCH - Character followed by numbers inside quotes

I have a column in sql which holds value inside double quotes like "P1234567" , "P1234" etc..
I need to identify only columns which start with letter P and is followed by seven digits (numbers) only. I tried where column like'"P[0-9][0-9][0-9][0-9][0-9][0-9][0-9]"' but it doesn't seem to work.
Can someone please correct me or point me to a thread which can help me out?
Thanks
Standard SQL has no regex support, but most SQL engines have regex extensions added to them on top of the standard SQL. So, for example, if you're using MySQL then you'd do this:
... WHERE column REGEXP '^"P[0-9]{7}"'
And if you're using Postgres then that would be:
... WHERE column ~ '^"P[0-9]{7}"'
(updated to match the double-quote part of the question, I'd misunderstood that to begin with)
How about using length and isnumeric:
Select
*
from
mytable
where
mycolumn like '"P%'
and len(mycolumn) = 10 --2 chars for quotes + 1 for 'P' + 7 for the digits
and isnumeric(substring(mycolumn, 3, 7))=1
This answer is for SQL Server, other DBMS's may have a different syntax for length

how to make sqlite column aliases numeric or computed

i'm trying with sqlite to make code like this working:
select
data1 as 1
data2 as 'hello' + 'hi [or 'hello' || 'hi']
data3 as 6 * 3
from
table
but every effort was vain...
i tried with the || concatenation or with the +
i tried with something like
...AS select 6 * 3
but it seems like no operation is admitted in column aliases!
how can i set poperly the column alias of a select?
thanks everyone
Having an expression as column name is not possible in sqlite. Here is the full allowed syntax of sqlite. The column-alias after as doesn't accept expression:
A column alias is an identifier like a table name or column name.
To use an arbitrary string, you have to quote it, like with other identifiers:
SELECT 42 AS "42 is the answer!!!"
You cannot do computations on identifiers.

How to select on strange column name in SQLite terminal?

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