Skip to content

Instantly share code, notes, and snippets.

-- SPLIT_STR MySQL Function
-- from http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
@heluvaguy
heluvaguy / Mysql : Dense_rank
Created October 7, 2013 15:09
Implement Dense Rank In Mysql
SELECT id,
@rnk:=IF(@preval <=> score, @rnk, @row + 1) AS dns_rnk,
@row:= @row+1 AS rnk,
@preval:=score as score
FROM table
# be careful for NULL handling.
# if all the values of score column are null, then dns_rank will zero.
# please set proper initial value for @preval based on your data.
JOIN (SELECT @rnk := 0, @preval :=null, @row := 0) r
ORDER BY score DESC
@heluvaguy
heluvaguy / Mysql:Rank
Created October 7, 2013 15:10
Implement Rank in Mysql
SELECT cm_id,
@rnk:=IF(@preval <=> column, @rnk, @rnk + 1) AS rank,
@preval:=social_profile_score_absolute AS social_profile_score_absolute
FROM table
JOIN (SELECT @rnk := 1, @preval :=0) r
ORDER BY social_profile_score_absolute ASC
@heluvaguy
heluvaguy / Psql: SHOW CURRENT QUERIES
Created October 17, 2013 05:26
SHOW CURRENT QUERIES
SELECT datname,procpid,current_query FROM pg_stat_activity
@heluvaguy
heluvaguy / PSQL: KILL QUERY
Created October 17, 2013 05:34
kill a postgresql query
SELECT pg_cancel_backend(pid of the process)
@heluvaguy
heluvaguy / LINUX: Change from one extension to another
Created October 26, 2013 09:56
Change from one extension to another
for f in *.php4; do mv $f `basename $f .php4`.php; done;Add (append) an extension to all files
@heluvaguy
heluvaguy / LINUX: Add (append) an extension to all files
Created October 26, 2013 09:56
Add (append) an extension to all files
for f in *; do mv $f `basename $f `.txt; done;
@heluvaguy
heluvaguy / LINUX:Remove (delete) an extension from all files
Created October 26, 2013 09:57
Remove (delete) an extension from all files
for f in *.txt; do mv $f `basename $f .txt`; done;
@heluvaguy
heluvaguy / Angular:Clock Directive.js
Created November 9, 2013 10:43
Create Clock Using AngularJs
var myApp = angular.module('myApp', []);
myApp.directive('clock', function($timeout, dateFilter){
return function(scope, element, attrs){
var timeoutId; // timeoutId, so that we can cancel the time updates
// schedule update in one second
function updateLater() {
// save the timeoutId for canceling
timeoutId = $timeout(function() {
controllers.controller('MainCtrl', function($scope, $location, Facebook, $rootScope, $http, $location, Upload, Auth, User, Question, Category, Serie, Record, Location, Popup, Process, Card, Question) {
$scope.$on('authLoaded', function() {
$scope.isExpert($scope.main.serieId);
$scope.isMember($scope.main.serieId);
});
$scope.loadAuth = function() {
Auth.load().success(function(data) {
$scope.main.user = data.user;
$scope.$broadcast("authLoaded");