lundi 23 novembre 2015

T-SQL format date by pattern

Is possible change format date by specific pattern ? I need to made a function which has a two parameters. First is date and second is pattern. I need convert more date variants. Goal this function is change US and European date format.

For example i need convert

EU: dd:MM:yyyy hh:mm:ss to US: MM:dd:yyyy hh:mm:ss

On another page i need change

EU: dd/MM/yyyy to US: MM/dd/yyyy

And i have a several next variant to convert

And i want to made a similar function

Formater(euDate, pattern)
BEGIN
....

RETURN usDate

My production server is unfortunately SQL server 2005 and doesn't support function FORMAT(). And function CONVERT() doesn't support some variant of date, which i need convert. So in my current solution i parse EU date at individualy parts (@day = day(@euDate), @month, @year, ...) and join them in new string . And i compare it with input parameter in pattern and return CASE which is equal like pattern. I want to this function make general and simplier.

Thank you for Your advice.

Aucun commentaire:

Enregistrer un commentaire