从字符串转换日期和/或时间时转换失败。数据类型nvarchar

时间:2017-02-03 13:44:16

标签: sql-server sql-server-2008

我有一个数据类型nvarchar,但是为了在我之间使用,我需要将它更改为date数据类型,但它显示错误。

这是我当前的TimeStamp:2017-02-03 19:12:37.503

select PartNo,Convert(date,WODate,103) 
from   Inspection 
where  Convert(date,WODate,103) between Convert(date,'03-01-2017',103) 
       and Convert(date,'05-01-2017',103)

2 个答案:

答案 0 :(得分:0)

如果可能,您应该将列的数据类型更改为DateTime(或者只是日期,如果这就是您需要的全部内容)。
阅读Aaron Bertrand的Bad habits to kick : choosing the wrong data type以找出原因。

如果那是不可能的,那么在转换为日期时至少应该使用正确的样式:

select PartNo, Convert(date,WODate,121) 
from   Inspection 
where  Convert(date,WODate,120) <= date,'2017-01-03' 
and    Convert(date,WODate,120) <= '2017-01-05'

注意:

  1. 103样式用于具有以下格式的字符串:dd/MM/yyyy。 对于您在字符串中使用的格式(yyyy-MM-dd HH:mm:ss),您需要使用121样式。
  2. 使用字符串表示sql server中的日期时,最好使用格式'yyyyMMdd''yyyy-MM-dd'差不多好。'yyyy-MM-ddTHH:mm:ss'。如果你还要包括时间,请使用<div class="container gt-container-fluid gt-container-form"> <div class="row"> <div class="col-sm-6"> <fieldset class="gt-fieldset"> <legend class="gt-legend">Details</legend> <div class="row gt-row"> <div class="col-6"><label for="title">Requirements Title:</label></div> <div class="col-6"><input id="title" type="text" class="k-textbox" [(ngModel)]="Requirement.Title" /></div> </div> <div class="row gt-row"> <div class="col-12"><label for="details">Requirements Details:</label></div> </div> <div class="row gt-row"> <div class="col-12"> <textarea id="details" class="k-textbox gt-textbox" [(ngModel)]="Requirement.Details"></textarea> </div> </div> <div class="row gt-row"> <div class="col-6"><label for="rating">Requirement Rating:</label></div> <div class="col-6"><kendo-dropdownlist id="ratings" [data]="ratingsData" [textField]="'ProductName'" [valueField]="'ProductID'" [defaultItem]="ratingsPlaceHolder"></kendo-dropdownlist></div> </div> <div class="row gt-row"> <div class="col-6"><label for="status">Approval Status:</label></div> <div class="col-6"><kendo-dropdownlist id="approvalStatus" [data]=""></kendo-dropdownlist></div> </div> </fieldset> <fieldset class="gt-fieldset"> <legend class="gt-legend">Attachments</legend> <div class="row gt-row"> <div class="col-12"><label for="attachments">Drag and drop files of click "Select" to browse:</label></div> </div> <div class="row gt-row"> <div class="col-12"> <kendo-upload id="Attachments" [saveUrl]="" [removeUrl]="" [disabled]=""> </kendo-upload> </div> </div> </fieldset> </div> <div class="col-sm-6"> <kendo-tabstrip> <kendo-tabstrip-tab [title]="'Framework Items'" [selected]="true"> <template kendoTabContent> <div class="row gt-row"> <div class="col-6"><label for="framework">Framework Items:</label></div> <div class="col-6"><kendo-multiselect [data]="frameworkItems" [value]="" [placeholder]="'Framework Items'"></kendo-multiselect></div> </div> <div class="row gt-row"> <div class="col-12 gt-col-addremovebuttons"> <button kendoButton (click)="onButtonClick()" [primary]="true">+</button> <button kendoButton (click)="onButtonClick()" [primary]="true">-</button> </div> </div> <div class="row gt-row"> <div class="col-12"> <kendo-grid [data]="frameworkItemsData" [height]="200"> <kendo-grid-column field="FrameworkLocation" title="ID"> </kendo-grid-column> <kendo-grid-column field="FrameworkTitle" title="Name"> </kendo-grid-column> </kendo-grid> </div> </div> </template> </kendo-tabstrip-tab> <kendo-tabstrip-tab [title]="'Related Actions'"> <template kendoTabContent> <div class="row gt-row"> <div class="col-6"><label for="actions">Related Actions:</label></div> <div class="col-6"><kendo-multiselect [data]="actionItems" [value]="" [placeholder]="'Actions'"></kendo-multiselect></div> </div> </template> </kendo-tabstrip-tab> </kendo-tabstrip> </div> </div> <div class="row gt-row"> <div class="col-12 gt-col-buttons"> <button kendoButton (click)="saveChanges()" [primary]="true">Save</button> <button kendoButton (click)="onButtonClick()" [primary]="false">Edit</button> <button kendoButton (click)="deleteRecord()" [primary]="false">Delete</button> <button kendoButton (click)="onButtonClick()" [primary]="false">Close</button> </div> </div> </div>(注意M代表monthes而m代表分钟,也不要忘记将日期和时间扭曲与大写T分开。)
  3. 您可以在MSDN page on cast and convert.

    中阅读所有相关信息

答案 1 :(得分:0)

看起来您的日期时间格式为yyyy-mm-dd hh:mi:ss.mmm,并且您尝试使用103格式转换它,即dd / mm / yyyy。您的其他硬编码日期遵循该格式,但不能在没有转换的情况下使用文字格式。我建议你使用以下内容:

{{1}}

以下资源具有参考格式:

http://www.w3schools.com/sql/func_convert.asp