SQLite strftime Function

The SQLite strftime function is a very powerful function which facilitates you to fetch date and time and also perform date calculation.

Syntax:

Here, format can be any of the following:

IndexformatExplanation
1)%YYear as 4 digits (0000 to 9999)
2)%WWeek of year (00 to 53)
3)%wDay of week (0 to 6, where 0 is Sunday)
4)%mMonth of year (01 to 12)
5)%dDay of month (00 to 31)
6)%HHour (00 to 24)
7)%MMinute (00 to 25)
8)%SSeconds (00 to 59)
9)%sSeconds since 1970-01-01
10)%fFractional seconds (SS.SSS)
11)%jDay of year (001 to 366)
12)%JJulian day as a numeric value

Here, timestring is a date value which can be anyone of the following:

IndextimestringDescription
1)nowIt is a literal used to return the current date.
2)YYYY-MM-DDIt specifies the date value formatted as 'YYYY-MM-DD'
3)YYYY-MM-DD HH:MMIt specifies the date value formatted as 'YYYY-MM-DD HH:MM'
4)YYYY-MM-DD HH:MM:SSIt specifies the date value formatted as 'YYYY-MM-DD HH:MM:SS'
5)YYYY-MM-DD HH:MM:SS.SSSIt specifies the date value formatted as 'YYYY-MM-DD HH:MM:SS.SSS'
6)HH:MMIt specifies the date value formatted as 'HH:MM'
7)HH:MM:SSIt specifies the date value formatted as 'HH:MM:SS'
8)HH:MM:SS.SSSIt specifies the date value formatted as 'HH:MM:SS.SSS'
9)YYYY-MM-DDTHH:MMIt specifies the date value formatted as 'YYYY-MM-DDTHH:MM' where t is a literal character separating the date and time portions.
10)YYYY-MM-DDTHH:MM:SSIt specifies the date value formatted as 'YYYY-MM-DDTHH:MM:SS' where t is a literal character separating the date and time portions
11)YYYY-MM-DDTHH:MM:SS.SSSIt specifies the date value formatted as 'YYYY-MM-DDTHH:MM:SS.SSS' where t is a literal character separating the date and time portions
12)DDDDDDDDDDIt specifies the Julian date number

modifier1, modifier2, ... modifier_n: modifiers are optional. These are used with timestring to add or subtract time, date or years.

IndexModifierDescription
1)[+-]NNN yearsIt is used to specify number of years added/subtracted to the date
2)[+-]NNN monthsIt is used to specify number of months added/subtracted to the date
3)[+-]NNN daysIt is used to specify number of days added/subtracted to the date
4)[+-]NNN hoursIt is used to specify number of hours added/subtracted to the date
5)[+-]NNN minutesIt is used to specify number of minutes added/subtracted to the date
6)[+-]NNN secondsIt is used to specify number of seconds added/subtracted to the date
7)[+-]NNN.NNNN secondsIt is used to specify number of seconds (and fractional seconds) added/subtracted to the date
8)start of yearIt is used to shift the date back to the start of the year
9)start of monthIt is used to shift the date back to the start of the month
10)start of dayIt is used to shift the date back to the start of the day
11)weekday NIt is used to move the date forward to the next date where weekday number is N (0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday)
12)unixepochIt is used with the DDDDDDDDDD timestring to interpret the date as UNIX Time (ie: number of seconds since 1970-01-01)
13)localtimeIt is used to adjust date to localtime, assuming the timestring was expressed in UTC
14)utcIt is used to adjust date to utc, assuming the timestring was expressed in localtime

Example1: Retrieve Current Date:

Output:

SQLite Strftime function 1 SQLite Strftime function 2

Example2: Retrieve First day of the month:

Output:

SQLite Strftime function 3 SQLite Strftime function 4 SQLite Strftime function 5 SQLite Strftime function 6

Example2: Retrieve Last day of the month:

Output:

SQLite Strftime function 7 SQLite Strftime function 8 SQLite Strftime function 9 SQLite Strftime function 10

Example3: Add/ Subtract years and days:

Output:

SQLite Strftime function 11 SQLite Strftime function 12 SQLite Strftime function 13 SQLite Strftime function 14
Next TopicSQLite Time




Latest Courses