Showing posts with label form. Show all posts
Showing posts with label form. Show all posts

Sunday, March 25, 2012

Date Part of the date time

I have data in a smalldatetime column but the dates are
mixed. Some in the '3/7/04 11:47:00 AM' form and some
in '3/7/04' (This is when I open the table from EM).
When I select from the Query Analyzer I get
'2004-01-08 13:35:00'
My question is How can I select only the date part of it
(Not the time part) to compare with another column ?
Thnaks for any help.......select convert (varchar, <date>,101) returns the date in mm/dd/yyyy format.
It comes out as a string. That can easily be compared against another date.
****************************************
***************************
Andy S.
MCSE NT/2000, MCDBA SQL 7/2000
andymcdba1@.NOMORESPAM.yahoo.com
Please remove NOMORESPAM before replying.
Always keep your antivirus and Microsoft software
up to date with the latest definitions and product updates.
Be suspicious of every email attachment, I will never send
or post anything other than the text of a http:// link nor
post the link directly to a file for downloading.
This posting is provided "as is" with no warranties
and confers no rights.
****************************************
***************************
"Randy" <anonymous@.discussions.microsoft.com> wrote in message
news:099201c3db71$adcdd100$a601280a@.phx.gbl...
quote:

> I have data in a smalldatetime column but the dates are
> mixed. Some in the '3/7/04 11:47:00 AM' form and some
> in '3/7/04' (This is when I open the table from EM).
> When I select from the Query Analyzer I get
> '2004-01-08 13:35:00'
> My question is How can I select only the date part of it
> (Not the time part) to compare with another column ?
> Thnaks for any help.......
|||SELECT CONVERT(SMALLDATETIME, CONVERT(CHAR(8), column, 112)) FROM table
You can wrap this in a function for slightly cleaner code and slightly more
overhead. Depending on the size of your table, of course. I have a
function called dbo.getDayFloor() that handles this for me.
Note: I always convert to the smaller of the two datetime datatypes if I'm
only dealing with day boundaries and not concerned about time.
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Randy" <anonymous@.discussions.microsoft.com> wrote in message
news:099201c3db71$adcdd100$a601280a@.phx.gbl...
quote:

> I have data in a smalldatetime column but the dates are
> mixed. Some in the '3/7/04 11:47:00 AM' form and some
> in '3/7/04' (This is when I open the table from EM).
> When I select from the Query Analyzer I get
> '2004-01-08 13:35:00'
> My question is How can I select only the date part of it
> (Not the time part) to compare with another column ?
> Thnaks for any help.......
sql

Date Part of the date time

I have data in a smalldatetime column but the dates are
mixed. Some in the '3/7/04 11:47:00 AM' form and some
in '3/7/04' (This is when I open the table from EM).
When I select from the Query Analyzer I get
'2004-01-08 13:35:00'
My question is How can I select only the date part of it
(Not the time part) to compare with another column ?
Thnaks for any help.......select convert (varchar, <date>,101) returns the date in mm/dd/yyyy format.
It comes out as a string. That can easily be compared against another date.
--
*******************************************************************
Andy S.
MCSE NT/2000, MCDBA SQL 7/2000
andymcdba1@.NOMORESPAM.yahoo.com
Please remove NOMORESPAM before replying.
Always keep your antivirus and Microsoft software
up to date with the latest definitions and product updates.
Be suspicious of every email attachment, I will never send
or post anything other than the text of a http:// link nor
post the link directly to a file for downloading.
This posting is provided "as is" with no warranties
and confers no rights.
*******************************************************************
"Randy" <anonymous@.discussions.microsoft.com> wrote in message
news:099201c3db71$adcdd100$a601280a@.phx.gbl...
> I have data in a smalldatetime column but the dates are
> mixed. Some in the '3/7/04 11:47:00 AM' form and some
> in '3/7/04' (This is when I open the table from EM).
> When I select from the Query Analyzer I get
> '2004-01-08 13:35:00'
> My question is How can I select only the date part of it
> (Not the time part) to compare with another column ?
> Thnaks for any help.......|||SELECT CONVERT(SMALLDATETIME, CONVERT(CHAR(8), column, 112)) FROM table
You can wrap this in a function for slightly cleaner code and slightly more
overhead. Depending on the size of your table, of course. I have a
function called dbo.getDayFloor() that handles this for me.
Note: I always convert to the smaller of the two datetime datatypes if I'm
only dealing with day boundaries and not concerned about time.
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Randy" <anonymous@.discussions.microsoft.com> wrote in message
news:099201c3db71$adcdd100$a601280a@.phx.gbl...
> I have data in a smalldatetime column but the dates are
> mixed. Some in the '3/7/04 11:47:00 AM' form and some
> in '3/7/04' (This is when I open the table from EM).
> When I select from the Query Analyzer I get
> '2004-01-08 13:35:00'
> My question is How can I select only the date part of it
> (Not the time part) to compare with another column ?
> Thnaks for any help.......

Monday, March 19, 2012

Date is not getting saved in propper manner

hi friends

In my form i am storing system's date in the database.

4 that i m using a variable of date like:

dim today as new date

today=now.date

but its storing the default date that is '1/1/1900'

how to overcome this problem?

Hello:

Can you show us some of your code using to perform so?

Regards

|||

Hey,

If you are storing the date for when the row was created, you could set a default value for the date field with the default value of getdate(); this will get the date from the system time. Otherwise, try DateTime.Today instead of using a date variable. From that, you can then use ToString() to convert it to a string, or whatever you want to do with it.

date insert problem

hi...

my form has a text box which displays system date.

i am inserting date into MS SQL Server from this date textbox.

but it displays me error..

String was not recognized as a valid DateTime.

Line 154: myCommand1.ExecuteNonQuery()

i have written code as

myCommand1.Parameters.Add(New SqlParameter("@.date", SqlDbType.DateTime, 8))

myCommand1.Parameters("@.date").Value = FormatDateTime(datetxt.Text, DateFormat.GeneralDate)

and also tried to change date format with many other ways.

how should i solve this problem?

i also want to take time form a user with the help of web form and want to store it in other field called 'timein' and 'timeout'....

FormatDateTime returns a string. Use DateTime.Parse instead to create a valid datetime, and assign the datetime object parsed. Alternatively, you can use Convert.ToDateTime, but you get an exception for invalid dates. Tie your textbox in to a CompareValidator to ensure valid dates from the browser. I recommend turning off client validation for date validation as it does not validate medium date (e.g. 22-Apr-2007) correctly|||

Heh, who the hell ever types 22-Apr-2007? Personally, I wouldn't consider it valid input to begin with.

Since you have already stated that your sqldbtype is a datetime, just shove the string into the parameter.

EX:

cmd.parameter("@.MyDateTime).value=MyDateString

That of course assumes that you have already validated it as a valid date/time string format. Just make sure that you validate it both client and server side. Too many people I know drop the validators down on the client, then forget to wrap their "save" in a If page.IsValid(), and it works... Until a hacker comes along and posts some invalid data to the server.

Oh, and I would drop off the size parameter on the sqlparameter. Not sure if 8 is even the correct size for a datetime, but it's ignored anyway since datetime is a fixed size.

|||

The reason I suggest the use of medium date format is this. I've workd for a number of world-wide companies over many years, often on large projects. Hundreds of thousands of pounds have been wasted by developers and databases engineers, simply because of a US/UK date confusion, or forgetting to set the date locale to UK, then, some months later, finding a database full of data that is invalid, then having to fix it, and having systems off-line.

Medium date also still works in the local language, if you set the locale of a web site (e.g. Norway).

Developers should also be listening to business, who increasing, and in my view, rightly, want medium format. The whole of the rest of the world does not live in the US, that is why our web sites should be locale aware

Software development is also a team effort. It isn't down to the developer alone to ensure a web-site isn't hacked. Architects, Managers and Testers are all there to make it happen (safely).

As always, these are my opinions and suggestions, soI don't expect everyone to like or agree with them.

Happy coding all...

|||

That's all well and good, and I agree that applications should be built with globalization in mind, but that really has nothing to do with medium dates. We don't use them in the US, and they aren't used anywhere else in the world except for geeky tech documents. If you want to use a truly international standard, then use the ISO format (YYYY-MM-DD). It's the same format in every culture.

As for having incorrect dates in your database, that's why you should always use the datetime datatype. The value is the value no matter what culture you are in, infact I normally store all date/times in a database based on UTC. That way no matter where you are in the world, I can tell you what time and date a specific event occurred localized in your specific date format, and give you the time relative to your timezone.

Now, it may be in your company, that they have decided that medium format is best, but I can say that is an oddity, and not a rule. There is a standard date format, it's the ISO format that was approved by the international standards organization, and when people need a format that they don't want translated to their native culture's format, that is the one that should be used. After all, 01-Apr-2006 isn't correct for any other place but the US. They don't have "April" let alone "Apr" in other countries.

|||

More business documents in the UK are using Medium and even Long Dates than previously. You don't see YYYY-MM-DD used in the UK as most people would find it geeky (whatever that means). In my software I generally let the user override their locale anyway, and set a date format for the web site based on ISO8601, ISO, UK/US, etc.

Medium and Long dates do correctly translate into the local language. E.g. I just tried today's (long format - since Med format displays the same result) date on a simple web page, which returned 12-febrero-2006 when I changed the culture from UK English to international Spanish (es-ES).

BTW., It wasn't my database or even my company that had the incorrect dates. I spent 15 years contracting around the UK, working for various major companies. What I observed (and sometimes got asked for advice on) were issues where UK formatted dates (parsed as text using dd/mm/yy) were stored in a database that was assuming US format since the default language had never been changed. Eventually the systems broke, which gave rise to many issues and fixing. What I advised is this. If they had used medium format from the start, the issue would not have arose, because the text would have been corretcly converted into the underlying datetime type.

I am sure we can disagree long into the night about date formats. However, it's a minor issue, and you are correct to raise the point that storing dates in the underlying format is the correct way to go.

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. 

Wednesday, March 7, 2012

Date Format

I have a written a stored procedure and excecuting it and displaying it in gridview(ASP.NET). I am getting Birthdate in the form of8/23/1956 12:00:00 AM. I just need8/23/1956. I have defined it as smalldatetime in stored procedure. How can I get it in that format ?

HELP.......

In your .net code, if the object is of type DateTime you can call ToShortDate.

|||

Either you can convert the datetime to a varchar in your stored procedure:

select convert(varchar(10), getdate(), 103)

... or you can format the output in the GridView:

<asp:BoundFieldDataField="now"DataFormatString="{0:dd/MM/yyyy}"HtmlEncode="false"/>

|||

Set the DataFormatString, either in designer or HTML:

<asp:BoundField DataField="Date" DataFormatString="{0:dd-MMM-yy}" HeaderText="Date" HtmlEncode="false" />
|||

You can read more about CONVERT here:http://msdn2.microsoft.com/en-us/library/aa226054(SQL.80).aspx

...and more about DataFormatString here:http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx

|||

Thanks a bunch ... millepag

That CONVERT thing worked for me!!!!

Sunday, February 19, 2012

Date Conversion

I'm searching on a smalldatetime field in SQL Server so a typical value would be 09/21/2005 11:30:00 AM. I have a search form which offers the user a textbox to search by date and unless they enter the exact date and time, no matching records are found. Of course I want I all records for a given day to be returned. This is how I'm doing it now. Thanks.

Dim dteDate_RequestedAsString = txtDate_Requested.Text

If dteDate_Requested <>""Then
strSqlText +=" Date_Requested='" & dteDate_Requested &"'"
EndIf

You have to change to DateTime to get the results you want because SmallDateTime have limited resolution. Try the link below for more info. Hope this helps.
http://www.stanford.edu/~bsuter/sql-datecomputations.html|||I've changed my SQL Server field type from SmallDateTime to DateTime and it's still not working. If I do a response.write on the SQL statement, I see that it's working correctly (WHERE Date_Requested='09/22/2005')|||You need to keep in mind that there is no Date data type. All ofthe data types involving dates also includes times. You need towind up with a query that looks like this:
WHERE Date_Requested >= '20050922' AND Date_Requested < '20050923'

That is my best recommendation. That will return you all recordswhere Date_Requested falls on 9/22/2005 regardless of the time part ofthe date you are storing.
I must strongly recommend to you that you use Parameters instead of concatenating UI-supplied data to a string to be executed.
Here's the why:
Please, please, please, learn about injection attacks!
How To: Protect From SQL Injection in ASP.NET

And here's the how:
Using Parameterized Query in ASP.NET, Part 1
Using Parameterized Query in ASP.NET, Part 2
|||I see. I've been a developer quite awhile and did not know this was the best way to search date fields.
This is for a small intranet app so I'm not concerned about SQL injection attacks in this case but that is very good advice.
Thanks for the help.
|||

evanburen wrote:

I see. I've been a developer quite awhile anddid not know this was the best way to search date fields.


It's just what I've learned through trial and error and seeing otherpeople struggling with it. The method I suggested will takeadvantage of any index on your date field, and it takes the time partof the date out of the equation.

evanburen wrote:

This isfor a small intranet app so I'm not concerned about SQL injectionattacks in this case but that is very good advice.


I have a few thoughts to offer on this viewpoint.
While one would like to think that all coworkers are trustworthy,a curious or disgruntled employee, or perhaps a temporary contractor,might try to access data to which they are not otherwise privileged, orperhaps even attempt to inflict damage to the database ornetwork. There still might be data which needs to be keptsafeguarded, such as payroll information, benefit information.