MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old June 9th, 2004, 09:59 PM
awbrab awbrab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Singapore
Posts: 28 awbrab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Filter Date

I got a field called Date#, datatype = datetime, default value = getdate().

So after i insert a record, the Date# will auto record the system date and the time. And my MS SQL Server show: 6/9/2004 12:24:35 PM

However i am not able to filter my record by date. The statement i tried are:

SELECT * from mytable WHERE Date# = '6/9/2004 12:24:35 PM'

SELECT * from mytable WHERE Date# = "6/9/2004 12:24:35 PM"

SELECT * from mytable WHERE Date# = 6/9/2004 12:24:35 PM

SELECT * from mytable WHERE Date# = '6/9/2004'

Pls help me,
Thanks a million

Reply With Quote
  #2  
Old June 9th, 2004, 10:37 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
try it with square brackets around Date#

it's not a good idea to use special characters in a column name
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old June 9th, 2004, 10:48 PM
awbrab awbrab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Singapore
Posts: 28 awbrab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I tried using Date and [Date] as well
both does not work.

Anyway is it something to do with the datatype?
And how do i filter? Do i filter by keying '6/9/2004' or '6/9/2004 12:24:35 PM'?

Reply With Quote
  #4  
Old June 10th, 2004, 06:26 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
do you know how to generate a script for this table in Enterprise Manager?

i'd like to see the CREATE TABLE statement and a few sample rows

Reply With Quote
  #5  
Old June 10th, 2004, 10:42 PM
awbrab awbrab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Singapore
Posts: 28 awbrab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SHI]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[SHI]
GO

CREATE TABLE [dbo].[SHI] (
[Value1] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Value2] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Date#] [datetime] NULL
) ON [PRIMARY]
GO

Reply With Quote
  #6  
Old June 10th, 2004, 11:37 PM
brightlight brightlight is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 371 brightlight User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 32 m 27 sec
Reputation Power: 5
May be date format is different. have u tried with

SELECT * from mytable WHERE Date# = '9/6/2004 12:24:35 PM'
__________________
Regards,
Brightlight

Reply With Quote
  #7  
Old June 11th, 2004, 03:03 AM
awbrab awbrab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Singapore
Posts: 28 awbrab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I finally got the format needed using MS SQL Server Query Analyser.
I actually need to key in '2004-06-09 12:24:35.007'

But is there a way to only filter by the date only? That mean i just wanted to key in the date not the time in order to filter.

Actually I need a statement in order to retrieve the record where only the record for past 7 days from the current date at the of point is retrieve.

So right now i got 2 problem:
First, not able to filter by just keying date.
Second, need to statement to capture the current date and minus 7 days and retrieve the past 7 days record.

Pls help me as i have read up a few books but still no answer.
Thanks a lot

Reply With Quote
  #8  
Old June 11th, 2004, 03:56 AM
brightlight brightlight is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 371 brightlight User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 32 m 27 sec
Reputation Power: 5
1. Change the date according to ur key in date. append 23:59:59, which is the last second of that date. This will retrieve the records with the keyed in date no matter what time it has got. I think its better to format the date dd-mmm-yy to avoid the month and day problem.

Eg:

SELECT *
FROM SHI
WHERE (Date# BETWEEN '10-feb-02' AND '10-feb-02 23:59:59')


2. To get the last 7 days records

SELECT *
FROM SHI
WHERE (Date# BETWEEN DATEADD([day], - 7, GETDATE()) AND GETDATE())

Reply With Quote
  #9  
Old June 12th, 2004, 01:26 AM
awbrab awbrab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Singapore
Posts: 28 awbrab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks to all experts, problem is solved.

Reply With Quote
  #10  
Old June 26th, 2004, 02:35 AM
awbrab awbrab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Singapore
Posts: 28 awbrab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Problem again

Hi experts, i am problem regarding date filtering again. Pls help me.

select value1
from table1
where (date# between dateadd([day], -7, getdate()) and getdate())

The statement is correct but it takes the time of getdate() into consideration.
If i execute this statement at 26/6/04 1500hrs, the records show the record from 19/4 - 26/4 but records before 19/4 1500hrs it will not show. But i need the whole records on 19/4.

So how can i ignore the time while filtering?

Reply With Quote
  #11  
Old June 26th, 2004, 06:07 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
where date#
between cast(convert(char(10),dateadd([day], -7, getdate()),120) as date)
and getdate()

do the same to the upper end if necessary

Reply With Quote
  #12  
Old June 26th, 2004, 11:59 PM
awbrab awbrab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Singapore
Posts: 28 awbrab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I got an error:
Type date is not a defined system type

where date#
between cast(convert(char(10),dateadd([day],-7, getdate()),120) as date) and getdate()

I have check try changing the name date after the AS to other name and i will also say : Type othername is not a defined system type

Reply With Quote
  #13  
Old June 27th, 2004, 12:03 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
oops, my bad

where date#
between cast(convert(char(10),dateadd([day],-7, getdate()),120) as datetime) and getdate()

Reply With Quote
  #14  
Old June 27th, 2004, 12:42 AM
awbrab awbrab is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Singapore
Posts: 28 awbrab User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks a lot, i just figure out also.
Really thanks a lot

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Filter Date


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump