T-SQL格式按模式排列

时间:2022-08-03 08:47:11

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

欧盟: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

欧盟:dd / MM / yyyy对美国: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.

遗憾的是我的生产服务器是SQL Server 2005,并且不支持FORMAT()函数。函数CONVERT()不支持日期的某些变体,我需要转换。因此,在我目前的解决方案中,我在个别部分解析EU日期(@day = day(@euDate),@ month,@ year,...)并将它们加入新字符串中。并且我将它与模式中的输入参数进行比较并返回与模式相同的CASE。我希望这个功能一般而且更简单。

Thank you for Your advice.

感谢您的意见。

1 个解决方案

#1


1  

You almost certainly can use the convert function. You can read more about all the options here.

你几乎可以肯定可以使用转换功能。您可以在此处阅读有关所有选项的更多信息。

If there is some obscure invariant you need, check out this blog by Anubhav Goyal.

如果您需要一些模糊的不变量,请查看Anubhav Goyal的博客。

#1


1  

You almost certainly can use the convert function. You can read more about all the options here.

你几乎可以肯定可以使用转换功能。您可以在此处阅读有关所有选项的更多信息。

If there is some obscure invariant you need, check out this blog by Anubhav Goyal.

如果您需要一些模糊的不变量,请查看Anubhav Goyal的博客。