Funzioni che restituiscono parti di data e ora

Funzioni per manipolare i dati in SQL Server

Ana Voicu

Data Engineer

YEAR(date)

  • Restituisce l’anno dalla data indicata
SELECT 
    first_name,
    first_vote_date,
    YEAR(first_vote_date) AS first_vote_year
FROM voters;
| first_name | first_vote_date | first_vote_year |
|------------|-----------------|-----------------|
| Carol      | 2015-03-09      | 2015            |
| Ana        | 2015-01-17      | 2015            |
| Melissa    | 2015-04-09      | 2015            |
Funzioni per manipolare i dati in SQL Server

MONTH(date)

  • Restituisce il mese dalla data indicata
SELECT 
    first_name,
    first_vote_date,
    YEAR(first_vote_date) AS first_vote_year,
    MONTH(first_vote_date) AS first_vote_month
FROM voters;

| first_name | first_vote_date | first_vote_year | first_vote_month |
|------------|-----------------|-----------------|------------------|
| Carol      | 2015-03-09      | 2015            | 3                |
| Ana        | 2015-01-17      | 2015            | 1                |
| Melissa    | 2015-04-09      | 2015            | 4                |
Funzioni per manipolare i dati in SQL Server

DAY(date)

  • Restituisce il giorno dalla data indicata
SELECT 
    first_name,
    first_vote_date,
    YEAR(first_vote_date) AS first_vote_year,
    MONTH(first_vote_date) AS first_vote_month,
    DAY(first_vote_date) AS first_vote_day
FROM voters;

| first_name | first_vote_date | first_vote_year | first_vote_month | first_vote_day |
|------------|-----------------|-----------------|------------------|----------------|
| Carol      | 2015-03-09      | 2015            | 3                | 9              |
| Ana        | 2015-01-17      | 2015            | 1                | 17             |
| Melissa    | 2015-04-09      | 2015            | 4                | 9              |
Funzioni per manipolare i dati in SQL Server

DATENAME(datepart, date)

  • Restituisce una stringa di testo che rappresenta la parte di data specificata
datepart abbreviazioni
year yy, yyyy
month mm, m
dayofyear dy, y
week wk, ww
weekday dw, w
Funzioni per manipolare i dati in SQL Server

Esempio di DATENAME()

DECLARE @date datetime = '2019-03-24'
SELECT 
    YEAR(@date) AS year,
    DATENAME(YEAR, @date) AS year_name,
    MONTH(@date) AS month,
    DATENAME(MONTH, @date) AS month_name,
    DAY(@date) AS day,
    DATENAME(DAY, @date) AS day_name,
    DATENAME(WEEKDAY, @date) AS weekday
| year | year_name | month | month_name | day | day_name | weekday |
|------|-----------|-------|------------|-----|----------|---------|
| 2019 | 2019      | 3     | March      | 24  | 24       | Sunday  |
Funzioni per manipolare i dati in SQL Server

DATEPART(datepart, date)

  • Simile a DATENAME()
  • Restituisce un intero che rappresenta la parte di data specificata
 DECLARE @date datetime = '2019-03-24'
SELECT 
    DATEPART(YEAR, @date) AS year_name,
    DATENAME(YEAR, @date) AS year_name,
    DATEPART(MONTH, @date) AS month_name,
    DATENAME(MONTH, @date) AS month_name
| year | year_name | month | month_name | 
|------|-----------|-------|------------|
| 2019 | 2019      | 3     | March      |
Funzioni per manipolare i dati in SQL Server

DATEFROMPARTS(year, month, day)

  • Riceve 3 parametri: anno, mese e giorno
  • Genera una date
SELECT 
    DATEFROMPARTS(2019, 3, 5) AS new_date;
| new_date   | 
|------------|
| 2019-03-05 |
Funzioni per manipolare i dati in SQL Server

DATEFROMPARTS(year, month, day)

SELECT 
    YEAR('2019-03-05')  AS date_year,
    MONTH('2019-03-05') AS date_month,
    DAY('2019-03-05')   AS date_day,
    DATEFROMPARTS(YEAR('2019-03-05'), MONTH('2019-03-05'), DAY('2019-03-05')) AS reconstructed_date;
| date_year | date_month | date_day | reconstructed_date |
|-----------|------------|----------|--------------------|
| 2019      | 3          | 5        | 2019-03-05         |
Funzioni per manipolare i dati in SQL Server

Ayo berlatih!

Funzioni per manipolare i dati in SQL Server

Preparing Video For Download...