对使用SQL数据类型感到困惑 [英] Confused about using SQL data type

查看:106
本文介绍了对使用SQL数据类型感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Sql数据库中存储月份(字符串)和年份(数字)例如:2013年1月.sql server中是否有任何预定义的数据类型。如果不是我怎么能实现这一目标?谢谢。

I want to store month(string) and year(numeric) in Sql database Eg : January 2013. Is there any predefined data type available in sql server. If not how can I achieve this? Thank you.

推荐答案

您应该根据您的要求将日期/时间值存储到具有以下sql日期类型之一的字段中:日期和时间数据类型和函数(Transact-SQL) [ ^ ]

然后可以使用自定义日期和时间格式字符串 [ ^ ]使用c#等代码进行演示vb.net。
You should store the date/time value into a field with one of these sql date type based on your requirement: Date and Time Data Types and Functions (Transact-SQL)[^]
These date values can then be retrieved and formatted using Custom Date and Time Format Strings[^] for presentation using code like c# or vb.net.


不要。

相反,请查看使用DATETIME(甚至是DATE)数据类型。它们都没有存储格式化的日期,但是它们以一种格式存储数据,它不仅可以用于演示。

将它存储为本月的第一天,在DATETIME中,午夜,并将其转换为适当的字符串,以便在演示文稿软件中显示(通常非常简单)。这样,无论用户想要显示哪种语言,它都可以无缝地工作,您可以更轻松地进行比较和分组。 (您也不必处理数据库中潜在的损坏日期,例如2013年1月或2013年6月等等 - 这可能是以后修复的噩梦。)
Don't.
Instead, look at using a DATETIME (or even a DATE) datatype instead. Neither of them store a formatted date, but they store the dat in a format it can be used for a lot more than just presentation.
Store it as the first of the month, in a DATETIME, at midnight, and convert it to the appropriate string for presentation in your presentation software (which is normally pretty trivial). That way, it works seamlessly regardless of which language the user wants to display, and you can do comparisons and groupings a lot more easily. (You also don't have to cope with potential corrupt dates in your DB like "2013 January" or "Junuary 2013" and so on - which can be a nightmare to fix later).


将日期保留为将日期保留为1的约定,因此2013年1月将存储为2013年1月1日。使用此变量的任何代码或SQL都必须忽略日期部分。
Use Date with a convention that you leave the day as 1 so January 2013 is stored as 1 Jan 2013. Any code or SQL that uses this variable has to simply ignores the day part.


这篇关于对使用SQL数据类型感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆