Obtener Número De 2 Dígitos Para El Mes
Tengo una columna entera " Mes" Me gustaría obtener un número de 2 dígitos para el mes.
Esto es lo que he intentado: DATEPART (mm, @Date)
Devuelve un dígito para los meses de enero a septiembre Estoy usando SQL Server 2008
¿Alguien tiene alguna sugerencia?
11 answers
Hay diferentes maneras de hacerlo
- Usando RTRIM y especificando el rango :
Como
SELECT RIGHT('0' + RTRIM(MONTH('12-31-2012')), 2);
- Usando Subcadena para extraer la parte mes después de convertir la fecha en texto
Como
SELECT SUBSTRING(CONVERT(nvarchar(6),getdate(), 112),5,2)
Ver Violín
Puede haber otras maneras de conseguir esto.
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
2013-02-20 09:34:53
Función
FORMAT(date,'MM')
Hará el trabajo con dos dígitos.
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-02-08 20:35:12
Pinal Dave tiene un buen artículo con algunos ejemplos sobre cómo agregar 0 finales a los números SQL.
Una forma es usar la función RIGHT
, que haría la declaración algo como lo siguiente:
SELECT RIGHT('00' + CAST(DATEPART(mm, @date) AS varchar(2)), 2)
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
2013-02-20 09:12:19
Otro truco simple:
SELECT CONVERT(char(2), cast('2015-01-01' as datetime), 101) -- month with 2 digits
SELECT CONVERT(char(6), cast('2015-01-01' as datetime), 112) -- year (yyyy) and month (mm)
Salidas:
01
201501
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-15 13:42:29
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
2013-02-20 09:10:50
Añadir 0 antes de él comprobando si el valor cae entre 1 y 9 por primera vez echándolo a varchar
select case when DATEPART(month, getdate()) between 1 and 9
then '0' else '' end + cast(DATEPART(month, getdate()) as varchar(2))
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
2013-02-20 09:16:27
Simplemente se puede usar:
SELECT RIGHT('0' + CAST(MONTH(@Date) AS NVARCHAR(2)), 2)
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-01-18 17:40:47
CONVERT (char (2), getdate (), 101)
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-07-13 10:21:10
Intenta:
select right ('0'+convert(nvarchar(2), DATEPART(mm, getdate())),2 )
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
2013-02-20 09:12:54
Para mí la solución más rápida fue
DATE_FORMAT(CURDATE(),'%m')
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-02-08 19:46:50
Mi manera de hacerlo es:
right('0'+right(datepart(month,[StartDate]),2),2)
La razón de la función 'right' de interanl es evitar que SQL lo haga como math add, lo que nos dejará con un dígito de nuevo.
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-17 12:01:57