Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Editing this statement to pick certain dates

Author  Topic 

Sudor
Starting Member

3 Posts

Posted - 2014-02-03 : 06:08:58
Hi

I am very new to SQL overall and have had the fortune to not be forced to tamper with the existing code.

Unfortunatly, one of my exports did not run for 3 days and te query fetches todays date. Could anyone help me with chaning below line to export data for a Specific data instead of current date?

WHERE (dbo.fr.ForetagKod = 400) AND (YEAR(dbo.ft.FaktDat) = YEAR(GETDATE())) AND ({ fn MONTH(dbo.ft.FaktDat) } = { fn MONTH(GETDATE()) })

Thank you!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-03 : 06:40:24
declare @date as a parameter and use below logic

...
WHERE (dbo.fr.ForetagKod = 400) AND dbo.ft.FaktDat >= @Date AND dbo.ft.FaktDat < @Date + 1

then pass required date value to it to get data for that date

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Sudor
Starting Member

3 Posts

Posted - 2014-02-03 : 06:56:39
Hi
Thanks for the reply.

But how would I declare the date as a parameter? =)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-03 : 07:00:41
quote:
Originally posted by Sudor

Hi
Thanks for the reply.

But how would I declare the date as a parameter? =)



if its an adhoc query use DECLARE

ie like


DECLARE @Date datetime
SET @Date = <pass your value here>

.... your query
WHERE (dbo.fr.ForetagKod = 400) AND dbo.ft.FaktDat >= @Date AND dbo.ft.FaktDat < @Date + 1




if its a stored procedure use like below

CREATE PROC GetData
@date datetime
AS

..... your query
WHERE (dbo.fr.ForetagKod = 400) AND dbo.ft.FaktDat >= @Date AND dbo.ft.FaktDat < @Date + 1
GO


execute it like

EXEC GetData <pass your value here>




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Sudor
Starting Member

3 Posts

Posted - 2014-02-03 : 07:09:49
Big thank you! Got it to work. it was a so called "adhoc" query.

/Sudor
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-03 : 13:41:09
ok..cool

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -