MS-SQL の場合
DECLARE @d1 INT; SET @d1 = 201208
DECLARE @d2 VARCHAR(6); SET @d2 = 201208
SELECT 'data type'='int'
, 'in'=@d1
, 'out'=CONVERT(VARCHAR(3), CAST(LEFT(@d1,6)+'01' AS DATE), 107)
+ '-' + LEFT(@d1,4)
SELECT 'data type'='varchar'
, 'in'=@d2
, 'out'=CONVERT(VARCHAR(3), CAST(@d2+'01' AS DATE), 107)
+ '-' +LEFT(@d2,4)
/* -- OUTPUT
data type in out
int 201208 Aug-2012
data type in out
varchar 201208 Aug-2012
*/
DATENAME を利用することもできます
SELECT 'data type' = 'int'
, 'in' = @d1
, 'out' = LEFT(DATENAME(month, CAST(LEFT(@d1,6)+'01' AS DATE)), 3)
+ '-' + LEFT(@d1,4)
SELECT 'data type' = 'varchar'
, 'in' = @d2
, 'out' = LEFT(DATENAME(month, CAST(@d2+'01' AS DATE)), 3)
+ '-' + LEFT(@d2,4)
または別のSTUFFを使用してください:)
DECLARE @d1 INT; SET @d1 = 201208
DECLARE @d2 VARCHAR(6); SET @d2 = 201208
SELECT 'data type' = 'int'
, 'in' = @d1
, 'out' = STUFF(CONVERT(VARCHAR(20), CAST(LEFT(@d1,6)+'01' AS DATE), 107), 4, 5, '-')
SELECT 'data type' = 'varchar'
, 'in' = @d2
, 'out' = STUFF(CONVERT(VARCHAR(20), CAST(@d2+'01' AS DATE), 107), 4, 5, '-')