想要在sql中验证并保存日期 [英] wants to validate and save dates in sql

查看:74
本文介绍了想要在sql中验证并保存日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我再次提出问题.我想捕获访问网站的人的学术细节.我的表格捕获来访者的研究生/hsc(10 + 2)ssc(10)信息,即大学的名称/10和12 board.stream(即ba,bcom,bsc或其他),以及每个年级的及格率.以及过去的年份.

第一个问题是如何验证日期字段,即,它只能允许4位数的整数值

第二个问题是在sql server(2005)中是否有任何类型为date的变量,它将只存储日期而不是时间,因为我想存储该年份的过去年份

请帮助

谢谢
anoop

hi here i am again asking question. i want to capture academics detail of person hitting website. my form captures visitors graduate/hsc(10+2)ssc(10) information i.e its name of university/10 and 12 board.stream in university(ie ba,bcom,bsc or other)and passing percent in each grade. as well as year of passing.

first question is how to validate date field i.e it should only allow 4 digit int values

second question is there any variable of type as date in sql server(2005) which would only store date and not time because i want to store year of passing in that feild

plz help

thanks
anoop

推荐答案



对于第一个问题,您可以使用正则表达式验证控件,例如

表达式为 \ d {4} ,或者您可以使用自己的javascript方法

对于第二个问题,据我所知,仅日期格式没有特定的数据类型.

我们仅将日期传递到该列,之后我们检索值并从该值中仅截取日期.
默认情况下,它花费的时间像... 12:00:00的格式

所以在其中没有问题,我们可以在从数据库读取数据时进行操作


最好的
Hi,

For first question you can use regularexpression validation control like

expression is \d{4} or you can use your own javascript method

For second question ,upto my knowledge there is no specific datatype for only date format.

We pass only date to that column after that we retrieve the value and cut only date from that value.
by default it takes time as like ... 12:00:00 in format

so no problem in it we can manipulate while reading data from database


All the Best



对于第二个问题

不,在sql Server 2005中没有日期"数据类型,但是在SqlServer 2008中引入.
存储时,请尝试保存DateTime的Date部分..
Hi
For the second question

No, there is no "date" data type in sql server 2005, but introduced in SqlServer 2008.

When you store try to save Date part of DateTime..


您不希望查询中的时间(hh:mm:ss)部分,然后使用转换功能

----从表中检索信息并返回日期(数据类型)值
-----作为字符mm/dd/yyyy

选择CONVERT(varchar(10),DateTimeColumnName,101)作为MyDate
来自TableName



选择CONVERT(varchar(10),GETDATE(),101)作为charSystemDate

验证日期(使用T-SQL的数据类型)

对于通过存储过程的实例T-SQL
----------程序的开始--------------------------
创建PROC uspStoreData
@dtDate varchar(50)


声明@dtRealDate datetime

IF ISDATE(@dtDate)= 1
开始
SET @dtRealDate = CONVERT(datetime,@ dtDate)
结束

-----插入tableName(DateTimeColumnName)值(@dtRealDate)

----更新tableName设置DateTimeColumnName = @ dtRealDate where子句此处

----------过程结束------------------------

-------------------------------------------------- ---
使用ASP.NET,C#,VB.NET:
-------------------------------------------------- ---
如果您需要一个好的工具来验证来自表单的输入,那么
下载我的一个朋友整理好的免费工具
使用ASP.NET,C#,VB.NET(Windows窗体和Web窗体
http://www.netstair.com/download/ncTools/ncTools-Lib.zip

----转换为日期/时间yyyy-mm-dd hh:mm:ss(一旦下载,请按照以下说明使用)
ncTools.oUtilities oObj =新的ncTools.oUtilities()
DateTime dtDateInputed
如果(oOBJ.IsDate(textbox1.text))
{
dtDateInputed = Convert.DateTime(textbox1.text);
}
-----转换为字符串mm/dd/yyyy
字符串strDateInputed = Convert.DateTime(textbox1.text).ToShortDateString();



祝你好运
Al
you do not want the time (hh:mm:ss) portion from the query then use convert function

---- Retrieve info from table and return date (datatype) value
-----as character mm/dd/yyyy

select CONVERT(varchar(10),DateTimeColumnName,101) as MyDate
from TableName

OR

select CONVERT(varchar(10),GETDATE(),101) as charSystemDate

Validate date (datatype with T-SQL)

For Instance T-SQL via Store procedure
---------- Begin of Proc----------------------
CREATE PROC uspStoreData
@dtDate varchar(50)
as

DECLARE @dtRealDate datetime

IF ISDATE(@dtDate)=1
BEGIN
SET @dtRealDate = CONVERT(datetime,@dtDate)
END

----- Insert into tableName (DateTimeColumnName) values(@dtRealDate)
OR
---- update tableName set DateTimeColumnName=@dtRealDate where clause here

---------- End of Proc----------------------

-----------------------------------------------------
With ASP.NET, C#, VB.NET:
-----------------------------------------------------
if you need a good tools to validate input from a form then
download the free tools that a friend of mine put together that works
with ASP.NET, C#, VB.NET (Windows form and Web Forms
http://www.netstair.com/download/ncTools/ncTools-Lib.zip

---- Convert to Date/Time yyyy-mm-dd hh:mm:ss (Once downloaded use as follow)
ncTools.oUtilities oObj = new ncTools.oUtilities()
DateTime dtDateInputed
if (oObj.IsDate(textbox1.text))
{
dtDateInputed = Convert.DateTime(textbox1.text);
}
----- Convert to String mm/dd/yyyy
string strDateInputed = Convert.DateTime(textbox1.text).ToShortDateString();



Good Luck
Al


这篇关于想要在sql中验证并保存日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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