Update - Where Clause Using Text Not Responding As Expected - postgresql

I have a table that contains a column named 'time_source', with five potential values:
'model', 'country_city', 'region', 'update', and 'storage'
I've been attempting to make an update statement that will not update column date_destination if the time_source is 'model' like this:
update t_vessel_list_ballast
set date_destination = date_depart + voyage_time
where time_source not like 'model';
But it isn't working as i'd expect. instead, it's overwriting the date_destination even when 'model' is in the time_source. I tried something like this:
update t_vessel_list_ballast
set date_destination = date_depart + voyage_time
where time_source like 'country_city'
or time_source like 'region';
but i'm still getting the same results.
There is no other place in the model where this could be happening.
Why am i not getting the result i'm expecting? How do i solve this to get what I want?

I think with like, if you want to match rows (or not match rows like you do here), you'll also need %s on the pattern to show where to match any other characters. For example like '%match' will match any string ending with match, and like 'match%' will match strings starting with match. You can also combine them to match a string containing match with like '%match%'.

Related

I need to change the output of a query so that instead of it coming back as the abbreviation 'em' it says 'employee'. Tsql

I have the correct result coming back. I just need to convert 6 abbreviations in that result to their correct names. There are 20k names assigned to 1 of 6 abbreviated names.
I tried aliasing but that seems to only work for table names.
I tried doing a case statement but that didn't work.
You need to provide more details (like some sample input and output), but if you have data like EM100, and you want to make it EMPLOYEE 100, then you could use an expression such as:
CASE WHEN ColumnName like 'EM%' THEN 'EMPLOYEE ' + SUBSTRING (ColumnName,3,100)
WHEN ColumnName like 'RN%' THEN 'REGNURSE' + SUBSTRING (ColumnName,3,100)
else ColumnName END
But providing more details will help provide a more specific answer.

string match and remove each row in a postgresql

So in short:
I need to find all rows in the column Translation that begin with the letter M (M123 is one of the Prefixes) and then I need to remove the M123 and similar prefixes from all the rows.
For example, row 1 contains the following data:
M123 - This is translated from Spanish to English
And I need to remove the M123 - from the mentioned data. And this I need to do for the Translation column in every row in the table.
It's been a while since I actually did some SQL-Queries. So I tried a WHERE clause to find all the M prefixes but my query returns an empty search. Following is the query I am using atm:
SELECT Translation from Translation_Table where Translation like 'M';
I am a little bit confused right now. So any help is appreciated.
I sense that you might be wanting to do an update here, rather than a select:
UPDATE Translation_Table
SET Translation = REGEXP_REPLACE(Translation, 'M[0-9]+', '')
WHERE Translation ~ '^M[0-9]+';
addition to this answer following query will remove all occurence of M[any length of a number]
UPDATE Translation_Table
SET Translation = REGEXP_REPLACE(Translation, '[M[:digit:]]', '', 'g')
WHERE Translation ~ '.M[0-9]*';

PSQL Apply mapping to an array

I have an array of an enum type order_options that gets passed through to a function and it is pretty simple, it looks like this:
'{master_date, e_name}'
I have 6 possible values and the enum type looks like this:
create type order_options as enum ('master_date','e_name','r_data','n_count','creation_date','last_updated_on');
In the function however, I want to apply a mapping to these values to change the value of sort_params which is the array passed through.
For each enum, I have an 'alternative name' that I want to use in the order by clause of a subsequent select statement. For example:
'master_date' = o.master_date
'e_name' = d.e_name
and so on.
I've looked in to doing a replace whereby I loop through each element of the array and attempt a replace with each mapping but it gets pretty messy and complex.
So for example, this works for one individual mapping:
select array_replace('{master_date}'::text[],'master_date','o.master_date');
I'd like my sort_params to look like this after mapping:
'{o.master_date, d.e_name}'
Is there any easier way to do this?
For now, this is what I have:
select array_replace(sort_params_u,'master_date','o.master_date') into sort_params_u;
select array_replace(sort_params_u,'e_name','d.e_name') into sort_params_u;
etc

Postgres LIKE %% syntax errors

I have been trying to do a LIKE comparison in postgres but repeatedly receive an error telling me that the column "%#firstname%" doesn't exist.
I should clarify, this query is executed in a function, "#firstname" is the parameter passed into the function.
The relevant section of the query is below:
WHERE u."firstname" LIKE "%#firstname%"
I do not want an exact comparison which is why I am trying to add the %% to the query. It works just fine without them for exact queries. Whenever, I add the % then it assumes that they are part of the variable name and subsequently can't find it.
I have also tried the following:
'%"#firstname"%' which results in an empty array being returned even though it should have matched
"%'#firstname'%" which results in error: column "%'#filter'%" does not exist
%"#firstname"% which results in error: column "%'#filter'%" does not exist
If "#firstname" is a parameter you need something like:
WHERE u.firstname LIKE concat('%', "#firstname", '%');

JQuery Wildcard for using atttributes in selectors

I've research this topic extensibly and I'm asking as a last resort before assuming that there is no wildcard for what I want to do.
I need to pull up all the text input elements from the document and add it to an array. However, I only want to add the input elements that have an id.
I know you can use the \S* wildcard when using an id selector such as $(#\S*), however I can't use this because I need to filter the results by text type only as well, so I searching by attribute.
I currently have this:
values_inputs = $("input[type='text'][id^='a']");
This works how I want it to but it brings back only the text input elements that start with an 'a'. I want to get all the text input elements with an 'id' of anything.
I can't use:
values_inputs = $("input[type='text'][id^='']"); //or
values_inputs = $("input[type='text'][id^='*']"); //or
values_inputs = $("input[type='text'][id^='\\S*']"); //or
values_inputs = $("input[type='text'][id^=\\S*]");
//I either get no values returned or a syntax error for these
I guess I'm just looking for the equivalent of * in SQL for JQuery attribute selectors.
Is there no such thing, or am I just approaching this problem the wrong way?
Actually, it's quite simple:
var values_inputs = $("input[type=text][id]");
Your logic is a bit ambiguous. I believe you don't want elements with any id, but rather elements where id does not equal an empty string. Use this.
values_inputs = $("input[type='text']")
.filter(function() {
return this.id != '';
});
Try changing your selector to:
$("input[type='text'][id]")
I figured out another way to use wild cards very simply. This helped me a lot so I thought I'd share it.
You can use attribute wildcards in the selectors in the following way to emulate the use of '*'. Let's say you have dynamically generated form in which elements are created with the same naming convention except for dynamically changing digits representing the index:
id='part_x_name' //where x represents a digit
If you want to retrieve only the text input ones that have certain parts of the id name and element type you can do the following:
var inputs = $("input[type='text'][id^='part_'][id$='_name']");
and voila, it will retrieve all the text input elements that have "part_" in the beginning of the id string and "_name" at the end of the string. If you have something like
id='part_x_name_y' // again x and y representing digits
you could do:
var inputs = $("input[type='text'][id^='part_'][id*='_name_']"); //the *= operator means that it will retrieve this part of the string from anywhere where it appears in the string.
Depending on what the names of other id's are it may start to get a little trickier if other element id's have similar naming conventions in your document. You may have to get a little more creative in specifying your wildcards. In most common cases this will be enough to get what you need.