SqlServer get current date

1. Get the current date

select GETDATE()

format:

select CONVERT(varchar,GETDATE(),120) --2018-04-23 14:47:10

 

2. Get the current year --2017

select DATENAME(YYYY,GETDATE()) --2018

select DATENAME(YEAR,GETDATE()) --2018

select DATEPART(YYYY,GETDATE()) --2018

select DATEPART(YEAR,GETDATE()) --2018

select YEAR (time field name)--2018

 

3. Get the current month -- 05 or 5

select DATENAME(MM,GETDATE()) --04

select DATENAME(MONTH,GETDATE()) --04

 

select DATEPART(MM,GETDATE()) --4

select DATEPART(MONTH,GETDATE()) --4

 

select MONTH(time field name) --4

 

4. Get the current date -- 07 or 7

select DATENAME(DAY,GETDATE()) --23
select DATENAME(D,GETDATE()) --23
select DATENAME(DD,GETDATE()) --23

 

select DATEPART(D,GETDATE()) --23
select DATEPART(DD,GETDATE()) --23 
select DATEPART(DAY,GETDATE()) --23

 

select DAY(GETDATE())

 

5. Get the current year-month-201705

select CONVERT(varchar(6),GETDATE(),112) --201804

select CONVERT(varchar(7),GETDATE(),120) --2018-04

 

6. Get the current year-month-day--20170512

select CONVERT(varchar(8),GETDATE(),112) --20180423

select CONVERT(varchar(10),GETDATE(),120) --2018-04-23

 

7. Date related functions finishing:

GETDATE() : Get the current time, the time format is the default.

DATENAME: There are two parameters, the value interval interval and time date

                         date is time;

interval includes year, month, day, week, etc.

          eg:

               YEAR year; YYYY year; YY year;
               QUARTER quarter; QQ quarter; Q quarter;                MONTH
               month (04); MM month (04); M month (04)                ; Week of the year; WK Week of the year; WW Week of the year; ISOWK Week of the year; ISOWW Week of the year; ISO_WEEK Week of the year;                DAY day; DD day; D day;                DAYOFYEAR day of the year; DY day of the year; Y day of the year;                HOUR hours; HH hours;                MINUTE minutes; MI minutes; N minutes;                SECOND seconds; SS seconds; S seconds;                MCS microseconds (omitted); MICROSECOND microseconds (omitted); MILLISECOND milliseconds (omitted); MS milliseconds (omitted); NANOSECOND billionths of a second (omitted); NS billionths of a second (omitted) ;







DATEPART: The parameter refers to DATENAME, but the return value is different, DATENAME returns varchar, DATEPART returns int

CONVERT function conversion format: mainly pay attention to the third parameter date_style; format: select CONVERT(varchar,GETDATE(),0)

        The last date_style can have 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22 ,23,24,25,100,

                                                          101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 120, 121, 126, 127, 130, 131, etc.

               Commonly used 20, 23, 24, 102, 111, 112, 120, etc.;

               Example:

                        select CONVERT(varchar,GETDATE(),20)  --2018-04-23 14:44:22
                        select CONVERT(varchar,GETDATE(),23)  --2018-04-23
                        select CONVERT(varchar,GETDATE(),24)  --14:44:22
                        select CONVERT(varchar,GETDATE(),102) --2018.04.23
                        select CONVERT(varchar,GETDATE(),111) --2018/04/23
                        select CONVERT(varchar,GETDATE(),112) --20180423

          String to date: select CONVERT(datetime,'2018-04-23',20) The third parameter refers to the above.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324687058&siteId=291194637