Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
SQL Server - DateFirst Example
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
-------------------------------------------------------------------------------- -- Create Date Table -------------------------------------------------------------------------------- CREATE TABLE date_t ( [TheDate] DATE NOT NULL PRIMARY KEY ); -------------------------------------------------------------------------------- -- Create Date list -- Using Number sequence: http://datablog.roman-halliday.com/index.php/2018/06/01/dates-in-sql-server-create-sample-date-ranges/ -------------------------------------------------------------------------------- DECLARE @DateStart DATE = '01-JAN-17'; -- At time of writing this is the past DECLARE @DateEnd DATE = '01-JAN-19'; -- At time of writing this is the future PRINT 'Start Date: ' + CONVERT(VARCHAR(10), @DateStart, 120); PRINT 'Start End: ' + CONVERT(VARCHAR(10), @DateEnd, 120); WITH number_tbl AS ( SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 ), number_sequences AS ( SELECT t1.a AS t1a, t2.a AS t2a, t3.a AS t3a, (t3.a + (10 * t2.a) + (100 * t1.a)) AS concatenate_calculation FROM number_tbl t1 CROSS JOIN number_tbl t2 CROSS JOIN number_tbl t3 ) INSERT INTO date_t ([TheDate]) SELECT CAST(DateAdd(DAY, concatenate_calculation, @DateStart) AS DATE) AS [TheDate] FROM number_sequences WHERE CAST(DateAdd(DAY, concatenate_calculation, @DateStart) AS DATE) <= @DateEnd ORDER BY concatenate_calculation ; -------------------------------------------------------------------------------- -- Format to get results -------------------------------------------------------------------------------- SELECT @@DateFirst AS [DateFirst Setting]; -- Default --Set the start of the week to Monday SET DATEFIRST 1; SELECT @@DateFirst AS [DateFirst Setting]; SELECT [TheDate], DateName(dw, [TheDate]) AS TheDate_day_name, DatePart(dw, [TheDate]) AS TheDate_day_of_week, DatePart(dd, [TheDate]) AS TheDate_day_of_month, DatePart(dy, [TheDate]) AS TheDate_day_of_year, DatePart(wk, [TheDate]) AS TheDate_week_of_year FROM date_t WHERE [TheDate] BETWEEN '29-DEC-17' AND '20-JAN-18' ; --Set the start of the week to Sunday SET DATEFIRST 7; SELECT @@DateFirst AS [DateFirst Setting]; SELECT [TheDate], DateName(dw, [TheDate]) AS TheDate_day_name, DatePart(dw, [TheDate]) AS TheDate_day_of_week, DatePart(dd, [TheDate]) AS TheDate_day_of_month, DatePart(dy, [TheDate]) AS TheDate_day_of_year, DatePart(wk, [TheDate]) AS TheDate_week_of_year FROM date_t WHERE [TheDate] BETWEEN '29-DEC-17' AND '20-JAN-18' ;
View schema
Execution time: 0,03 sec, rows selected: 51, rows affected: 731, absolute service time: 0,2 sec
edit mode
|
history
|
discussion
DateFirst Setting
1
7
DateFirst Setting
1
1
TheDate
TheDate_day_name
TheDate_day_of_week
TheDate_day_of_month
TheDate_day_of_year
TheDate_week_of_year
1
29.12.2017 00:00:00
Friday
5
29
363
53
2
30.12.2017 00:00:00
Saturday
6
30
364
53
3
31.12.2017 00:00:00
Sunday
7
31
365
53
4
01.01.2018 00:00:00
Monday
1
1
1
1
5
02.01.2018 00:00:00
Tuesday
2
2
2
1
6
03.01.2018 00:00:00
Wednesday
3
3
3
1
7
04.01.2018 00:00:00
Thursday
4
4
4
1
8
05.01.2018 00:00:00
Friday
5
5
5
1
9
06.01.2018 00:00:00
Saturday
6
6
6
1
10
07.01.2018 00:00:00
Sunday
7
7
7
1
11
08.01.2018 00:00:00
Monday
1
8
8
2
12
09.01.2018 00:00:00
Tuesday
2
9
9
2
13
10.01.2018 00:00:00
Wednesday
3
10
10
2
14
11.01.2018 00:00:00
Thursday
4
11
11
2
15
12.01.2018 00:00:00
Friday
5
12
12
2
16
13.01.2018 00:00:00
Saturday
6
13
13
2
17
14.01.2018 00:00:00
Sunday
7
14
14
2
18
15.01.2018 00:00:00
Monday
1
15
15
3
19
16.01.2018 00:00:00
Tuesday
2
16
16
3
20
17.01.2018 00:00:00
Wednesday
3
17
17
3
21
18.01.2018 00:00:00
Thursday
4
18
18
3
22
19.01.2018 00:00:00
Friday
5
19
19
3
23
20.01.2018 00:00:00
Saturday
6
20
20
3
DateFirst Setting
1
7
TheDate
TheDate_day_name
TheDate_day_of_week
TheDate_day_of_month
TheDate_day_of_year
TheDate_week_of_year
1
29.12.2017 00:00:00
Friday
6
29
363
52
2
30.12.2017 00:00:00
Saturday
7
30
364
52
3
31.12.2017 00:00:00
Sunday
1
31
365
53
4
01.01.2018 00:00:00
Monday
2
1
1
1
5
02.01.2018 00:00:00
Tuesday
3
2
2
1
6
03.01.2018 00:00:00
Wednesday
4
3
3
1
7
04.01.2018 00:00:00
Thursday
5
4
4
1
8
05.01.2018 00:00:00
Friday
6
5
5
1
9
06.01.2018 00:00:00
Saturday
7
6
6
1
10
07.01.2018 00:00:00
Sunday
1
7
7
2
11
08.01.2018 00:00:00
Monday
2
8
8
2
12
09.01.2018 00:00:00
Tuesday
3
9
9
2
13
10.01.2018 00:00:00
Wednesday
4
10
10
2
14
11.01.2018 00:00:00
Thursday
5
11
11
2
15
12.01.2018 00:00:00
Friday
6
12
12
2
16
13.01.2018 00:00:00
Saturday
7
13
13
2
17
14.01.2018 00:00:00
Sunday
1
14
14
3
18
15.01.2018 00:00:00
Monday
2
15
15
3
19
16.01.2018 00:00:00
Tuesday
3
16
16
3
20
17.01.2018 00:00:00
Wednesday
4
17
17
3
21
18.01.2018 00:00:00
Thursday
5
18
18
3
22
19.01.2018 00:00:00
Friday
6
19
19
3
23
20.01.2018 00:00:00
Saturday
7
20
20
3