Tables
Create table if not exists
Problem: Create table named "Table 1" with 3 columns ("id" - key, "First Name" - text, "Age" - integer), only if this table does not exist.
MySQL
CREATE TABLE IF NOT EXISTS `Table 1` (
id INTEGER NOT NULL,
`First Name` TEXT,
Age INTEGER,
PRIMARY KEY (id)
);
SQL Server
IF NOT EXISTS (SELECT * FROM SysObjects WHERE NAME = 'Table 1' AND XTYPE = 'U')
CREATE TABLE [Table 1] (
id int PRIMARY KEY NOT NULL,
[First name] TEXT,
Age INT
)
GO
SQLite
CREATE TABLE IF NOT EXISTS [Table 1] (
id INT PRIMARY KEY NOT NULL,
'First Name' TEXT,
Age INT
);
PostgreSQL
CREATE TABLE IF NOT EXISTS "Table 1" (
id BIGSERIAL PRIMARY KEY,
"First Name" TEXT,
Age INT
);
Strings
Split
Problem: Get string part delimited by same characters or characters sequences. For example, get "Ipsum
" from "Lorem Ipsum Dolor
", or "Dolor
" from "Lorem////Ipsum////Dolor
".
There are no standard string
split function in MySQL. Let's write our own!
MySQL
DROP FUNCTION IF EXISTS STR_SPLIT;
DELIMITER $
CREATE FUNCTION STR_SPLIT (inp TEXT, del TEXT, ind INT) RETURNS TEXT DETERMINISTIC
BEGIN
SET @right_part = SUBSTRING_INDEX(inp, del, ind);
RETURN SUBSTRING_INDEX(@right_part, del, -1);
END$
DELIMITER ;
SELECT STR_SPLIT("Lorem Ipsum Dolor", " ", 1);
SELECT STR_SPLIT("Lorem Ipsum Dolor", " ", 2);
SELECT STR_SPLIT("Lorem Ipsum Dolor", " ", 3);
SELECT STR_SPLIT("Lorem////Ipsum////Dolor", "////", 1);
SELECT STR_SPLIT("Lorem////Ipsum////Dolor", "////", 2);
SELECT STR_SPLIT("Lorem////Ipsum////Dolor", "////", 3);
Split (alternative)
MySQL
DROP FUNCTION IF EXISTS STR_SPLIT;
DELIMITER $
CREATE FUNCTION STR_SPLIT (inp TEXT, del TEXT, ind INT) RETURNS TEXT DETERMINISTIC
BEGIN
SET @s = inp;
SET @result= "";
SET @i = 1;
REPEAT
IF (LOCATE(del, @s) = 0) THEN
RETURN @s;
END IF;
SET @del_ind = LOCATE(del, @s);
SET @result = SUBSTRING(@s, 1, @del_ind - 1);
SET @s = SUBSTRING(@s, @del_ind + LENGTH(del), LENGTH(@s) - @del_ind);
SET @i = @i + 1;
UNTIL @i > ind END REPEAT;
RETURN @result;
END$
DELIMITER ;
SELECT STR_SPLIT("Lorem Ipsum Dolor", " ", 1);
SELECT STR_SPLIT("Lorem Ipsum Dolor", " ", 2);
SELECT STR_SPLIT("Lorem Ipsum Dolor", " ", 3);
SELECT STR_SPLIT("Lorem////Ipsum////Dolor", "////", 1);
SELECT STR_SPLIT("Lorem////Ipsum////Dolor", "////", 2);
SELECT STR_SPLIT("Lorem////Ipsum////Dolor", "////", 3);
Remove substring
Problem: Remove substring by its start and length. For example, get "Lom ipsum
" from "Lorem ipsum
".
MySQL
DROP FUNCTION IF EXISTS STR_REMOVE;
CREATE FUNCTION STR_REMOVE (inp TEXT, start INT, length INT) RETURNS TEXT DETERMINISTIC
RETURN CONCAT(SUBSTRING(inp, 1, start - 1), SUBSTRING(inp, start + length, LENGTH(inp) - (start - 1 + length)));
SELECT STR_REMOVE("Lorem ipsum", 3, 2);
Date and Time
Get current date and time
MySQL
SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();
SELECT UNIX_TIMESTAMP();
SELECT UTC_TIMESTAMP();
SELECT UTC_DATE();
SELECT UTC_TIME();
SQL Server
SELECT GETDATE();
SELECT CAST(GETDATE() AS DATE);
SELECT CAST(GETDATE() AS TIME);
SELECT GETUTCDATE();
SELECT CAST (GETUTCDATE() AS DATE);
SELECT CAST (GETUTCDATE() AS TIME);
SQLite
SELECT datetime(CURRENT_TIMESTAMP, 'localtime')
SELECT CURRENT_TIMESTAMP;
PostgreSQL
SELECT current_timestamp;
SELECT current_date;
SELECT current_time;
Add or subtract some value from date or time
MySQL
SELECT DATE_ADD(CURDATE(), INTERVAL 1 DAY);
SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY);
SELECT DATE_ADD(CURDATE(), INTERVAL 1 MONTH);
SELECT DATE_SUB(CURDATE(), INTERVAL 1 MONTH);
SELECT DATE_ADD(CURDATE(), INTERVAL 1 YEAR);
SELECT DATE_SUB(CURDATE(), INTERVAL 1 YEAR);
SELECT ADDTIME(CURTIME(), '01:00');
SELECT SUBTIME(CURTIME(), '01:00');
SELECT ADDTIME(CURTIME(), '00:01');
SELECT SUBTIME(CURTIME(), '00:01');
SELECT ADDTIME(CURTIME(), '00:00:01');
SELECT SUBTIME(CURTIME(), '00:00:01');
Calculate two dates or times differences
MySQL
SELECT DATEDIFF('2014-06-07', '2014-07-09');
SELECT DATEDIFF('2014-07-09', '2014-06-07');
SELECT TIMEDIFF('05:30:40', '02:00:00');
SELECT TIMEDIFF('02:00:00', '05:30:40');