日期格式转换DD / MM / YYYY输入YYYY-MM-DD不是VARCHAR /日期 [英] Converting dd/MM/yyyy input to yyyy-MM-dd in date format not varchar/datetime

查看:428
本文介绍了日期格式转换DD / MM / YYYY输入YYYY-MM-DD不是VARCHAR /日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索了一整天之后,我无法找到一个解决方案,面临着多重转换错误。主要目的是转换 DD / MM / YYYY YYYY-MM-DD 日期格式,以适应SQL Server中。



我有一个SQL Server 2012数据库的数据类型日期在我的表中的列。



我使用的是与我的项目网站的jquery-1.4.1-vsdoc.js一个jQuery的日期选择器,输出 DD / MM / YYYY 这是使用



获得

 昏暗dDate为DATETIME = DateTime.Parse(的Request.Form(txtStart.UniqueID)) 

然后我试图做使用获得的日期,但数据库的日期格式是插入SQL < 。code> YYYY-MM-DD



有没有试过,但它不工作:

  SELECT CONVERT(日期,CONVERT(DATETIME,'14 / 10/2011',0),120)

尝试这样做,它的工作原理,但它 VARCHAR 数据类型,这不是我所需要的

  SELECT REPLACE(CONVERT(VARCHAR(10),'15 /2014分之4',111),'/',' - ' )AS [YYYY-MM-DD] 

我真的需要一个代码,这可以在我的网站VB / C#代码或SQL语句取其履行本,请帮助



使用jQuery我的剧本已经写在代码中,我的SQL是当前设置的格式,但是在运行时它不适用

 <脚本类型=文/ JavaScript的> 
$(文件)。就绪(函数(){
$(#<%= txtStart.ClientID%GT;)dynDateTime({
showsTime:假的,
ifFormat:%Y-%M-%D,
daFormat:%L,%M%p,%E%M,%Y,
对准:BR,
电:假的,
singleClick:假的,
displayArea:.siblings('dtcDisplayArea。'),
键:。接下来()
});
});
< / SCRIPT>


解决方案

主要目标是一个DD / MM / YYYY转换为YYYY-MM-DD日期格式,以适应在SQL




不要!你应该避免字符串转换至于你所能。让您的数据在它的自然表现为尽可能多的时间就可以了。



您可能需要解析从 DD / MM日期/ YYYY 的DateTime 来开始(虽然如果你能避免连这个,这样做 - 我们不知道你的数据来自) ,但你应该的离开的它作为一个的DateTime 。使用参数化的SQL相反,参数的值传递给数据库作为的DateTime 。当然,你要的的确保在你的数据库中的字段使用适当的类型开始(而不是 VARCHAR )。确保您的模式相匹配的数据是运行一个健全的制度是至关重要的。



解析部分应该是比较简单的。例如:

 日期时间日期= DateTime.ParseExact(文字,DD / MM / YYYY,
的CultureInfo。 InvariantCulture的);

您的可能的要使用用户的文化,而不是(这取决于所在数据来源于),你的可能的要使用 DateTime.TryParseExact 而不是(如果这是从机器对用户输入的数据,而不是东西-machine通讯)。


After searching for a full day, I'm unable to find a solution, facing multiple conversion errors. Main objective is to convert a dd/MM/yyyy to yyyy-MM-dd "date" format to fit in SQL Server.

I have a SQL Server 2012 database with datatype date as a column in my table.

I'm using a jQuery datepicker with jquery-1.4.1-vsdoc.js on my project website that outputs dd/MM/yyyy which is obtained using

Dim dDate As DateTime = DateTime.Parse(Request.Form(txtStart.UniqueID))

Then I'm attempting to do an insert SQL using the date obtained but the database date format is yyyy-MM-dd.

Have tried this but it doesn't work:

SELECT CONVERT(date, CONVERT(DATETIME, '14/10/2011', 0), 120)

Tried this and it works but it's of varchar data type which is not what I need

SELECT REPLACE(CONVERT(VARCHAR(10), '15/4/2014', 111), '/', '-')  AS [YYYY-MM-DD]

I really need a code either on my website VB/C# code or SQL statement whichever to fulfill this please help

My script using the jQuery has already written in code the format that my sql is currently set, however at runtime it does not apply

<script type="text/javascript">
$(document).ready(function () {
    $("#<%=txtStart.ClientID %>").dynDateTime({
        showsTime: false,
        ifFormat: "%Y-%m-%d ",
        daFormat: "%l;%M %p, %e %m, %Y",
        align: "BR",
        electric: false,
        singleClick: false,
        displayArea: ".siblings('.dtcDisplayArea')",
        button: ".next()"
    });
});
</script>

解决方案

Main objective is to convert a dd/MM/yyyy to yyyy-MM-dd "date" format to fit in SQL

Don't! You should avoid string conversions as far as you possibly can. Keep your data in its natural representation for as much of the time as you can.

You may well need to parse the date from dd/MM/yyyy to DateTime to start with (although if you can avoid even this, do so - we don't know where your data comes from), but then you should leave it as a DateTime. Use parameterized SQL instead, passing the value of the parameter to the database as a DateTime. Of course, you should also make sure that the fields in your database are using the appropriate type to start with (not varchar). Ensuring that your schema matches your data is crucial to running a sane system.

The parsing part should be relatively simple. For example:

DateTime date = DateTime.ParseExact(text, "dd/MM/yyyy",
                                    CultureInfo.InvariantCulture);

You may want to use a user's culture instead (depending on where the data has come from) and you may want to use DateTime.TryParseExact instead (if this is user-entered data rather than something from machine-to-machine communication).

这篇关于日期格式转换DD / MM / YYYY输入YYYY-MM-DD不是VARCHAR /日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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