a is value entered by user.
cur.execute('SELECT * FROM table WHERE Column-name LIKE ?', ('%{}%'.format(a,))
The database must show value matching with entered value. It shows for one column only. I want to retrieve record for more than one column. Need to use OR operator but not sure the syntax. Could anyone plz help me.
Just expand the where clause with more conditions, separated by OR - accordingly, you need to repeat the bind parameters:
cur.execute('SELECT * FROM table WHERE col1 LIKE ? OR col2 LIKE ?', ('%{}%'.format(a, a))
Related
I have a column called TAG_
Data in the TAG_ column could like the below:
STV-123456
TV-12456
ME-666666
I want to create two computed columns
One that shows the first part of TAG_ before the hyphen
STV
TV
ME
One that shows the second part of TAG_ after the hyphen
123456
12456
666666
This shouldn't be hard but the light bulb is not on yet. Please help.
try this:
SELECT SUBSTRING(TAG_ ,0,CHARINDEX('-',TAG_ ,0)) AS before,
SUBSTRING(TAG_ ,CHARINDEX('-',TAG_ ,0)+1,LEN(TAG_ )) AS after from testtable
and the result:
Hope this helps!
Example for MySQL, syntax is likely different for other vendors:
create table t
( tag_ text not null
, fst text generated always as (substr(tag_, 1, locate('-', tag_)-1)) stored
, snd text generated always as (substr(tag_, locate('-', tag_)+1)) stored
);
Fiddle
i am trying to use a if formula in power query. For example, if the Column contains “guy” then value is male and the false value is “female”. I tried different ways and I can’t find the right formula to use in power query. Can anyone help me please?
If you are entering this into the Add Custom Column dialog, something like (for case-insensitive):
= if Text.Contains([Column],"guy", Comparer.OrdinalIgnoreCase) then "male" else "female"
It sounds like you are wanting to conditionally replace values in a column, based on the existing values in that column? If so, you can use the Table.ReplaceValue function in Power Query:
= Table.ReplaceValue(Source,
each [Gender],
each if [Gender] = "guy" then "male" else "female",
Replacer.ReplaceText,{"Gender"})
That will change all values of "guy" to "male', and ALL other values to "female", as you stated.
You can also leave values in place that don't meet the criteria, by simply referencing the column name instead of a specifying a new value:
= Table.ReplaceValue(Source,
each [Gender],
each if [Gender] = "guy" then "male" else [Gender],
Replacer.ReplaceText,{"Gender"})
Create a table with a column called Gender, and load it to power query. Right-click on the column header and choose Replace Values to get the UI to build your statement for you, then replace the generated code with the above modification(s) and apply to your actual requirements. The key is using the each expression to tell Power Query to test at the row value level. If you omit each, you'll see the error:
"Expression.Error: There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?"
= Table.AddColumn(#"Changed Type","ColumnName",each if[Column] ="""Guy""" then"""Male""" else"""Female""")
I have a number of variables (count varies each time script is executed). I need to insert these variables into a sql statement. For example, let us assume the sql statement to be:
select * from customer where customer_id in (...);
What comes in those dotted lines will be my variables. If I knew the count of variables, I could have done something like:
query='''create table xyz as
select * from customer where customer_id in (?,?);'''
cursor.execute(query,var1,var2)
query='select * from xyz;'
xyz_details=pd.read_sql(query,con)
But since the count varies, I cannot keep a fixed number of '?' in my query. It could be 2 in one run and 4 in another. The script does store the count in a variable 'cnt'. So if cnt is 4, i have var1,var2,var3,var4 as arguments, and i will need to have 4 '?' in my query.
However, since these counts keep changing, I cannot fix a definite value in my query and I am looking on how to proceed.
Any comments are appreciated!
Maybe you can try something like that ?
list_args = [var1, var2, ..., varn]
query='''create table xyz as
select * from customer where customer_id in ({});'''.format(','.join('?' * len(list_args)))
cursor.execute(query,var1,var2)
query='select * from xyz;'
xyz_details=pd.read_sql(query,con)
I want to know how to take query results and change them in to human talk.
For example:
Select backup_set.id, backup_set.status, backup_set.type
from backset.table
Then take the backup_set.type result, which is usually a number such as 1,2,3,4,5 and change it to something like SUSPENDED, SCHEDULED etc... But I dont want to change the data in the table just the output.
This can be done using a CASE statement.
Select backup_set.id, backup_set.status,
case backup_set.type
when 1 then 'SUSPENDED'
when 2 then 'SCHEDULED'
else 'UNKNOWN'
end as "type"
from backset.table
But it would be better if you stored that information in another table and make the type column a foreign key to that lookup table. Then you can use a simple join to retrieve the description.
Edit
If you want to replace UNKNOWN with the actual numeric value, you just need to put that into the ELSE part. You need to cast the number to a text value though:
Select backup_set.id, backup_set.status,
case backup_set.type
when 1 then 'SUSPENDED'
when 2 then 'SCHEDULED'
else backup_set.type::text
end as "type"
from backset.table
Short version: Is it possible to query for all timeuuid columns corresponding to a particular date?
More details:
I have a table defined as follows:
CREATE TABLE timetest(
key uuid,
activation_time timeuuid,
value text,
PRIMARY KEY(key,activation_time)
);
I have populated this with a single row, as follows (f0532ef0-2a15-11e3-b292-51843b245f21 is a timeuuid corresponding to the date 2013-09-30 22:19:06+0100):
insert into timetest (key, activation_time, value) VALUES (7daecb80-29b0-11e3-92ec-e291eb9d325e, f0532ef0-2a15-11e3-b292-51843b245f21, 'some value');
And I can query for that row as follows:
select activation_time,dateof(activation_time) from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e
which results in the following (using cqlsh)
activation_time | dateof(activation_time)
--------------------------------------+--------------------------
f0532ef0-2a15-11e3-b292-51843b245f21 | 2013-09-30 22:19:06+0100
Now lets assume there's a lot of data in my table and I want to retrieve all rows where activation_time corresponds to a particular date, say 2013-09-30 22:19:06+0100.
I would have expected to be able to query for the range of all timeuuids between minTimeuuid('2013-09-30 22:19:06+0100') and maxTimeuuid('2013-09-30 22:19:06+0100') but this doesn't seem possible (the following query returns zero rows):
select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time>minTimeuuid('2013-09-30 22:19:06+0100') and activation_time<=maxTimeuuid('2013-09-30 22:19:06+0100');
It seems I need to use a hack whereby I increment the second date in my query (by a second) to catch the row(s), i.e.,
select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time>minTimeuuid('2013-09-30 22:19:06+0100') and activation_time<=maxTimeuuid('2013-09-30 22:19:07+0100');
This feels wrong. Am I missing something? Is there a cleaner way to do this?
The CQL documentation discusses timeuuid functions but it's pretty short on gte/lte expressions with timeuuids, beyond:
The min/maxTimeuuid example selects all rows where the timeuuid column, t, is strictly later than 2013-01-01 00:05+0000 but strictly earlier than 2013-02-02 10:00+0000. The t >= maxTimeuuid('2013-01-01 00:05+0000') does not select a timeuuid generated exactly at 2013-01-01 00:05+0000 and is essentially equivalent to t > maxTimeuuid('2013-01-01 00:05+0000').
p.s. the following query also returns zero rows:
select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time<=maxTimeuuid('2013-09-30 22:19:06+0100');
and the following query returns the row(s):
select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time>minTimeuuid('2013-09-30 22:19:06+0100');
I'm sure the problem is that cqlsh does not display milliseconds for your timestamps
So the real timestamp is something like '2013-09-30 22:19:06.123+0100'
When you call maxTimeuuid('2013-09-30 22:19:06+0100') as milliseconds are missing, zero is assumed so it is the same as calling maxTimeuuid('2013-09-30 22:19:06.000+0100')
And as 22:19:06.123 > 22:19:06.000 that causes record to be filtered out.
Not directly related to answer but as an additional addon to #dimas answer.
cqlsh (version 5.0.1) seem to show the miliseconds now
system.dateof(id)
---------------------------------
2016-06-03 02:42:09.990000+0000
2016-05-28 17:07:30.244000+0000