Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 December 7th, 2005, 11:24 PM
Registered User
 
Join Date: Dec 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default DateTime

I'm new to SQL Server 2000 and have a question about DateTime. I'm more comfortable with Access and how it handles dates. I use a # for dates there. Also I never had the actual time in the field either. Now I have a table in SQL Server with date and time. I try to run a query with just date but it doesn't work. How can I convert this field to just date, or what function can I use in my query to just look at the date and ignore the time? Thanks.

 
Old December 8th, 2005, 12:45 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

All date type fields in SQL have the time in them. You can simply not save the time, or convert the datetime to a char to remove the time from being displayed. Most other databases have a date only field type of some sort but MS-SQL does not.

 
Old December 8th, 2005, 12:51 PM
Authorized User
 
Join Date: Sep 2005
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Play around with this

declare @date datetime
select @date = convert(varchar,getdate(),110) --or select @date = '20051208'
select @date,dateadd(d,1,@date) -- just so that you can see what the date looks like

select * from table
where datefield >= @date
and datefield < dateadd(d,1,@date)



“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" -- http://sqlservercode.blogspot.com/
 
Old December 9th, 2005, 04:45 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default

Hi,

SELECT CONVERT(varchar(10), field1, 103) FROM Table1
SELECT CONVERT(varchar(10), getDate(), 103) FROM Table1

change the style(103) as you want.

If you have any datetime parameters, convert them also
SELECT * FROM table1 WHERE CONVERT(varchar(10), field1, 103) = CONVERT(varchar(10), @Parameter1, 103)

 
Old December 9th, 2005, 06:45 AM
Authorized User
 
Join Date: Sep 2005
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nalla, If you do a convert on a datetime field you will get a table scan instead of an index seek/scan and that will degrade performance

“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" -- http://sqlservercode.blogspot.com/
 
Old December 13th, 2005, 11:31 PM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default

SqlMenace,
Suppose my table name is Table 1 and the field name is Field1 and the values in my field is stored in this format,
2005-03-25 10:17:36.390
I want to select all the records from Table1 where Field1 = '25/03/2005'

so what I'm doing is,

@parameter1 = '25/03/2005'

SELECT * FROM Table1 WHERE CONVERT(varchar(10), Field1, 103) = CONVERT(varchar(10), @Parameter1, 103)

Please tell me how to do this without the CONVERT function or any methods better than this.

 
Old December 14th, 2005, 06:51 AM
Authorized User
 
Join Date: Sep 2005
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nalla, you can do it like this


declare @parameter1 datetime
select @parameter1= '20050325'
select @parameter1,dateadd(d,1,@parameter1) -- just so that you can see what the date looks like

select * from table
where Field1 >= @parameter1
and Field1 < dateadd(d,1,@parameter1)


“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" -- http://sqlservercode.blogspot.com/
 
Old December 14th, 2005, 07:08 PM
Authorized User
 
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi RickP,

Here's a link that discusses different ways of getting just the date part from a datetime value:

http://www.sql-server-helper.com/fun...date-only.aspx

Hope this helps.

SQL Server Helper - FAQ - About Dates
http://www.sql-server-helper.com/faq/dates-p01.aspx





Similar Threads
Thread Thread Starter Forum Replies Last Post
datetime scandalous SQL Server 2005 3 December 19th, 2007 10:33 AM
datetime MathLearner Beginning VB 6 0 November 15th, 2007 07:12 AM
DateTime pzmrcd C# 3 July 26th, 2007 06:35 AM
UTC DateTime to Local DateTime r_ganesh76 SQL Server 2000 1 April 4th, 2005 08:21 AM
DateTime DARSIN General .NET 1 December 1st, 2004 06:24 AM





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