![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
The query needs to identify all sales between the last day of the previous month and going back one year. |
|
Hi, I have a requirement to design a query that identifies items sold between two dates. There is a 'SoldDate' datetime field used to register what date the item was sold. The query needs to identify all sales between the last day of the previous month and going back one year. What I would like to do is to design a query / stored procedure that will dynamically create the criteria to allow the client to simply run the query or stored proc. I know how to establish the last day of the previous month part, I'm just not sure of how best to design the remainder of the query. Thank in advance |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
The query needs to identify all sales between the last day of the previous month and going back one year. Below is one method. I generally recommend using >= and < instead of BETWEEN for datetime data types. This will better handle datetime values that include time. WHERE SoldDate >= CAST(CONVERT(CHAR(6), DATEADD(year, -1, DATEDIFF(day, 0, GETDATE())), 112) + '01' AS datetime) AND SoldDate < CAST(CONVERT(CHAR(6), DATEADD(day, 0, DATEDIFF(day, 0, GETDATE())), 112) + '01' AS datetime) -- Hope this helps. Dan Guzman SQL Server MVP brymcgu... (AT) googlemail (DOT) com> wrote in message news:1178789743.579297.70040 (AT) n59g2000hsh (DOT) googlegroups.com... Hi, I have a requirement to design a query that identifies items sold between two dates. There is a 'SoldDate' datetime field used to register what date the item was sold. The query needs to identify all sales between the last day of the previous month and going back one year. What I would like to do is to design a query / stored procedure that will dynamically create the criteria to allow the client to simply run the query or stored proc. I know how to establish the last day of the previous month part, I'm just not sure of how best to design the remainder of the query. Thank in advance- Hide quoted text - - Show quoted text - |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
I know how to establish the last day of the previous month part, I'm just not sure of how best to design the remainder of the query. |
#7
| |||
| |||
|
|
I know how to establish the last day of the previous month part, I'm just not sure of how best to design the remainder of the query. Instead of using procedural coding, why not use a table of the reporting periods for a decade or two? A simple BETWEEN predicate will classify each sale quickly and give you extra control over non- operating days, etc. |

#8
| |||
| |||
|
|
I know how to establish the last day of the previous month part, I'm just not sure of how best to design the remainder of the query. Instead of using procedural coding, why not use a table of the reporting periods for a decade or two? A simple BETWEEN predicate will classify each sale quickly and give you extra control over non- operating days, etc. |
![]() |
| Thread Tools | |
| Display Modes | |
| |