I have a table with rows and where one field is a bit-value with 7 digits.
Suppose I have a procedure where I want to select all rows where this bit field equals '0101010', this is easily done by select * where .... and so on.
But: how do I do if I want to allow one/multiple digits of the digits to be either 1 Or 0, i.e I want to get all rows where the bitfield has an entry on the form 1001*1* where the * can be either 1 or 0. So, in this case I would like all entries where the bit field is 1001010, 1001011, 1001110 or 1001111.
select * from TABLE where bit_field in (1001010, 1001011, 1001110, 1001111) would probably work in this example, but if I want to use only the string '1001*1*' as input to the procedure, what then?
.
Any help is very appreciated.
Thanks,
Niklas
Edit: I've tried this: select * from table where field like bit'\\\0'; for getting all entries of the form **0, but that didn't work...
Edit2: It turned out it vas a bit-field, not binary... problem still remain though.
Not a direct answer to your question, per se', but an alternative approach. You mentioned that you didn't want to convert to individual columns because of legacy code. If you do want individual columns and the only thing holding you back is the legacy code, consider the following.
You could add columns for the options and use insert/update triggers to populate them OR you could create a view that splits the bits into separate columns. For new development, you can code to the new columns. Over time, as you modify legacy code you can change it to the new approach. When all the "read" legacy code has been changed, the last step is to change the "write" code to use the new columns rather than the bit column and remove the triggers.
I have a SQL Fiddle demonstrating this here. Note that I only included an insert trigger for brevity.
Related
I'm an intern in a property rental company. I'm in charge of developping the CRM under Symfony.
So, ones of my entities are properties (houses) and their availabilities. See the table structure below.
The problem I'm facing for now, is that the availabilities had been defined for each day (e.g. 28/01, 29/01, 30/01) instead of being defined for a range of day (e.g. 28/01 -> 30/01). So, the table is really heavy (~710 000 rows). Furthermore, before we changed the way of editing an availability, it created a new row for a same date instead of editing it. So, there are a lot of duplications in this table.
What I want, is to lighten the DB by keeping only the rows which have the max value in date_modif_availabilities for the same date_availabilities and id_properties.
For example, if I have these rows (availabilities_duplications):
I only want to keep the row with the latest modif like this (availabilities_keep_max_value) :
The thing is, I don't know enough the SQL language. I'm able to write few basics scripts but not complex subqueries. Even with code samples that I found.
Thank you in advance for your help.
You could select the elements for which no element with greater modified date exists.
Something like this:
SELECT avl1.*
FROM availabilities avl1
WHERE NOT EXISTS (SELECT *
FROM availabilities avl2
WHERE avl1.date_availabilities = avl2.date_availabilities
AND avl1.id_properties = avl2.id_properties
AND avl2.date_modif_availabilities > avl1.date_modif_availabilities);
This of course has the pre-condition that the combination of the three columns date_availabilities, id_properties and date_modif_availabilities is unique.
Furthermore, it seems that all columns (except the PK) may be NULL. Looks kinda odd to me.
You can use subquery :
select t.*
from table t
where id_availabilities = (select t1.id_availabilities
from table t1
where t1.id_properties = t.id_properties and
t1.date_availabilities = t.date_availabilities
order by t1.date_modif_availabilities desc
limit 1
);
However, if you have concern about the performance, then you want index on (id_properties, date_availabilities and id_availabilities).
I have a table with ~200,000 rows. There are three different phone number columns and the data in them is not all formatted the same. I'd like to remove any value that is not a number and update every cell.
For instance, (412)641-5892 becomes 4126415892.
I found this STRIP_NON_DIGIT() function here. I can use that in my SQL queries and it works properly, but it takes a minute to return a result. I'd like to run a mass UPDATE across the entire table, but not sure what the syntax is for that.
Something like this is what I'm going for.
UPDATE leads
SET phone = STRIP_NON_DIGIT(phone),
mobile_phone = STRIP_NON_DIGIT(mobile_phone),
home_phone = STRIP_NON_DIGIT(home_phone)
Turns out the answer was the pseudo code that I wrote!
i have the following part of query:
SUM(COALESCE(user_a, user_b)) AS income_adsense
Now, i have a html table in my web app where i present the data from this query.
problem is i want to mark data in one color if answer is from col user_a and different color if answer is from user_b col.
is there a way to achieve that in my query itself? (some sort of flag maybe?)
.
right now the only solution i have is to return all col's and work with the data on the client side but i am wondering if there's a cleaner/best practice solution.
guess it's worth mentioning i don't want to change the table structure.
Well, to make sense of the data I would do something like:
CASE
WHEN SUM(user_a) > SUM(user_b)
THEN 'User_A'
ELSE 'User_B'
END [Most of the data comes from]
You could also have two separate SUM() columns to make sense of this and compare the sum of values in your application.
SUM(user_a) [User_A Score Weight]
, SUM(user_b) [User_B Score Weight]
I am attempting to maintain and fix a horribly out-of-date CRM designed by an ex-employee ~4-5 years ago in Access 2007. I have brought it into Access 2013 and fixed a ton of stuff up, but I am still running into many problems.
I spent a good 4 hours today attempting to figure out why certain values didn't line up. These values were being pulled from a SELECT statement on a Combo Box over a stored Query which simply returns a table with a few extra rows. Great.
However this value (a number) doesn't appear to correlate with what we expect. I enter in one value, save the ticket, and a completely different value gets stored into the table. Opening up the ticket, I see the value that I expect. Digging deeper, I found the following difference:
Set value_1 = Me.RegistrationID // What's being stored in the table
Set value_2 = Me.RegistrationID.Column(0) // What we expect
Surprise surprise! This is a Combo Box and some value is being stored in the table. The Control Source is "RegistrationID" and the Row Source is the query in question.
However I do not know what it is! This specific value correlating to the Combo Box appears to pull the correct data when we later open the tickets. However I have a strong feeling that this could be why many tickets from before one of the rows was deleted all appear to have invalid RegistrationID's.
How badly can this break?
How easily can we correct tens of thousands of tickets?
How can I fix this to store the correct value?
This is what I expect is happening.
Your combo box row source is based on a Select query which returns and displays multiple rows. For example:
Select RegistrationID, CustomerID, CustomerName From MyTable;
The Control Source for the combo box is bound to RegistrationID which is part of the Forms Record Source.
The issue is the bound column. If we set the bound column in our example to 1, then we get the behavior your are describing with:
Set value_1 = Me.RegistrationID - Set's value to CustomerID (may appear correct)
Set value_2 = Me.RegistrationID.Column(0) - position 0 from our query (RegistrationID)
Further building on our query example, you can say:
Me.TextBox1 = Me.RegistrationID.Column(0) - RegistrationID
Me.TextBox2 = Me.RegistrationID.Column(1) - CustomerID
Me.TextBox3 = Me.RegistrationID.Column(2) - CustomerName
The RegistrationID is what normally should be stored in the table.
As long as your form shows any values that directly relate to this RegistrationID you're fine.
I would start by checking to see under the format setting to see if column widths are set properly and I would also check under the data section to see if the bound column is correct. I might also throw in an after update macro/vba sub routine that saves the record. Hope this helps.
I was able to display data from my MySQL table using this code:
datardr = cmd.ExecuteReader
If datardr.HasRows Then
datardr.Read()
tb_lname.Text = datardr("SURNAME")
tb_fname.Text = datardr("GIVEN")
tb_mname.Text = datardr("MID")
tb_mi.Text = datardr("MIDDLE")
tb_app.Text = datardr("APPELLATION")
tb_prefix.Text = datardr("PREFIX")
tb_sex.Text = datardr("SEX")
tb_status.Text = datardr("STATUS")
End If
However, I noticed that it's not displaying all data coming from these fields. I can only view the SURNAME, GIVEN, MID and MIDDLE but the others are not displayed.. I have double checked my database fields and I'm sure that they're the same and without special characters or whitespaces.
Please help. Thanks!
Here is the exact code that I have => VB2010 and MySQL Code
Alright, here's another answer for you.
I think it's because of your SQL statement in line 21.
I assume you are selecting ONE record (am I right?), so that you can insert the resultant fields into the text boxes. And, you order the result with SURNAME.
Did you double check whether there are already data inside the masterlist table? Especially check, if you already entered the data in every field in every row.
In line 30, you called datardr.Read() method, so the DataReader object datardr will read the first record line it encountered in the result of the sql statement.I think only the four fields of the first record, SURNAME, GIVEN, MID and MIDDLE has data values, and any other fields contain null values. So, you only got these FOUR values appeared inside the text boxes, and any other fields appeared to be blank.
I THINK IT MIGHT BE THE MAIN PROBLEM. Just check whether the data you wanted to be appeared already existed in the database table. OK!
And another suggestion. Don't you think you might need WHERE clause in your SQL statement? Well, you want to display only one record, don't you?
WISH YOU BEST LUCK!!! :-)
I think you better check the sql statement that passed into the command object, cmd.
Maybe you didn't select the entire record with select *.
And, one more recommendation.
If datardr is the DataReader, I highly recommend you NOT to use it. It cause much problems than it serves.
The more flexible approach is to use just the DataTable.
The command object has ExecuteNonQuery method that returns the DataTable object.
It is more flexible and much more easier to use than DataReader. Trust Me...! ;-)