Wednesday, 15 March 2017

SQL String & Date Functions


SQL - String Functions


SQL string functions are used primarily for string manipulation. The following table details the important string functions:
 ASCII(str)
Returns the numeric value of the leftmost character of the string str. Returns 0 if str is the empty string. Returns NULL if str is NULL. ASCII() works for characters with numeric values from 0 to 255.
>SELECT ASCII('4');
52
>SELECT ASCII('2');
50
BIN(N)
Returns a string representation of the binary value of N, where N is a longlong (BIGINT) number. This is equivalent to CONV(N,10,2). Returns NULL if N is NULL.
>SELECT BIN(13);
1101
>SELECT BIN(12);
1100

BIT_LENGTH(str)
Returns the length of the string str in bits.
>SELECT BIT_LENGTH('text');
32
>SELECT BIT_LENGTH('texte');
40
>SELECT BIT_LENGTH('texter');
48


CHAR(N,... [USING charset_name])
CHAR() interprets each argument N as an integer and returns a string consisting of the characters given by the code values of those integers. NULL values are skipped.
SELECT CHAR(77,121,83,81,'76');
MySQL
>SELECT CHAR(65,97,83,82,'86');
AaSRV
ΓΌ  (A to Z) -> (65 to 90) respectively       (a to z)-> (97 to 122) respectively
CHAR_LENGTH(str)
Returns the length of the string str measured in characters. A multi-byte character counts as a single character. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.
>SELECT CHAR_LENGTH("text");
4
>SELECT CHAR_LENGTH("texte");
5

CHARACTER_LENGTH(str)
CHARACTER_LENGTH() is a synonym for CHAR_LENGTH().

CONCAT(str1,str2,...)
Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are non-binary strings, the result is a non-binary string. If the arguments include any binary strings, the result is a binary string.
>SELECT CONCAT('My', 'S', 'QL');
MySQL

CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT().
>SELECT CONCAT_WS(',','First name','Last Name' );
  First name,Last name
CONV(N,from_base,to_base)
Converts numbers between different number bases. Returns a string representation of the number N, converted from base from_base to to_base. Returns NULL if any argument is NULL. 
>SELECT CONV('a',16,2);
1010
>SELECT CONV(15,10,2);
1111
>SELECT CONV(11,10,2);
1011

ELT(N,str1,str2,str3,...)
Returns str1 if N = 1, str2 if N = 2, and so on. Returns NULL if N is less than 1 or greater than the number of arguments. ELT() is the complement of FIELD().
>SELECT ELT(1, 'ej', 'Heja', 'hej', 'foo');
ej
>SELECT ELT(3, 'ej', 'Heja', 'hej', 'foo');
hej
>SELECT ELT(5, 'ej', 'Heja', 'hej', 'foo');
NULL

EXPORT_SET(bits,on,off[,separator[,number_of_bits]])
Returns a string such that for every bit set in the value bits, you get an on string and for every bit not set in the value, you get an off string. Bits in bits are examined from right to left (from low-order to high-order bits). Strings are added to the result from left to right, separated by the separator string (the default being the comma character .,.). 
>SELECT EXPORT_SET(5,'Y','N',',',4);
Y,N,Y,N

FIELD(str,str1,str2,str3,...)
Returns the index (position starting with 1) of str in the str1, str2, str3, ... list. Returns 0 if str is not found.
>SELECT FIELD('ej', 'Hej', 'ej', 'ejh', 'hej', 'foo')
2
>SELECT FIELD('ej', 'Hej', 'egj', 'ejh', 'ej', 'foo')
4
FIND_IN_SET(str,strlist)
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings.
>SELECT FIND_IN_SET('b','a,c,b,d');
3
>SELECT FIND_IN_SET('c','c,a,b,d');
1
FORMAT(X,D)
Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part.
>SELECT FORMAT(12332.123456, 4);
12,332.1235
>SELECT FORMAT(12332.123456, 3);
12,332.123
>SELECT FORMAT(12332.123456, 0);
12,332

HEX(N_or_S)
If N_or_S is a number, returns a string representation of the hexadecimal value of N, where N is a longlong (BIGINT) number. This is equivalent to CONV(N,10,16).
If N_or_S is a string, returns a hexadecimal string representation of N_or_S where each character in N_or_S is converted to two hexadecimal digits.
>SELECT HEX(255);                                       
FF
>SELECT 0x646566;
def
>SELECT 646566;
646566
INSERT(str,pos,len,newstr)
Returns the string str, with the substring beginning at position pos and len characters long replaced by the string newstr. Returns the original string if pos is not within the length of the string. Replaces the rest of the string from position pos if len is not within the length of the rest of the string. Returns NULL if any argument is NULL.
>SELECT INSERT('Quadratic', 3, 4, 'What');
QuWhattic 
>SELECT INSERT('Quadratic', 2, 1, 'What');
Qwhatadratic


INSTR(str,substr)
Returns the position of the first occurrence of substring substr in string str. This is the same as the two-argument form of LOCATE(), except that the order of the arguments is reversed.
>SELECT INSTR('foobarbar', 'bar');
4
>SELECT INSTR('foobarbar', 'bar');
3

LCASE(str)
LCASE() is a synonym for LOWER().

LEFT(str,len)
Returns the leftmost len characters from the string str, or NULL if any argument is NULL.
>SELECT LEFT('foobarbar', 6);
foobar
>SELECT LEFT('foobarbar', 1);
f

LENGTH(str)
Returns the length of the string str, measured in bytes
>SELECT LENGTH('text');
4
>SELECT LENGTH('textert');
7
LOCATE(substr,str), LOCATE(substr,str,pos)
The first syntax returns the position of the first occurrence of substring substr in string str. The second syntax returns the position of the first occurrence of substring substr in string str, starting at position pos. Returns 0 if substr is not in str.
>SELECT LOCATE('bar', 'fbarbar');
2
>SELECT LOCATE('bar', 'foobarbar');
4
LOWER(str)
Returns the string str with all characters changed to lowercase according to the current character set mapping.
>SELECT LOWER('QUADRATICALLY');
quadratically
LPAD(str,len,padstr)
Returns the string str, left-padded with the string padstr to a length of len characters. If str is longer than len, the return value is shortened to len characters.
>SELECT LPAD('hiartff',4,'??');
hiar
>SELECT LPAD('hia',4,'??');
?hia
>SELECT LPAD('hi',4,'??');
??hi

LTRIM(str)
Returns the string str with leading space characters removed.
>SELECT LTRIM('   barbar');
barbar
MAKE_SET(bits,str1,str2,...)
Returns a set value (a string containing substrings separated by .,. characters) consisting of the strings that have the corresponding bit in bits set. str1 corresponds to bit 0, str2 to bit 1, and so on. NULL values in str1, str2, ... are not appended to the result.
>SELECT MAKE_SET(1,'a','b','c');
a
>SELECT MAKE_SET(b,'a','b','c');
B
>SELECT MAKE_SET(3,'a','b','c');
a,b
>SELECT MAKE_SET(7,'a','b','c');
a,b,c

MID(str,pos,len)
MID(str,pos,len) is a synonym for SUBSTRING(str,pos,len).

OCT(N)
Returns a string representation of the octal value of N, where N is a longlong (BIGINT) number. This is equivalent to CONV(N,10,8). Returns NULL if N is NULL.
>SELECT OCT(12);
14
>SELECT OCT(26);
32

OCTET_LENGTH(str)
OCTET_LENGTH() is a synonym for LENGTH().

ORD(str)
If the leftmost character is not a multi-byte character, ORD() returns the same value as the ASCII() function.
>SELECT ORD('4');
52
>SELECT ORD('2’);
50
POSITION(substr IN str)
POSITION(substr IN str) is a synonym for LOCATE(substr,str).
QUOTE(str)
Quotes a string to produce a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotes and with each instance of single quote ('), backslash ('\'), ASCII NUL, and Control-Z preceded by a backslash. If the argument is NULL, the return value is the word 'NULL' without enclosing single quotes.
>SELECT QUOTE('Don\\\'t!');
‘Don\\\’t!’



expr REGEXP pattern
This function performs a pattern match of expr against pattern. Returns 1 if expr matches pat; otherwise it returns 0. If either expr or pat is NULL, the result is NULL. REGEXP is not case sensitive, except when used with binary strings.
>SELECT 'ABCDEF' REGEXP 'A%C%%';
0
>SELECT 'ABCDE' REGEXP '.*';
1
>SELECT 'new*\n*line' REGEXP 'new\\*.\\*line';
1

REPEAT(str,count)
Returns a string consisting of the string str repeated count times. If count is less than 1, returns an empty string. Returns NULL if str or count are NULL.
>SELECT REPEAT('SQL', 3);
SQLSQLSQL
REPLACE(str,from_str,to_str)
Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str.
>SELECT REPLACE('www.mysql.com', 'm', 'Ww');
www.Wwysql.coWw
REVERSE(str)
Returns the string str with the order of the characters reversed.
>SELECT REVERSE('abcd');
dcba
RIGHT(str,len)
Returns the rightmost len characters from the string str, or NULL if any argument is NULL.
>SELECT RIGHT('foobarbar', 6);
barbar

RPAD(str,len,padstr)
Returns the string str, right-padded with the string padstr to a length of len characters. If str is longer than len, the return value is shortened to len characters.
>SELECT RPAD('hi',5,'?');
hi???
>SELECT RPAD('hiaa',3,'?');
hia
RTRIM(str)
Returns the string str with trailing space characters removed.
>SELECT RTRIM('barbar   ');
barbar
SOUNDEX(str)
Returns a soundex string from str. A standard soundex string is four characters long, but the SOUNDEX() function returns an arbitrarily long string. You can use SUBSTRING() on the result to get a standard soundex string.
>SELECT SOUNDEX('Hello');
H400
>SELECT SOUNDEX('friend');
F653
expr1 SOUNDS LIKE expr2
This is the same as SOUNDEX(expr1) = SOUNDEX(expr2).
SPACE(N)
Returns a string consisting of N space characters.
>SELECT SPACE(2);
‘     ‘

STRCMP(str1, str2)
Compares two strings and returns 0 if both strings are equal, it returns -1 if the first argument is smaller than the second according to the current sort order otherwise it returns 1.
>SELECT STRCMP('MOHD', 'MNHD');
1


>SELECT STRCMP('MOHD', 'MOHD');
0
>SELECT STRCMP('AMOHD', 'MOHD');
-1
>SELECT STRCMP('MOHD', 'AMOHD');
1

SUBSTRING(str,pos)
>SELECT SUBSTRING('Quadratically',5);
radically
SUBSTRING(str FROM pos)
>SELECT SUBSTRING('foobarbar' FROM 2);
obarbar
SUBSTRING(str,pos,len)
>SELECT SUBSTRING('Quadratically',3,4);
adra


SUBSTRING_INDEX(str,delim,count)
Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

>SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str) SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');
Returns the string str with all remstr prefixes or suffixes removed. If none of the specifiers BOTH, LEADING, or TRAILING is given, BOTH is assumed. remstr is optional and, if not specified, spaces are removed.
>SELECT TRIM('  bar   ');
bar

>SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');
barxxx

>SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx');
bar

>SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');
barx

UCASE(str)
UCASE() is a synonym for UPPER().

UNHEX(str)
Performs the inverse operation of HEX(str). That is, it interprets each pair of hexadecimal digits in the argument as a number and converts it to the character represented by the number. The resulting characters are returned as a binary string.
>SELECT UNHEX('4D7955314C');
MyUIL

UPPER(str)
Returns the string str with all characters changed to uppercase according to the current character set mapping.
>SELECT UPPER('Allah-hus-samad');

ALLAH-HUS-SAMAD



SQL - Date Functions


1.ADDDATE(date,INTERVAL expr unit), ADDDATE(expr,days)
>SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);
1998-02-02
>SELECT ADDDATE('1998-01-02', 31);
1998-02-02

ADDTIME(expr1,expr2)
ADDTIME() adds expr2 to expr1 and returns the result. expr1 is a time or datetime expression, and expr2 is a time expression.
>SELECT ADDTIME('1997-12-31 23:59:59.999999','1 1:1:1.000002');
1998-01-02  01:01:01.000001

CONVERT_TZ(dt,from_tz,to_tz)
This converts a datetime value dt from the time zone given by from_tz to the time zone given by to_tz and returns the resulting value. This function returns NULL if the arguments are invalid.
>SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');
2004-01-01 22:00:00

CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.
>SELECT CURDATE();
2017-03-08
>SELECT CURDATE() + 0;
20170308
CURRENT_DATE and CURRENT_DATE()
CURRENT_DATE and CURRENT_DATE() are synonyms for CURDATE()

CURTIME()
Returns the current time as a value in 'HH:MM:SS' or HHMMSS format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.
>SELECT CURTIME();
11:07:45
>SELECT CURTIME() + 0;
110745

CURRENT_TIME and CURRENT_TIME()
CURRENT_TIME and CURRENT_TIME() are synonyms for CURTIME().
CURRENT_TIMESTAMP and CURRENT_TIMESTAMP()
CURRENT_TIMESTAMP and CURRENT_TIMESTAMP() are synonyms for NOW().

DATE(expr)
Extracts the date part of the date or datetime expression expr.
>SELECT DATE('2003-12-31 01:02:03');
2003-12-31

DATEDIFF(expr1,expr2)
DATEDIFF() returns expr1 . expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.
>SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');
1
DATE_FORMAT(date,format)
Formats the date value according to the format string.
>SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y');
Saturday October 1997
Ex:
%W
Weekday name (Sunday..Saturday)
%M
Month name(jan..dec)
%Y
Year, numeric four digits

DATE_SUB(date,INTERVAL expr unit)
This is similar to DATE_ADD() function.
DAY(date)
DAY() is a synonym for DAYOFMONTH().
DAYNAME(date)
Returns the name of the weekday for date.
>SELECT DAYNAME('1998-02-05');
Thursday
DAYOFWEEK(date)
Returns the weekday index for date (1 = Sunday, 2 = Monday, ., 7 = Saturday). These index values correspond to the ODBC standard.
>SELECT DAYOFWEEK('1998-02-03');
3
DAYOFYEAR(date)
Returns the day of the year for date, in the range 1 to 366.
>SELECT DAYOFYEAR('1998-02-03');
34
EXTRACT(unit FROM date)
The EXTRACT() function uses the same kinds of unit specifiers as DATE_ADD() or DATE_SUB(), but extracts parts from the date rather than performing date arithmetic.
>SELECT EXTRACT(YEAR FROM '1999-07-02');
1999
>SELECT EXTRACT(YEAR_MONTH FROM '1999-07-02 01:02:03');
199907
FROM_DAYS(N)
Given a day number N, returns a DATE value.
>SELECT FROM_DAYS(729669);
1997-10-07
FROM_UNIXTIME(unix_timestamp)
FROM_UNIXTIME(unix_timestamp,format)
Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context.
>SELECT FROM_UNIXTIME(875996580);
1997-10-04 22:33:00


HOUR(time)
Returns the hour for time. The range of the return value is 0 to 23 for time-of-day values.
>SELECT HOUR('10:05:03');
10
LAST_DAY(date)
Takes a date or datetime value and returns the corresponding value for the last day of the month. Returns NULL if the argument is invalid.
>SELECT LAST_DAY('2003-02-05');
2003-02-28
LOCALTIME and LOCALTIME()
LOCALTIME and LOCALTIME() are synonyms for NOW().
LOCALTIMESTAMP and LOCALTIMESTAMP()
LOCALTIMESTAMP and LOCALTIMESTAMP() are synonyms for NOW().
MAKEDATE(year,dayofyear)
Returns a date, given year and day-of-year values. dayofyear must be greater than 0 or the result is NULL.
>SELECT MAKEDATE(2001,31), MAKEDATE(2001,32);
2001-01-31  2002-02-01
MAKETIME(hour,minute,second)
Returns a time value calculated from the hour, minute, and second arguments.
>SELECT MAKETIME(12,15,30);
12:15:30
MICROSECOND(expr)
Returns the microseconds from the time or datetime expression expr as a number in the range from 0 to 999999.
>SELECT MICROSECOND('12:00:00.123456');
123456
MINUTE(time)
Returns the minute for time, in the range 0 to 59.
>SELECT MINUTE('98-02-03 10:05:03');
5
MONTH(date)
Returns the month for date, in the range 0 to 12.
>SELECT MONTH('1998-02-03')
2
MONTHNAME(date)
Returns the full name of the month for date.
>SELECT MONTHNAME('1998-02-05');
February
NOW()
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.
>SELECT NOW();
2017-03-09 2:31:45
PERIOD_ADD(P,N)
Adds N months to period P (in the format YYMM or YYYYMM). Returns a value in the format YYYYMM. Note that the period argument P is not a date value.
>SELECT PERIOD_ADD(9801,2);
199803
PERIOD_DIFF(P1,P2)
Returns the number of months between periods P1 and P2. P1 and P2 should be in the format YYMM or YYYYMM. Note that the period arguments P1 and P2 are not date values.
>SELECT PERIOD_DIFF(9802,199703);
11
QUARTER(date)
Returns the quarter of the year for date, in the range 1 to 4.
>SELECT QUARTER('98-04-01');
2
SECOND(time)
Returns the second for time, in the range 0 to 59.
>SELECT SECOND('10:05:03');
3

SEC_TO_TIME(seconds)
Returns the seconds argument, converted to hours, minutes and seconds, as a value in 'HH:MM:SS' or HHMMSS format, depending on whether the function is used in a string or numeric context.
>SELECT SEC_TO_TIME(2378);
00:39:38
STR_TO_DATE(str,format)
This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts or a DATE or TIME value if the string contains only date or time parts.
>SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');
2004-04-31
SUBDATE(date,INTERVAL expr unit) and SUBDATE(expr,days)
When invoked with the INTERVAL form of the second argument, SUBDATE() is a synonym for DATE_SUB(). For information on the INTERVAL unit argument, see the discussion for DATE_ADD().
>SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
1998-01-02
SUBTIME(expr1,expr2)
SUBTIME() returns expr1 . expr2 expressed as a value in the same format as expr1. expr1 is a time or datetime expression, and expr2 is a time.
>SELECT SUBTIME('1997-12-31 23:59:59.999999',
    '1 1:1:1.000002');
1997-12-30 22:58:58.999997
SYSDATE()
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context.
>SELECT SYSDATE();
2017-03-09 15:03:03
TIME(expr)
Extracts the time part of the time or datetime expression expr and returns it as a string.
>SELECT TIME('2003-12-31 01:02:03');
01:02:03
TIMEDIFF(expr1,expr2)
TIMEDIFF() returns expr1 . expr2 expressed as a time value. expr1 and expr2 are time or date-and-time expressions, but both must be of the same type.
>SELECT TIMEDIFF('1997-12-31 23:59:59.000001',
    '1997-12-30 01:01:01.000002');
46:58:57.999999
TIMESTAMP(expr), TIMESTAMP(expr1,expr2)
With a single argument, this function returns the date or datetime expression expr as a datetime value. With two arguments, it adds the time expression expr2 to the date or datetime expression expr1 and returns the result as a datetime value.

>SELECT TIMESTAMP('2003-12-31');
2003-12-31 00:00:00
TIMESTAMPADD(unit,interval,datetime_expr)
Adds the integer expression interval to the date or datetime expression datetime_expr. 
>SELECT TIMESTAMPADD(MINUTE,1,'2003-01-02');
2003:01:02 00:01:00
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
Returns the integer difference between the date or datetime expressions datetime_expr1 and datetime_expr2.
>SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
3
TIME_FORMAT(time,format)
This is used like the DATE_FORMAT() function, but the format string may contain format specifiers only for hours, minutes and seconds.
>SELECT TIME_FORMAT('100:00:00', '%H %k %h %I %l');
100 100 04 04 4
TIME_TO_SEC(time)
Returns the time argument converted to seconds.
>SELECT TIME_TO_SEC('22:23:00');
80580
TO_DAYS(date)
Given a date, returns a day number (the number of days since year 0).
>SELECT TO_DAYS(950501);
728779
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
If called with no argument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) as an unsigned integer.
>SELECT UNIX_TIMESTAMP();
8822226357
>SELECT UNIX_TIMESTAMP('1997-10-04 22:23:00');
875996580
UTC_DATE, UTC_DATE()
Returns the current UTC date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.
>SELECT UTC_DATE(), UTC_DATE() + 0;
2017-03-09        20170309
UTC_TIME, UTC_TIME()
Returns the current UTC time as a value in 'HH:MM:SS' or HHMMSS format, depending on whether the function is used in a string or numeric context.
>SELECT UTC_TIME(), UTC_TIME() + 0;
03:58:12     035812

UTC_TIMESTAMP, UTC_TIMESTAMP()
Returns the current UTC date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context.
>SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0;
2017-03-09 04:01:45   20170309040145
WEEK(date[,mode])
This function returns the week number for date. 
>SELECT WEEK('1998-02-20');
7
WEEKDAY(date)
Returns the weekday index for date (0 = Monday, 1 = Tuesday, . 6 = Sunday).
>SELECT WEEKDAY('1998-02-03 22:23:00');
1
WEEKOFYEAR(date)
Returns the calendar week of the date as a number in the range from 1 to 53. WEEKOFYEAR() is a compatibility function that is equivalent to WEEK(date,3).
>SELECT WEEKOFYEAR('1998-02-20');
8
YEAR(date)
Returns the year for date, in the range 1000 to 9999, or 0 for the .zero. date.
>SELECT YEAR('98-02-03');
1998
YEARWEEK(date), YEARWEEK(date,mode)
Returns year and week for a date. The mode argument works exactly like the mode argument to WEEK(). The year in the result may be different from the year in the date argument for the first and the last week of the year.
>SELECT YEARWEEK('1987-01-01');
198652

No comments:

Post a Comment