Showing posts with label building. Show all posts
Showing posts with label building. Show all posts

Monday, March 19, 2012

Date Insert

I'm a beginner to SQL Server 2005.

I'm building a small form with a SQL2005 database. I'm creating the database and adding a field called DateInsert. When the user clicks the submit button and the form data gets written to my database, how do I automatically generate a timestamp and write it to my DateInsert Field in the database?

Thanks in advance!

Hello,

You can use GetDate() in your insert statement. Such as:

INSERT INTO MyTableName

(ID,FIELD1,FIELD2,TIMESTAMP) VALUES (1,"Example","Example", GetDate())

Hope this helps.

|||

Thanks for your help. That is what I needed.

Is there a way to bind this to the FormView Insert Item on the formview control? I'm looking for the autogenerated code for the control and can't seem to find it.

|||

You can avoid the extra work and just take care of it on the database-end like described above.

|||

Or you can use the getdate() as the default value for that column. This can be done in enterprise manager.

|||

You can use default constraint with the field if you are sure you want the current date time value to appear in the field on every insert. Below is the syntax you can use at the time of creating a table.

create table <table name>( <column name>datetime default (getdate() ), )

If you've already created the table then below is the alter query you can use to set the default value constraint.

alter table <table name>add constraint [df_<table name>_name>]default (getdate() )forname>
 
 If you've added the default constraint for any column using one of above queries then you don't need to include that column in the insert list,
the default value will automatically inserted for that column.
The way is provide getdate() as the value for that field in the Insert query.
Hope this will help. 

Date inputs (UK format) in SQL svr 2000 with OLE DB

Hi all,

I'm building an ASP app on a Windows 2000/IIS machine which interfaces
with our SQL Server 2000 database via OLE DB.

Since we're based in the UK I want the users to be able to type in
dates in UK date format to input into the database. In Enterprise
Manager on the SQL Server I can manually enter a record into a table
and just type in a UK date (MM/DD/YYYY e.g. 25/12/2004) and it accepts
it happily.

However, if I enter the exact same record on the form on the web page,
again using the UK date format, I get this back from the IIS box:

HTTP 500.100 - Internal Server Error - ASP error
Internet Information Services

Technical Information (for support personnel)

Error Type:
Microsoft OLE DB Provider for SQL Server (0x80040E07)
The conversion of a char data type to a datetime data type resulted
in an out- of-range datetime value.
/ictest/eventadm.asp, line 78

I'm assuming that despite SQL Server accepting the date in UK format,
OLE DB will not. Can the OLE provider be configured to allow such
date formats, either (I imagine) in the registry or by altering the
connection string?

TIA,
NiallMake sure the SQL Server user is a UK English User otherwise SQL Server will
do this

(presume user language is set to English (American))

This guy is American so he is giving me a date of 25/12/2004. Americans use
MMDDYYYY so what he means is

the 12th day of the 25th month

this as we can guess is way out of range.

Here is another example

SET LANGUAGE us_english
GO

select cast('25/12/2004' as datetime)

Changed language setting to us_english.
Server: Msg 242, Level 16, State 3, Line 2
The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value.

SET LANGUAGE British
GO

select cast('25/12/2004' as datetime)

2004-12-25 00:00:00.000

--
--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.SQLDTS.com - The site for all your DTS needs.
www.konesans.com - Consultancy from the people who know

"Niall Porter" <niallporter@.yahoo.co.uk> wrote in message
news:2db8d05e.0407280624.773f87ff@.posting.google.c om...
> Hi all,
> I'm building an ASP app on a Windows 2000/IIS machine which interfaces
> with our SQL Server 2000 database via OLE DB.
> Since we're based in the UK I want the users to be able to type in
> dates in UK date format to input into the database. In Enterprise
> Manager on the SQL Server I can manually enter a record into a table
> and just type in a UK date (MM/DD/YYYY e.g. 25/12/2004) and it accepts
> it happily.
> However, if I enter the exact same record on the form on the web page,
> again using the UK date format, I get this back from the IIS box:
>
> HTTP 500.100 - Internal Server Error - ASP error
> Internet Information Services
> Technical Information (for support personnel)
> Error Type:
> Microsoft OLE DB Provider for SQL Server (0x80040E07)
> The conversion of a char data type to a datetime data type resulted
> in an out- of-range datetime value.
> /ictest/eventadm.asp, line 78
> I'm assuming that despite SQL Server accepting the date in UK format,
> OLE DB will not. Can the OLE provider be configured to allow such
> date formats, either (I imagine) in the registry or by altering the
> connection string?
> TIA,
> Niall|||"Niall Porter" <niallporter@.yahoo.co.uk> wrote in message
news:2db8d05e.0407280624.773f87ff@.posting.google.c om...
> Hi all,
> I'm building an ASP app on a Windows 2000/IIS machine which interfaces
> with our SQL Server 2000 database via OLE DB.
> Since we're based in the UK I want the users to be able to type in
> dates in UK date format to input into the database. In Enterprise
> Manager on the SQL Server I can manually enter a record into a table
> and just type in a UK date (MM/DD/YYYY e.g. 25/12/2004) and it accepts
> it happily.
> However, if I enter the exact same record on the form on the web page,
> again using the UK date format, I get this back from the IIS box:
>
> HTTP 500.100 - Internal Server Error - ASP error
> Internet Information Services
> Technical Information (for support personnel)
> Error Type:
> Microsoft OLE DB Provider for SQL Server (0x80040E07)
> The conversion of a char data type to a datetime data type resulted
> in an out- of-range datetime value.
> /ictest/eventadm.asp, line 78
> I'm assuming that despite SQL Server accepting the date in UK format,
> OLE DB will not. Can the OLE provider be configured to allow such
> date formats, either (I imagine) in the registry or by altering the
> connection string?
> TIA,
> Niall

The best way to handle this would probably be to use a date format which
works regardless of any client and server settings - there are two:

'20040728' -- no time needed
'2004-07-28T20:20:00' -- ISO8601, includes time

Although I don't know much about ADO, I believe that if ADO is aware that
the target is a datetime parameter or column, it will automatically handle
the conversion for you. That might be one option for you - pass the value to
a stored procedure, and when you use the Command.CreateParameter method, ADO
will be aware that the parameter is datetime. But I admit that I haven't
tried it myself, so I may be wrong about this.

Simon|||Niall Porter (niallporter@.yahoo.co.uk) writes:
> Since we're based in the UK I want the users to be able to type in
> dates in UK date format to input into the database. In Enterprise
> Manager on the SQL Server I can manually enter a record into a table
> and just type in a UK date (MM/DD/YYYY e.g. 25/12/2004) and it accepts
> it happily.
> However, if I enter the exact same record on the form on the web page,
> again using the UK date format, I get this back from the IIS box:
>
> HTTP 500.100 - Internal Server Error - ASP error
> Internet Information Services
> Technical Information (for support personnel)
> Error Type:
> Microsoft OLE DB Provider for SQL Server (0x80040E07)
> The conversion of a char data type to a datetime data type resulted
> in an out- of-range datetime value.
> /ictest/eventadm.asp, line 78
> I'm assuming that despite SQL Server accepting the date in UK format,
> OLE DB will not. Can the OLE provider be configured to allow such
> date formats, either (I imagine) in the registry or by altering the
> connection string?

So how does your code look like? Since you get the error from OLE DB,
I assume that you are passing the value as a parameter to a prepared
statement, and that you declare the parameter to be of type
adDBTimeStamp. In such case you must use ISO format, 2004-12-25.

If you want to support regional settings, you should use some ASP
routine to convert the date value. CDate() might be your guy, but
we're straying beyond the realms of this newsgroup. You may want to
try an ASP forum.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks for all the help from everyone so far, I'm not quite out of the
woods yet tho!

"Allan Mitchell" <allan@.no-spam.sqldts.com> wrote in message news:<4107c9f7$0$7125$db0fefd9@.news.zen.co.uk>...
> Make sure the SQL Server user is a UK English User otherwise SQL Server will
> do this
> (presume user language is set to English (American))

The database user (a pure SQL Server user) was set to English. I
found another language called "British English", I've changed that
user to British English and now I can input dates in a UK format
(DD/MM/YYYY) which is great, thanks for that. I appreciate the 'best'
way to do it is to use an ISO format but my users wouldn't cope with
such technicalities as this...

However, dates *returned* are still in US format (MM/DD/YYYY)! Is
there some other esoteric setting I have to tweak somewhere?

> This guy is American so he is giving me a date of 25/12/2004. Americans use
> MMDDYYYY so what he means is
> the 12th day of the 25th month
> this as we can guess is way out of range.
>
> Here is another example
> SET LANGUAGE us_english
> GO

Won't that set it for the all databases on the whole server? We have
a number of applications that use the SQL server for their databases,
some of which may well have been developed in the states. If I change
the setting for the whole server, any other applications that don't
use the ISO or other non-regional date formatting will break surely?

> select cast('25/12/2004' as datetime)

Tried this also, didn't make any difference.

> Changed language setting to us_english.
> Server: Msg 242, Level 16, State 3, Line 2
> The conversion of a char data type to a datetime data type resulted in an
> out-of-range datetime value.
> SET LANGUAGE British
> GO
> select cast('25/12/2004' as datetime)
> 2004-12-25 00:00:00.000
> --
> --
> Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
> www.SQLDTS.com - The site for all your DTS needs.
> www.konesans.com - Consultancy from the people who know
>|||Niall Porter (niallporter@.yahoo.co.uk) writes:
> However, dates *returned* are still in US format (MM/DD/YYYY)! Is
> there some other esoteric setting I have to tweak somewhere?

I'm still not sure why you post this in an SQL Server forum, I would
expect this to be an ASP problem. Then again, since you have not posted
any code, I have no idea of what you are doing.

>> SET LANGUAGE us_english
>> GO
> Won't that set it for the all databases on the whole server? We have
> a number of applications that use the SQL server for their databases,
> some of which may well have been developed in the states. If I change
> the setting for the whole server, any other applications that don't
> use the ISO or other non-regional date formatting will break surely?

SET LANGAUGE changes the language for the current session. However, this
option controls how date literals are interpreted on *SQL Server* and
you got the conversion error on client level.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Erland.

If the client connects as a US user on a UK server entering a data of
'25/12/2004' then wouldn't the error returned be this error. My expereince
of mixing regional settings says it would.

--
--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.SQLDTS.com - The site for all your DTS needs.
www.konesans.com - Consultancy from the people who know

"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns9536ECDAE92D1Yazorman@.127.0.0.1...
> Niall Porter (niallporter@.yahoo.co.uk) writes:
> > However, dates *returned* are still in US format (MM/DD/YYYY)! Is
> > there some other esoteric setting I have to tweak somewhere?
> I'm still not sure why you post this in an SQL Server forum, I would
> expect this to be an ASP problem. Then again, since you have not posted
> any code, I have no idea of what you are doing.
> >> SET LANGUAGE us_english
> >> GO
> > Won't that set it for the all databases on the whole server? We have
> > a number of applications that use the SQL server for their databases,
> > some of which may well have been developed in the states. If I change
> > the setting for the whole server, any other applications that don't
> > use the ISO or other non-regional date formatting will break surely?
> SET LANGAUGE changes the language for the current session. However, this
> option controls how date literals are interpreted on *SQL Server* and
> you got the conversion error on client level.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp|||Allan Mitchell (allan@.no-spam.sqldts.com) writes:
> If the client connects as a US user on a UK server entering a data of
> '25/12/2004' then wouldn't the error returned be this error. My
> expereince of mixing regional settings says it would.

Depends. The error Neill got was:

Microsoft OLE DB Provider for SQL Server (0x80040E07)
The conversion of a char data type to a datetime data type resulted
in an out-of-range datetime value.

The only way to get that error from Query Analyzer is to connect a linked
server with a date-format conflict along the line.

The problem here is that things can to wrong on several levels. In Neill's
case it got wrong already on client level. I suspect because he was
using adDBTimeStamp to which he must feed an ISO format.

It is also confusing since the regional settings on the client neither the
server on what happens in SQL Server which uses its own language
definitions. You can set language when you connect, but then you have to
pass that in the connection string - and it has to be a language as SQL
Server knows it.

The main problem in Neill's case is that he has not posted any code, so
we are left to guessing.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Sunday, March 11, 2012

Date frustration

I screwed up when building my application, now I need to try to fix it...
Initially I setup my dates in a varchar field, now I need them in a datetime
field...
I have the following query that is acting funny,
UPDATE tblEmpPersonalInfo SET
SepDate = EmpSWVTCSepDate
This query will not successfully run, giving me this error,
The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value.
The statement has been terminated.
But, if I edit the query like this,
UPDATE tblEmpPersonalInfo SET
SepDate = EmpSWVTCSepDate
WHERE EmpID = 'aa381234'
then the query will work fine... I don't want to have to run the query for
every employee in the database, so how can I do this?
Thanks,
DrewUse ISDATE() function to identify if the data in the column are truly date
values. Check out SQL Server Books Online for more details on this function.
Anith|||Hi
Check that all the values in the EmpSWVTCSepDate column are actually valid
dates.
SELECT * FROM tblEmpPersonalInfo
WHERE ISDATE(EmpSWVTCSepDate) = 0
Regards
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Drew" <drew.laing@.NOswvtc.dmhmrsas.virginia.SPMgov> wrote in message
news:e6aUZElAFHA.2792@.TK2MSFTNGP15.phx.gbl...
> I screwed up when building my application, now I need to try to fix it...
> Initially I setup my dates in a varchar field, now I need them in a
datetime
> field...
> I have the following query that is acting funny,
> UPDATE tblEmpPersonalInfo SET
> SepDate = EmpSWVTCSepDate
> This query will not successfully run, giving me this error,
> The conversion of a char data type to a datetime data type resulted in an
> out-of-range datetime value.
> The statement has been terminated.
> But, if I edit the query like this,
> UPDATE tblEmpPersonalInfo SET
> SepDate = EmpSWVTCSepDate
> WHERE EmpID = 'aa381234'
> then the query will work fine... I don't want to have to run the query for
> every employee in the database, so how can I do this?
> Thanks,
> Drew
>|||Thanks... There was 1 invalid date in there, 10-10--2003
Drew
"Mike Epprecht (SQL MVP)" <mike@.epprecht.net> wrote in message
news:u4k2MPlAFHA.2604@.TK2MSFTNGP10.phx.gbl...
> Hi
> Check that all the values in the EmpSWVTCSepDate column are actually valid
> dates.
> SELECT * FROM tblEmpPersonalInfo
> WHERE ISDATE(EmpSWVTCSepDate) = 0
> Regards
> --
> Mike Epprecht, Microsoft SQL Server MVP
> Zurich, Switzerland
> IM: mike@.epprecht.net
> MVP Program: http://www.microsoft.com/mvp
> Blog: http://www.msmvps.com/epprecht/
> "Drew" <drew.laing@.NOswvtc.dmhmrsas.virginia.SPMgov> wrote in message
> news:e6aUZElAFHA.2792@.TK2MSFTNGP15.phx.gbl...
> datetime
>|||Drew,
1 - Update those without problem
UPDATE tblEmpPersonalInfo SET
SepDate = EmpSWVTCSepDate
where isdate(EmpSWVTCSepDate) = 1
2 - Identify those with problem, correct the problem and update them again.
select * from tblEmpPersonalInfo
where isdate(EmpSWVTCSepDate) = 0
-- fix the problem
...
UPDATE tblEmpPersonalInfo SET
SepDate = EmpSWVTCSepDate
where isdate(EmpSWVTCSepDate) = 1 and SepDate is null
AMB
"Drew" wrote:

> I screwed up when building my application, now I need to try to fix it...
> Initially I setup my dates in a varchar field, now I need them in a dateti
me
> field...
> I have the following query that is acting funny,
> UPDATE tblEmpPersonalInfo SET
> SepDate = EmpSWVTCSepDate
> This query will not successfully run, giving me this error,
> The conversion of a char data type to a datetime data type resulted in an
> out-of-range datetime value.
> The statement has been terminated.
> But, if I edit the query like this,
> UPDATE tblEmpPersonalInfo SET
> SepDate = EmpSWVTCSepDate
> WHERE EmpID = 'aa381234'
> then the query will work fine... I don't want to have to run the query for
> every employee in the database, so how can I do this?
> Thanks,
> Drew
>
>|||Too bad we cannot set some sort of flashing attribute on this message :)
Glad you found your problem, and even more important, glad you are cutting
future problems off at the knee caps. The way a value displays can be dealt
with at runtime, the most important thing is that the value is true!
----
Louis Davidson - drsql@.hotmail.com
SQL Server MVP
Compass Technology Management - www.compass.net
Pro SQL Server 2000 Database Design -
http://www.apress.com/book/bookDisplay.html?bID=266
Note: Please reply to the newsgroups only unless you are interested in
consulting services. All other replies may be ignored :)
"Drew" <drew.laing@.NOswvtc.dmhmrsas.virginia.SPMgov> wrote in message
news:ewv0xWlAFHA.1388@.TK2MSFTNGP09.phx.gbl...
> Thanks... There was 1 invalid date in there, 10-10--2003
> Drew
> "Mike Epprecht (SQL MVP)" <mike@.epprecht.net> wrote in message
> news:u4k2MPlAFHA.2604@.TK2MSFTNGP10.phx.gbl...
>

Thursday, March 8, 2012

Date format passing parameter to WebService

Hi Lads,
I'm having this wee problem with the date format of a parameter - I'm
building a list of the needed parameters on a web page (determined from the
report) via the API, I have set the report to English(Ireland), I assumed
this would take care of the date format (dd/MM/yyyy) rightly or wrongly I'm
not too sure - reigonal settings on my PC are the same. The problem that I
have is that when I pass back the date 30/09/2005 it comes back with an error
- any suggestions? they would be most appreciated.You can use strings instead DateTime, and you will avoid any TimeZone
convertion and reginal settings.
Jerry
http://www.rdlcomponents.com
"Rowan Massey" wrote:
> Hi Lads,
> I'm having this wee problem with the date format of a parameter - I'm
> building a list of the needed parameters on a web page (determined from the
> report) via the API, I have set the report to English(Ireland), I assumed
> this would take care of the date format (dd/MM/yyyy) rightly or wrongly I'm
> not too sure - reigonal settings on my PC are the same. The problem that I
> have is that when I pass back the date 30/09/2005 it comes back with an error
> - any suggestions? they would be most appreciated.|||Hi Jerry,
Unfortunately I can't use strings! I actually build up parameters on the web
page depending on their type in the report itself! i.e. for datetime
parameters, I create a text box an image control next to it to popup a date
selection screen, for a string I create a textbox only etc. I'm trying to
make it as generic as possible so I only have to do this once!
"Jerry" wrote:
> You can use strings instead DateTime, and you will avoid any TimeZone
> convertion and reginal settings.
> Jerry
> http://www.rdlcomponents.com
> "Rowan Massey" wrote:
> > Hi Lads,
> >
> > I'm having this wee problem with the date format of a parameter - I'm
> > building a list of the needed parameters on a web page (determined from the
> > report) via the API, I have set the report to English(Ireland), I assumed
> > this would take care of the date format (dd/MM/yyyy) rightly or wrongly I'm
> > not too sure - reigonal settings on my PC are the same. The problem that I
> > have is that when I pass back the date 30/09/2005 it comes back with an error
> > - any suggestions? they would be most appreciated.