Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 15th, 2007, 10:52 AM
Friend of Wrox
 
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
Default Convert from nvarchar(25) to smalldatetime

Chaps, really hope you can point me in the right direction

I have a table field called sqldate which currently has a data type of nvarchar and length of 25 which has date entry’s in dd/mm/yy.

So that i can successfully query the field i need to convert it to smalldatetime.

Since creating this field i now can successfully input date formats into smalldatetime which before now eluded me.


Any help would be appreciated.


Thanks



Aspless


 
Old August 15th, 2007, 10:58 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmmm not sure if this is what you mean but something like this should work for you:

SELECT * from Table where Convert(smalldatetime,field) = @smalldatetimeparameter

But please explain to me this line:
Since creating this field i now can successfully input date formats into smalldatetime which before now eluded me.

and also why you are not just using a smalldatetime column.

hth,

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET

Professional IIS 7 and ASP.NET Integrated Programming

================================================== =========
 
Old August 15th, 2007, 04:10 PM
Friend of Wrox
 
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
Default

Thanks for the reply.

After reading the line
 
Quote:
quote:Since creating this field i now can successfully input date formats into smalldatetime which before now eluded me


i can see why you would be confused .. apologies, too many hours by the screen.

So that i can successfully query the sqldate field using the following code

Code:
SELECT * FROM atable where sqldate > DateDiff(Day, 14, GetDate())
i need to change using query analyzer the sqldate field from nvarchar to smalldatetime.

As there are already records in the format of dd/mm/yy if i try to change the format in sql design i get the error.

Code:
Arithmetic overflow error converting expression to data type smalldatetime
I am hoping there is a script which will allow me to convert the field with the entries in place.

I hope this makes more sense.

Cheers


aspless


 
Old August 16th, 2007, 07:39 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Instead of trying to change the columns data type you could create a new column in the table with a smalldatetime type and just execute this query:

UPDATE Table set newDateTimeColumn = Convert(smalldatetime, varcharDateField)

Someone else may have a better solution but I do not.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET

Professional IIS 7 and ASP.NET Integrated Programming

================================================== =========
 
Old August 16th, 2007, 02:16 PM
Friend of Wrox
 
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
Default


Thanks for the reply.

I will try your method.

Cheers



aspless

 
Old September 14th, 2007, 09:05 AM
Friend of Wrox
 
Join Date: Dec 2006
Posts: 104
Thanks: 9
Thanked 1 Time in 1 Post
Default

Just a quick update on the solution

Your suggestion did work with the addition of the date type specifier

UPDATE atable set newcolumn = Convert(smalldatetime, original_column, 103)

Cheers

 
Old September 14th, 2007, 09:21 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Glad it worked out for you. ^^

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET

Professional IIS 7 and ASP.NET Integrated Programming

================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
convert data from nvarchar to Date Time rahulgzb SQL Server 2000 4 September 7th, 2007 09:45 PM
convert nvarchar to money smatyk SQL Server 2000 2 October 26th, 2006 06:37 PM
Convert Char Data Type to smalldatetime rylemer SQL Language 3 March 3rd, 2005 03:13 PM
Convert int to nvarchar bekim SQL Language 1 August 12th, 2004 06:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.