Obtener solo el Mes y el año a partir de la FECHA SQL


Necesito acceder solo Mes.Campo Año desde fecha en SQL Server.

Author: Tanner, 2009-11-23

17 answers

Además de las sugerencias ya dadas, hay otra posibilidad que puedo inferir de su pregunta:
- Usted todavía quiere que el resultado sea una fecha
- Pero usted quiere 'descartar' los Días, Horas, etc
- Dejando solo un año/mes campo de fecha

SELECT
   DATEADD(MONTH, DATEDIFF(MONTH, 0, <dateField>), 0) AS [year_month_date_field]
FROM
   <your_table>

Esto obtiene el número de meses enteros a partir de una fecha base (0) y luego los agrega a esa fecha base. Redondeando así hasta el mes en el que se encuentra la fecha.

NOTA: En SQL Server 2008, todavía tendrá el TIEMPO adjunto como 00:00: 00.000 Esto no es exactamente lo mismo que" eliminar " cualquier notación de día y hora por completo. También el DÍA fijado a la primera. por ejemplo, 2009-10-01 00:00:00.000

 133
Author: MatBailie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-02 18:23:16
select month(dateField), year(dateField)
 105
Author: Lucas Ayala,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-11-23 09:18:53
SELECT DATEPART(yy, DateVal)
SELECT DATEPART(MM, DateVal)
SELECT DATENAME(MM, DateVal)
 29
Author: Adriaan Stander,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-11-23 09:18:59
SELECT convert(varchar(7), getdate(), 126) 

Es posible que desee visitar este sitio web: http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005 /

 21
Author: Renne007,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-15 02:14:44
datename(m,column)+' '+cast(datepart(yyyy,column) as varchar) as MonthYear

La salida se verá como: 'Diciembre de 2013'

 14
Author: Valya Sylevych,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-11 00:19:17

Hay dos funciones SQL para hacerlo:

Consulte la documentación vinculada para más detalles.

 12
Author: Grzegorz Gierlik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-15 07:49:48

Esto también puede ser útil.

SELECT YEAR(0), MONTH(0), DAY(0);

O

SELECT YEAR(getdate()), MONTH(getdate()), DAY(getdate());

O

SELECT YEAR(yourDateField), MONTH(yourDateField), DAY(yourDateField);
 10
Author: NoNaMe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-29 05:40:59

Vamos a escribirlo de esta manera: YEAR(anySqlDate) y MONTH(anySqlDate). Pruébelo con YEAR(GETDATE()) por ejemplo.

 10
Author: Philippe Grondier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-29 08:58:49
convert(varchar(7), <date_field>, 120)
because 120 results in 'yyyy-MM-dd' which is varchar(10)
using varchar(7) will display only year and month

example:
select convert(varchar(7), <date_field>, 120), COUNT(*)
from <some_table>
group by convert(varchar(7), <date_field>, 120)
order by 1
 8
Author: razvangry,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-08 11:09:26

Estoy interpretando su pregunta de dos maneras.

A) Solo necesita Mes y Año por separado en cuyo caso aquí está la respuesta

select 
        [YEAR] = YEAR(getdate())
        ,[YEAR] = DATEPART(YY,getdate())
        , [MONTH] = month(getdate())
        ,[MONTH] = DATEPART(mm,getdate())
        ,[MONTH NAME] = DATENAME(mm, getdate()) 

B)

Que desea mostrar a partir de una fecha dada digamos '2009-11-24 09:01:55.483' en MES.Formato del año. Así que la salida debe venir como 11.2009 en este caso.

Si se supone que ese es el caso, intente esto (entre otras alternativas)

select [Month.Year] = STUFF(CONVERT(varchar(10), GETDATE(),104),1,3,'')
 5
Author: priyanka.sarkar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-11-24 04:22:39
RIGHT(CONVERT(VARCHAR(10), reg_dte, 105), 7) 
 2
Author: Valya Sylevych,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-11 04:53:50

CONCAT (datepart (yy,DATE), FORMAT (DATE,'MM'))

Le da por ejemplo 201601 si desea un resultado de seis dígitos

 2
Author: Mike,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-22 16:02:44

Prueba esto

select to_char(DATEFIELD,'MON') from YOUR_TABLE

Eg.

select to_char(sysdate, 'MON') from dual
 1
Author: Amin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-30 10:13:40

Algunas de las bases de datos como MS ACCESS o RODBC pueden no soportar las funciones SQL SERVER, pero para cualquier base de datos que tenga la función FORMAT simplemente puede hacer esto:

SELECT FORMAT(<your-date-field>,"YYYY-MM") AS year-date FROM <your-table>
 1
Author: Ibo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-11 18:14:33

Mi base de datos no es compatible con la mayoría de las funciones anteriores, sin embargo, encontré que esto funciona:

SELECT * FROM table WHERE SUBSTR(datetime_column, starting_position, number_of_strings)=required_year_and_month;

Por ejemplo: SELECT SUBSTR(created, 1,7) FROM table;

Devuelve el año y el mes en el formato "aaaa-mm"

 0
Author: mobfire,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-09 11:59:05

Select convert(varchar (11), transfer_date, 106)

Me consiguió el resultado deseado de la fecha formateada como 07 Mar 2018

Mi columna 'transfer_date' es una columna de tipo datetime y estoy usando SQL Server 2017 en azure

 0
Author: Hasan Junaid Hashmi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-12 07:06:31

Para el resultado:" AAAA-MM "

SELECT cast (YEAR () as varchar) + '-' + cast (Month () as varchar)

 0
Author: Jelena Lazarevic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-12 09:29:01