SQL Date

Get help on programming - C++, Java, Delphi, etc.
Post Reply
SLIP
Registered User
Posts: 12148
Joined: 07 Sep 2005, 02:00
Processor: Intel Sandy bridge i7-2600K
Motherboard: ASUS P8Z68 DELUXE GEN3
Graphics card: GeForce GTX560TOP OC
Memory: Corsair Vengeance 8GB
Location: Randburg
Contact:

SQL Date

Post by SLIP »

Hi,

What I need to do is...

I am building a report in Reporting Services.
Now if the report gets generated today(06/2008) the report must be dated for the (05/2008).

Now I came up with...:

Code: Select all

select (month((GetDate()))-1)
That returns the value 5.

but I need to select the data for the report between the first date and the last date of the previous month.

like so..:

Code: Select all

Select*
From <table>
where recorddate between '20080501' and '20080531'

That will give me all the data I need but the dates needs to be added manually instead of it calculating by itself what the first and last date is of the previous month...

Please Help? 8)
Image
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

Post by SilverBack »

Code: Select all

DATEADD (datepart , number, date )
Wont this do the trick?
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
SLIP
Registered User
Posts: 12148
Joined: 07 Sep 2005, 02:00
Processor: Intel Sandy bridge i7-2600K
Motherboard: ASUS P8Z68 DELUXE GEN3
Graphics card: GeForce GTX560TOP OC
Memory: Corsair Vengeance 8GB
Location: Randburg
Contact:

Post by SLIP »

[quote="SilverBack"]

Code: Select all

DATEADD (datepart , number, date )
Im still a noob with this.....so can you tell me what values to put where?
Image
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

Post by SilverBack »

Oh, ok. Sorry. Erm...hmmm, lets see...

EDIT: Better solution below ;)
Last edited by SilverBack on 11 Jun 2008, 16:11, edited 1 time in total.
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

Post by SilverBack »

Sorry, forgot to add. You will run into a problem eventually cos, say you have 2 years worth of records right. If you run it for May, it will pull up records for May for the current year, and May for previous year. You will have to check on the dates year value too.

This is more effective: :wink:

Code: Select all

SELECT * FROM <table>
WHERE MONTH(recorddate) = MONTH(DATEADD(m, -1, getdate()))
AND YEAR(recorddate) = YEAR(getdate())
Last edited by SilverBack on 11 Jun 2008, 16:10, edited 1 time in total.
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
GrimStoner
Registered User
Posts: 716
Joined: 08 Oct 2004, 02:00
Contact:

Post by GrimStoner »

From Google...

Code: Select all

CREATE FUNCTION [dbo].[ufn_GetFirstDayOfMonth] ( @pInputDate    DATETIME )
RETURNS DATETIME
BEGIN

    RETURN CAST(CAST(YEAR(@pInputDate) AS VARCHAR(4)) + '/' + 
                CAST(MONTH(@pInputDate) AS VARCHAR(2)) + '/01' AS DATETIME)

END
GO
and ...

Code: Select all

CREATE FUNCTION [dbo].[ufn_GetLastDayOfMonth] ( @pInputDate    DATETIME )
RETURNS DATETIME
BEGIN

    DECLARE @vOutputDate        DATETIME

    SET @vOutputDate = CAST(YEAR(@pInputDate) AS VARCHAR(4)) + '/' + 
                       CAST(MONTH(@pInputDate) AS VARCHAR(2)) + '/01'
    SET @vOutputDate = DATEADD(DD, -1, DATEADD(M, 1, @vOutputDate))

    RETURN @vOutputDate

END
GO
Then ...

Code: Select all

SELECT fishpaste 
FROM yackityschmackity 
WHERE YourDateField BETWEEN [dbo].[ufn_GetFirstDayOfMonth] (DATEADD(M, -1, InputDate)) AND [dbo].[ufn_GetLastDayOfMonth] (DATEADD (M, -1, InputDate))
This is a long way round though. Think you should rather pass the date parameters in with the query, i.e. like in VB

Code: Select all

SELECT fishpaste 
FROM yackityschmackity 
WHERE YourDateField BETWEEN @DateFrom AND @DateTo
and then do the date calculations in VB.
GrimStoner
Registered User
Posts: 716
Joined: 08 Oct 2004, 02:00
Contact:

Post by GrimStoner »

Silverback's is easier tho... Just do a check on the year as well.

Edit : Rofl... lots easier...
Last edited by GrimStoner on 11 Jun 2008, 16:13, edited 1 time in total.
SLIP
Registered User
Posts: 12148
Joined: 07 Sep 2005, 02:00
Processor: Intel Sandy bridge i7-2600K
Motherboard: ASUS P8Z68 DELUXE GEN3
Graphics card: GeForce GTX560TOP OC
Memory: Corsair Vengeance 8GB
Location: Randburg
Contact:

Post by SLIP »

Thanks works 100%


:D :D :D :D :D
Image
SLIP
Registered User
Posts: 12148
Joined: 07 Sep 2005, 02:00
Processor: Intel Sandy bridge i7-2600K
Motherboard: ASUS P8Z68 DELUXE GEN3
Graphics card: GeForce GTX560TOP OC
Memory: Corsair Vengeance 8GB
Location: Randburg
Contact:

Post by SLIP »

Thanks Silver... :wink:
Image
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

Post by SilverBack »

Sweet ;)

Looks like Grims will work too...also a nice way to do it. Will help with getting to know how things function in SQL etc ;)

Did you see the YEAR option added in? I updated my first answer...and added the second better way
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
Stevo
Registered User
Posts: 1277
Joined: 27 Jul 2004, 02:00

Post by Stevo »

Not to put a damper on your resolve but just remember that when using the "BETWEEN" function it only queries data "between" the two dates and not including.

If memory serves me right it includes the first date but not the last date.

You may want to double check that the results you are presented with are accurate.
Image
Hex_Rated
Registered User
Posts: 3679
Joined: 19 Jan 2006, 02:00
Contact:

Post by Hex_Rated »

Random tip: Converting to UTC time (seconds past midnight 1970) often helps when your messing around with dates.

I hate working with dates and times in databases.
DFI LanParty X48 LT-2TR
Intel Q9450 @ 3.2Ghz
Dell 24" 2408WFP | Phillips 37" 1080p
Sapphire HD4870 X2 2GB
4GB Corsair DDR-2 1066 | Thermalrite 120 Ultra Extreme | G9 Mouse | G15 Keyboard
Vista Ultimate x64
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

Post by SilverBack »

Hex_Rated wrote:Random tip: Converting to UTC time (seconds past midnight 1970) often helps when your messing around with dates.

I hate working with dates and times in databases.
Lets not even go there!! I have had numerous headaches with dates....hours spent on it....hate workign with dates too :x :? :P :lol:
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
GrimStoner
Registered User
Posts: 716
Joined: 08 Oct 2004, 02:00
Contact:

Post by GrimStoner »

MySQL sucks with dates.
Post Reply