Forum Moderators: coopster

Message Too Old, No Replies

Date Format

MySQL equivalent in MSSQL

         

trebian

3:24 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



Hi forum:
can a guru show me the equivalent of this code (that I use in Mysql), but need to replicate in MSSQL

[MySQL CODE]
Date_format(Orderdate, '%y-%m') as period .... more statements
[/MySQL CODE]

How do I do this in MSSQL? The date is in this format in MSSQL DB.: 2003-11-30 00:00:00

**I have tried various things but my efforts are failing!

Timotheos

3:49 pm on Jun 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been doing it in php...
$newdate = date('n/j/y',strtotime($date))

But now that you ask I realize I could do it in the select statement with convert().
CONVERT ( data_type [ ( length) ] , expression [ , style ] )

Here's some good articles:
[databasejournal.com...]
[winnetmag.com...]

trebian

4:48 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



Thank you for the suggestion. I did further research and found that instead of CONVERT() CAST() is preferred; from MSSQL manual.

I changed my MSSQL-code to


("select (CAST(year(Changedate) as varchar)+'-'+CAST(month(Changedate) as varchar)) as tim
... more statements
");

Timotheos

5:53 pm on Jun 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks trebian. That's cool.