- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用过程输入 30/10/1988 作为表中 DOB 列的日期
alter procedure addCustomer
@userName varchar(50),
@userNIC varchar(50),
@userPassword varchar(100),
@userDOB datetime,
@userTypeID int,
@userEmail varchar(50),
@userTelephone int,
@userAddress char(100),
@userCityID int,
@status int output
as
declare @userID int
declare @eid int
declare @tid int
declare @aid int
execute getLastRaw 'userID','tblUserParent', @userID output
insert into tblUserParent values (@userID, @userName, @userNIC, @userPassword, @userDOB, @userTypeID)
execute getLastRaw 'addressID','tblAddress', @aid output
insert into tblAddress values (@aid, @userAddress, @userID, @userCityID)
execute getLastRaw 'emailID','tblEmail', @eid output
insert into tblEmail values (@eid, @userEmail, @userID)
execute getLastRaw 'telephoneID','tblTelephoneNO', @tid output
insert into tblTelephoneNO values (@tid, @userTelephone , @userID)
insert into tblUserCustomer values (@userID, @eid , @tid, @aid)
最佳答案
如果你真的想避免基于模糊日期的可能性,那么你应该总是在两个 unambiguous date formats 之一中输入它。已经选择了答案并且它是有效的,但我相信传播知识;)
正如@cloud 和我的帖子所注意到的那样,我的帖子代表了一个年轻的、不太聪明的我,只提供了一个链接的答案,我会弹出 archive 的内容Jamie Thompson 对 TSQL 中明确日期格式的回答
tl;博士;
One of the most commonly used data types in SQL Server is [datetime] which unfortunately has some vagaries around how values get casted. A typical method for defining a [datetime] literal is to write it as a character string and then cast it appropriately. The cast syntax looks something like this: DECLARE @dt NVARCHAR(19) = '2009-12-08 18:00:00';
SELECT CAST(@dt AS datetime);
Unfortunately in SQL Server 2005 the result of the cast operation may be dependent on your current language setting. You can discover your current language setting by executing: SELECT @@LANGUAGE To demonstrate how your language setting can influence the results of a cast take a look at the following code: ALTER DATABASE tempdb
SET COMPATIBILITY_LEVEL = 90 ; --Behave like SQL Server 2005
USE tempdb
GO
DECLARE @t TABLE (
dateString NVARCHAR(19)
);
INSERT @t (dateString)
VALUES ('2009-12-08 18:00:00') --'yyyy-MM-dd hh24:mi:ss'
, ('2009-12-08T18:00:00') --'yyyy-MM-ddThh24:mi:ss'
, ('20091208 18:00:00') --'yyyyMMdd hh24:mi:ss'
SET LANGUAGE french;
SELECT 'french' AS lang
, DATENAME(MONTH,q.[dt]) AS mnth
, q.[dt]
FROM (
SELECT CAST(dateString AS DATETIME) AS dt
FROM @t
)q;SET LANGUAGE us_english;
SELECT 'us_english' AS lang
, DATENAME(MONTH,q.[dt]) AS mnth
, q.[dt]
FROM (
SELECT CAST(dateString AS DATETIME) AS dt
FROM @t
)q; We are taking the value which can be described in words as “6pm on 8th December 2009”, defining it in three different ways, thenseeing how the @@LANGUAGE setting can affect the results. Here are those results: french language datetime Notice how the interpretation of the month can change depending on @@LANGUAGE. If @@LANGUAGE=’french’ then the string '2009-12-08 18:00:00' is interpreted as 12th August 2009 (‘août’ is French for August for those that don’t know) whereas if @@LANGUAGE=’us_english’ it is interpreted as 8th December 2009. Clearly this is a problem because the results of our queries have a dependency on a server-level or connection-level setting and that is NOT a good thing. Hence I recommend that you only define [datetime] literals in one of the two unambiguous date formats: yyyy-MM-ddTHH24:mi:ss yyyyMMdd HH24:mi:ss That was going to be the end of this blog post but then I found out that this behaviour changed slightly in SQL Server 2008. Take the following code (see if you can figure out what the results will be before I tell you): ALTER DATABASE tempdb
SET COMPATIBILITY_LEVEL = 100 ; --Behave like SQL Server 2008
GO
USE tempdb
GO
SET LANGUAGE french;
DECLARE @dt NCHAR(10) = '2009-12-08 18:00:00'; --Ambiguous date format
SELECT CAST(@dt AS datetime) AS [ExplicitCast]
, DATENAME(MONTH,@dt) AS [MonthFromImplicitCast]
, DATENAME(MONTH,CAST(@dt AS datetime)) AS [MonthFromExplicitCast]; Here we are doing three different things with our nchar literal: explicitly cast it as a [datetime] extract the month name from the char literal using the DATENAME function (which results in an under-the-covers implicit cast) extract the month name from the char literal using the DATENAME function after it has been explicitly casted as a [datetime] Note that the compatibility level is set to SQL Server 2008 and @@LANGUAGE=’french’. Here are the results: image (Were you correct?) Let’s take a look at what is happening here. The behaviour when we are explicitly casting as [datetime] hasn’t changed, our nchar literal is still getting interpreted as 12th August rather than 8th December when @@LANGUAGE=’french’. The [MonthFromExplicitCast] field is interesting though, it seems as though the implicit cast has resulted in the desired value of 8th December. Why is that? To get the answer we can turn to BOL’s description of the DATENAME function syntax: image The implicit cast is not casting to [datetime] at all, it is actually casting to [date] which is a new datatype in SQL Server 2008. The new date-related datatypes in SQL Server 2008 (i.e. [date], [datetime2], [time], [datetimeoffset]) disregard @@LANGUAGE and hence we get behaviour that is more predictable and, frankly, better. These new behaviours for SQL Server 2008 were unknown to me when I began this blog post so I have learnt something in the course of authoring it, I hope it has helped you too. No doubt someone somewhere is going to get nastily burnt by this at some point, make sure that it isn’t you by always using unambiguous date formats: yyyy-MM-ddTHH24:mi:ss yyyyMMdd HH24:mi:ss regardless of which version you are on!
关于sql - 如何在TSQL中的表中输入日期? (将数据类型 varchar 转换为 datetime 时出错),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6365871/
我在 presto 中有表,名为(“mappings”)的列将键值对作为字符串 从 hello 中选择映射; 例如:{“foo”:“baar”,“foo1”:“bar1”} 我想将“映射”列转换为 M
我总是会在表格的特定列中输入 20 个字符的内容。 我需要此列是唯一的。 如果我将此列设置为 varchar(255) 而不是 varchar(20),SELECT 查询的速度会有任何差异吗? (输入
我在 the Dapper .NET project home page 上发现了以下评论. Dapper supports varchar params, if you are executing
我有以下代码。 Case 语句将列出的数字转换为文本并将其余代码转换为 NULL,但我不断收到以下错误:将 VARCHAR 值“RDG5”转换为数据类型 INT 时转换失败。 RDG5 是被转换为 N
运行此脚本时出现以下错误。我尝试过使用以下内容:整理 Latin1_General_CI_AS。请问可以排序吗?谢谢 Msg 457, Level 16, State 1, Line 8 Implic
是否可以将 SQL Server 2008 数据库中的列类型从 varchar(255) 更改为 varchar(MAX),而无需删除表并重新创建? 每次我尝试使用它来执行此操作时,SQL Serve
每次我对选择 varchar(max) 或 varchar(fix) 数据类型感到困惑。假设我有一个大约 5000 个 varchar 的数据列。列不是 null 类型。 我应该将其设置为 varch
您好,我遇到问题,我的 friend 拒绝更改字段的数据类型,所以我在使用 order by 时遇到问题,这里是示例数据 04-07-2016(mm-dd-yyyy) 和字段名称名为 regis_da
对于文字游戏,我正在尝试向 VARCHAR 数组添加 CHECK 约束: CREATE TABLE words_games ( gid SERIAL PRIMARY KEY,
我有一个 varchar 字段,其内容如下: a,b,c,d e,d,a,c b,q,d,e 我需要执行一个查询,仅选择具有与输入字符串相等的元素的字段的行。 前输入:c,a 选择的行: a,b,c,
大家好,我计划创建包含 10 列的表,该表应该至少有 10,000,000 行,并且在其中,我将有列 description - VARCHAR(600) 和索引。 所以问题是,在该列上查询 LIKE
我读过这个question关于MySQL中VARCHAR(254)和VARCHAR(255)的区别。 HiveQL 中是否有必须考虑的类似内容?也许 HiveQL 实现了一些类似于 MySQL 的存储
在 MySQL 中,VARCHAR(1024) 和 VARCHAR(512) 有什么区别?如果我的项目永远不会超过 512 个字符,那么使用 VARCHAR(1024) 我会失去什么? 最佳答案 不知
由于 Varchar 字段的存储要求基于输入的字符串的实际长度,将每个 Varchar 字段指定为最大可能的缺点是什么:Varchar (65535)?那么,除了最大字段 > 255 个字符的 1 个
我正在尝试搜索具有数据类型 map(varchar,varchar) 的列。现在访问列的一种方法是使用这个结构,name_of_column[' key '],它将给出该键的值。但我想知道什么是可能的
快速提问。如果我将使用十进制字段限制或十六进制(比如 16、32、64 而不是 10、20、50),从存储数据的角度来看是否重要? 我问是因为我想知道这是否与 HDD 上的集群有关? 谢谢! 最佳答案
我发现我可以写 SELECT CAST(Min(mynumber) AS VARCHAR(Max))+'mystring' AS X 作为 SELECT CAST(Min(mynumber) AS V
我必须将字符串“johnmelling”值插入到列为 的表中[用户密码] varbinary NOT NULL。 请有人建议我,插入“johnmelling”的最佳转换是什么? 我尝试插入如下, In
我已经在 MSDN 论坛和此处阅读了此内容,但仍然不清楚。我认为这是正确的: Varchar(max) 将存储为文本数据类型,因此有缺点。假设您的字段可靠地少于 8000 个字符。就像我的数据库表中的
我有一个这样描述的表: mysql> describe easy_table; +---------------------+--------------+------+-----+---------
我是一名优秀的程序员,十分优秀!