From: Russell Mangel on
Hi,

I need to convert a DATETIME to the following CHAR/VARCHAR

Jul 76

The Following TSQL works, but maybe there is a cleaner way?

DECLARE @d DATETIME = '1776-07-04';
SELECT SUBSTRING(DATENAME(MONTH,@d), 1, 3)+' '+SUBSTRING(DATENAME(YEAR,@d),
3, 2);

Thanks

Russell Mangel
Las Vegas, NV




From: Erland Sommarskog on
Russell Mangel (russell(a)tymer.net) writes:
> I need to convert a DATETIME to the following CHAR/VARCHAR
>
> Jul 76
>
> The Following TSQL works, but maybe there is a cleaner way?
>
> DECLARE @d DATETIME = '1776-07-04';
> SELECT SUBSTRING(DATENAME(MONTH,@d), 1, 3)+'
> '+SUBSTRING(DATENAME(YEAR,@d), 3, 2);

I think that is as clean as it can get. This is shorter:

select convert(char(4), @d) + substring(convert(varchar, @d), 10, 2)

But I'm not sure that I like it better. What you have will produce something
useful, no matter the language setting, but my short version produces
rubbish when I set the language to Japanese.

--
Erland Sommarskog, SQL Server MVP, esquel(a)sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

From: Peso on
DECLARE @d DATETIME = '1776-07-04'

SELECT RIGHT(CONVERT(CHAR(9), @d, 6), 6)


//Peter

"Russell Mangel" <russell(a)tymer.net> wrote in message
news:#brYb5wpKHA.5588(a)TK2MSFTNGP02.phx.gbl...
> Hi,
>
> I need to convert a DATETIME to the following CHAR/VARCHAR
>
> Jul 76
>
> The Following TSQL works, but maybe there is a cleaner way?
>
> DECLARE @d DATETIME = '1776-07-04';
> SELECT SUBSTRING(DATENAME(MONTH,@d), 1, 3)+'
> '+SUBSTRING(DATENAME(YEAR,@d), 3, 2);
>
> Thanks
>
> Russell Mangel
> Las Vegas, NV
>
>
>
>