对使用SQL数据类型感到困惑

编程入门 行业动态 更新时间:2024-10-25 12:27:14
本文介绍了对使用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。 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.

不要。 相反,请查看使用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数据类型感到困惑

本文发布于:2023-10-25 14:19:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据类型   困惑   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!