how to make sqlite column aliases numeric or computed - sql

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.

Related

SQL: Select a column where a longer string is like a column

I want to change some data in the database where the text of a column is in a string.
For example we have the String: 'QWZ-$ §_REMARKIUFZQ'
Now I want to select a column where the column name has the text 'Mark'.
Something like this stupid try:
SELECT info FROM database WHERE '%'+name+'%' LIKE 'QWZ-$ §_REMARKIUFZQ'
Best Regards,
Christian
From sqlite doc:
The operand to the right of the LIKE operator contains the pattern and the left hand operand contains the string to match against the pattern.
Switch the LIKE operands i.e. WHERE 'QWZ-$ §_REMARKIUFZQ' LIKE '%'||name||'%' (notice + changed to sqlite concatenate operator || for native sqlite syntax).
Assuming sqlite3 version >= 3.16, you may find the table valued function pragma_table_info is the place to look.

How to use LIKE in WHERE clause to get first 5 characters of variable?

I have a variable varchar that always takes in 10 digits. How can I use the LIKE operator to find/use only the first 5 digits of the variable?
my query:
variable IN VARCHAR2
SELECT * FROM items WHERE name LIKE SUBSTRING(variable, 1, 5)
... WHERE name LIKE '12345%'
will match any string that starts 12345. the '%' is a wildcard. You can also use the wildcard to match anywhere in the string: ... WHERE name LIKE '%12345%' will match a string with 12345 anywhere within it.
Edit for completeness: WHERE name LIKE '%12345' will match any string that ends with those five characters.
Try this:
SELECT * FROM items WHERE name LIKE (SUBSTRING(variable, 1, 5) + '%')
I guess you can use LEFT() like this:
SELECT * FROM items WHERE LEFT(name,5)=LEFT(variable,5);
Or if you you want to use LIKE with a wildcard, you can do this:
SELECT * FROM items WHERE name LIKE CONCAT(LEFT(variable,5),'%')
A few more example in the Demo fiddle
Edit: The above solution is for MySQL/MariaDB because earlier the tag of this question have MySQL but it's also my fault for not recognizing OP description of the datatype VARCHAR2. I might as well just post a suggestion related to the rdbms.
So, my first suggestion there using LEFT() however Oracle don't have that function, therefore:
SELECT * FROM items WHERE SUBSTR(name,1,5)=SUBSTR(variable,1,5);
or using concatenation operator
SELECT * FROM items WHERE name LIKE SUBSTR(variable,1,5)||'%'
Demo fiddle

Postgres buitin function to escape special character

I have 2 columns that I want to compare based on Postgres SIMILAR TO notation, in the example below it is the ~*. Lets call the columns: COLUMN1 and COLUMN2.
It works on most of the rows, then after further investigation if the value of either column has "++" the query failed.
SELECT
CASE
WHEN 'gcc' ~* 'gcc++' THEN 1
ELSE 2
END
with the following message
ERROR: invalid regular expression: quantifier operand invalid
Obviously the gcc++ string above need to be escaped.
Question:
Is the there a Postgres built-in function in postgres that escape the column values so that the comparison can be made safer? I'm thinking something like below ...
SELECT
CASE
WHEN escapeMe(COLUMN1) ~* escapeMe(COLUMN2) THEN 1
ELSE 2
END
FROM TABLE_T1

Get rows that contain only certain characters

I want to get only those rows that contain ONLY certain characters in a column.
Let's say the column name is DATA.
I want to get all rows where in DATA are ONLY (must have all three conditions!):
Numeric characters (1 2 3 4 5 6 7 8 9 0)
Dash (-)
Comma (,)
For instance:
Value "10,20,20-30,30" IS OK
Value "10,20A,20-30,30Z" IS NOT OK
Value "30" IS NOT OK
Value "AAAA" IS NOT OK
Value "30-" IS NOT OK
Value "30," IS NOT OK
Value "-," IS NOT OK
Try patindex:
select * from(
select '10,20,20-30,30' txt union
select '10,20,20-30,40' txt union
select '10,20A,20-30,30Z' txt
)x
where patindex('%[^0-9,-]%', txt)=0
For you table, try like:
select
DATA
from
YourTable
where
patindex('%[^0-9,-]%', DATA)=0
As per your new edited question, the query should be like:
select
DATA
from
YourTable
where
PATINDEX('%[^0-9,-]%', DATA)=0 and
PATINDEX('%[0-9]%', LEFT(DATA, 1))=1 and
PATINDEX('%[0-9]%', RIGHT(DATA, 1))=1 and
PATINDEX('%[,-][-,]%', DATA)=0
Edit: Your question was edited, so this answer is no longer correct. I won't bother updating it since someone else already has updated theirs. This answer does not fulfil the condition that all three character types must be found.
You can use a LIKE expression for this, although it's slightly convoluted:
where data not like '%[^0123456789,!-]%' escape '!'
Explanation:
[^...] matches any character that is not in the ... part. % matches any number (including zero) of any character. So [^0123456789-,] is the set of characters that you want to disallow.
However: - is a special character inside of [], so we must escape it, which we do by using an escape character, and I've chosen !.
So, you match rows that do not contain (not like) any character that is not in your disallowed set.
Use option with PATINDEX and LIKE logic operator
SELECT *
FROM dbo.test70
WHERE PATINDEX('%[A-Z]%', DATA) = 0
AND PATINDEX('%[0-9]%', DATA) > 0
AND DATA LIKE '%-%'
AND DATA LIKE '%,%'
Demo on SQLFiddle
As already mentioned u can use a LIKE expression but it will only work with some minor modifications, otherwise too many rows will be filtered out.
SELECT * FROM X WHERE T NOT LIKE '%[^0-9!-,]%' ESCAPE '!'
see working example here:
http://sqlfiddle.com/#!3/474f5/6
edit:
to meet all 3 conditions:
SELECT *
FROM X
WHERE T LIKE '%[0-9]%'
AND T LIKE '%-%'
AND T LIKE '%,%'
see: http://sqlfiddle.com/#!3/86328/1
Maybe not the most beautiful but a working solution.

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