
Context menu must be done on File's Owner:
create or replace function DynamicSql(p_fields varchar)
returns table
(
"YouKnowMyName" varchar,
"LookupTheNumber" int
) as
$$
declare
sql varchar;
begin
sql = 'select ' || p_fields || ' from employees';
return query execute sql;
end;
$$ language 'plpgsql';
select * from DynamicSql(' "LastName", "EmployeeID" ' );
select * from DynamicSql(' "FirstName", "EmployeeID" ' );
testdb-#
with p as
(
select *, firstname || ' ' || lastname as fullname
from person
)
select * from p;
person_id | lastname | firstname | nickname | favorite_number | fullname
-----------+-----------+--------------+----------+-----------------+----------------------
1 | lennon | john winston | john | | john winston lennon
2 | mccartney | james paul | paul | | james paul mccartney
3 | harrison | george | | | george harrison
4 | starr | richard | ringo | 10 | richard starr
(4 rows)
person_id | field | value
-----------+-----------------+----------------------
1 | favorite_number |
1 | firstname | john winston
1 | fullname | john winston lennon
1 | lastname | lennon
1 | nickname | john
1 | person_id | 1
2 | favorite_number |
2 | firstname | james paul
2 | fullname | james paul mccartney
2 | lastname | mccartney
2 | nickname | paul
2 | person_id | 2
3 | favorite_number |
3 | firstname | george
3 | fullname | george harrison
3 | lastname | harrison
3 | nickname |
3 | person_id | 3
4 | favorite_number | 10
4 | firstname | richard
4 | fullname | richard starr
4 | lastname | starr
4 | nickname | ringo
4 | person_id | 4
(24 rows)
with p as
(
select *, firstname || ' ' || lastname as fullname
from person
)
select
person_id,
'firstname' as field,
firstname as value
from p
union all
select
person_id,
'firstname' as field,
firstname as value
from p
union all
select
person_id,
'lastname' as field,
firstname as value
from p
union all
select
person_id,
'nickname' as field,
nickname as value
from p
union all
select
person_id,
'fullname' as field,
fullname as value
from p
union all
select
person_id,
'favorite_number' as field,
favorite_number::text as value
from p
order by person_id, field
with p as
(
select *, firstname || ' ' || lastname as fullname
from person
)
select
person_id,
skeys(hstore(p)) as field, svals(hstore(p)) as value
from p
order by person_id, field
with p as
(
select *, firstname || ' ' || lastname as fullname
from person
)
select
p.person_id,
unnest(array['person_id', 'favorite_number', 'firstname','fullname','lastname', 'nickname']) as field_label,
unnest(array[cast(p.person_id as text), cast(p.favorite_number as text), p.firstname, p.fullname, p.lastname, p.nickname]) as field_value
from p
order by p.person_id, field_label
create table person
(
person_id serial not null primary key,
lastname text not null,
firstname text not null,
nickname text null,
favorite_number int null
);
insert into person(lastname,firstname,nickname, favorite_number) values
('lennon','john winston','john',default),
('mccartney','james paul','paul',default),
('harrison','george',default,default),
('starr','richard','ringo', 10);
<input type="text" ng-model="untaintedNumber"/>
<input type="text" ng-model="untaintedNumber" numeric decimal-places="decPlaces" ng-change="showInLog()">
module.directive('numeric', function($filter, $locale) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
var decN = scope.$eval(attr.decimalPlaces); // this is the decimal-places attribute
// http://stackoverflow.com/questions/10454518/javascript-how-to-retrieve-the-number-of-decimals-of-a-string-number
function theDecimalPlaces(num) {
var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
if (!match) { return 0; }
return Math.max(
0,
// Number of digits right of decimal point.
(match[1] ? match[1].length : 0)
// Adjust for scientific notation.
- (match[2] ? +match[2] : 0));
}
function fromUser(text) {
var x = text.replaceAll($locale.NUMBER_FORMATS.GROUP_SEP, '');
var y = x.replaceAll($locale.NUMBER_FORMATS.DECIMAL_SEP, '.');
return Number(y); // return a model-centric value from user input y
}
function toUser(n) {
return $filter('number')(n, decN); // locale-aware formatting
}
ngModel.$parsers.push(fromUser);
ngModel.$formatters.push(toUser);
element.bind('blur', function() {
element.val(toUser(ngModel.$modelValue));
});
element.bind('focus', function() {
var n = ngModel.$modelValue;
var formattedN = $filter('number')(n, theDecimalPlaces(n));
element.val(formattedN);
});
} // link
}; // return
}); // module
String.prototype.replaceAll = function(stringToFind,stringToReplace){
if (stringToFind === stringToReplace) return this;
var temp = this;
var index = temp.indexOf(stringToFind);
while(index != -1){
temp = temp.replace(stringToFind,stringToReplace);
index = temp.indexOf(stringToFind);
}
return temp;
};
function Demo($scope) {
$scope.decPlaces = 2;
$scope.untaintedNumber = 1234567.8912;
$scope.changeValue = function() {
// The model didn't change to string type, hence we can do business as usual with numbers.
// The proof that it doesn't even change to string type is we don't even need
// to use parseFloat on the untaintedNumber when adding a 7 on it.
// Otherwise if the model's type mutated to string type,
// the plus operator will be interpreted as concatenation operator: http://jsfiddle.net/vuYZp/
// Luckily we are using AngularJS :-)
$scope.untaintedNumber = $scope.untaintedNumber + 7;
// contrast that with jQuery where everything are string:
// you need to call both $('elem').val() and Globalize's parseFloat,
// then to set the value back, you need to call Globalize's format.
/*
var floatValue = Globalize.parseFloat($('#uxInput').val());
floatValue = floatValue * 2;
var strValue = Globalize.format(floatValue, "n4");
$('#uxInput').val(strValue);
*/
};
$scope.showInLog = function() {
console.log($scope.untaintedNumber);
};
}
create table tosses
(
attempt int identity(1,1) primary key,
fate char(1),
constraint ck_tosses check(fate in ('H','T') or fate is null)
);
insert into tosses(fate) values
('H'),
('H'),
(null),
('T'),
(null),
('H'),
('T');
select
attempt,
fate,
case fate
when 'H' then 'Head'
when 'T' then 'Tail'
when null then 'Fate not yet determined'
else 'http://9gag.com/gag/4380545'
end as fate_result
from tosses
| ATTEMPT | FATE | FATE_RESULT | -------------------------------------------------- | 1 | H | Head | | 2 | H | Head | | 3 | (null) | http://9gag.com/gag/4380545 | | 4 | T | Tail | | 5 | (null) | http://9gag.com/gag/4380545 | | 6 | H | Head | | 7 | T | Tail |
select
attempt,
fate,
case when fate is null then 'Fate not yet determined'
else
case fate
when 'H' then 'Head'
when 'T' then 'Tail'
else 'http://9gag.com/gag/4380545'
end
end as fate_result
from tosses
| ATTEMPT | FATE | FATE_RESULT | ---------------------------------------------- | 1 | H | Head | | 2 | H | Head | | 3 | (null) | Fate not yet determined | | 4 | T | Tail | | 5 | (null) | Fate not yet determined | | 6 | H | Head | | 7 | T | Tail |
select
attempt,
fate,
case
when fate = 'H' then 'Head'
when fate = 'T' then 'Tail'
when fate is null then 'Fate not yet determined'
else 'http://9gag.com/gag/4380545'
end as fate_result
from tosses
| ATTEMPT | FATE | FATE_RESULT | ---------------------------------------------- | 1 | H | Head | | 2 | H | Head | | 3 | (null) | Fate not yet determined | | 4 | T | Tail | | 5 | (null) | Fate not yet determined | | 6 | H | Head | | 7 | T | Tail |