How to retrieve rows from database by passing month in where condition - sql

How to retrieve rows from a SQL Server database by passing month in where condition, as I have used datetime datatype for the date column.
Is there any function is SQL Server, where I can send month and will get the row accordingly?

I would create an indexed computed column based on the month in the date column. This way queries will scale as you'll have a specific column indexed for querying against.
Using Month(yourdatecolumn) = somemonth is going to table scan (as in read every value to determine the month) at query execution time so wouldn't be a good idea for large volumes of data.
See this article for a commentary on why using functions in your where clause is bad.

SELECT * FROM table
WHERE MONTH(datecolumn) = 1
this will return all records for January

Related

SQL Server, Efficient way to query multiple tables for a specific day of data

We have started saving the daily change data for a bunch of tables. We first put in all of the original data and than the change data with start and end dates for each table record (So not all records are saved each day just changed records). So to pull the data for a specific date I have to look at the beginning and end dates (end date might be null) and pull the MAX begin date to get the right records.
SELECT *
FROM dbo.DIM_EX_NAME_MASTER AS DEXNM
INNER JOIN
(SELECT APPID, MAX(DW_RECORD_START) AS StartDate
FROM dbo.DIM_EX_NAME_MASTER
WHERE (DW_RECORD_START < '4/4/2020') AND (DW_RECORD_END > '4/4/2020')
OR (DW_RECORD_START < '4/4/2020') AND (DW_RECORD_END IS NULL)
GROUP BY APPID) AS INNER_DEXNM ON DEXNM.APPID = INNER_DEXNM.APPID AND DEXNM.DW_RECORD_START = INNER_DEXNM.StartDate
So that's not so bad for one table but we want to build a report with a query that pulls from 25 tables with subqueries where the user selects the date to pull for.
That's going to be some really messy sql. Ideally I would like to create a view for each table and pass in the date as a parameter but SQL server doesn't allow for parameterized views.
Anyone have any ideas on how I can build multi-table date based queries without adding all of this extra sql per table?
Thanks for any help you can give!
SQL server doesn't allow for parameterized views.
No, but SQL Server does support user-defined table-valued functions.
This is pretty much exactly what you are asking for -- they can accept a date parameter and return the results as a table.

How does work AS OF clause in system-versioned temporal tables?

You need to query the customer table to return the state of the table before these changes were made - which query should you use?
Correct answer is: this query uses as of sub-clause to return the state of the customer table before changes were made.
SELECT *
FROM dbo.customer
FOR SYSTEM_TIME AS OF '2017-10-01 00:00:00'
ORDER BY customerID;
Question: How does, in this case, AS OF clause work? How this can query history table for '2017-10-01 00:00:00'?
You are working with system-versioned temporal tables. First introduced at ANSI SQL 2011 and then at SQL SERVER 2016.
This tables have two control columns, sysStartDate and sysEndDate, which specifies the period for which a record is valid (when a record is still valid end-date is set to 9999-12-31).
This clause, FOR SYSTEM_TIME AS OF <date_time> will query data that was valid for a specified date time. That means, it will filter out data with sysStartDate bigger than <date_time> and sysEndDate smaller than <date_time>, i.e., you are querying data that was valid at <date_time>.

Generate month and year from date and store automatically in table

Im working on sql server 2005, i have a column 'Date' of datatype datetime and want to extract month and year separately and store it in two separate columns in the same table as 'Date'. How do i do this and make it happen automatically everytime a new entry is entered where i dont have to manually input month and date.
You can use computed columns or trigger.
I recommend to use computed columns. It's very easy to implement and maintenance.
To get year and month use YEAR and MONTH functions
Computed Columns in SQL Server
DateTime functions in SQL Server

SQL Server 2005 simple query to get all dates between two dates

I know there are a lot of solutions to this but I am looking for a simple query to get all the dates between two dates.
I cannot declare variables.
As per the comment above, it's just guesswork without your table structures and further detail. Also, are you using a 3NF database or star schema structures, etc. Is this a transaction system or a data warehouse?
As a general answer, I would recommend creating a Calendar table, that way you can create multiple columns for Working Day, Weekend Day, Business Day, etc. and add a date key value, starting at 1 and incrementing each day.
Your query then is a very simple sub-select or join to the table to do something like
SELECT date FROM Calendar WHERE date BETWEEN <x> AND <y>
How to create a Calender table for 100 years in Sql
There are other options like creating the calendar table using iterations (eg, as a CTE table) and linking to that.
SQL - Create a temp table or CTE of first day of the month and month names

Sql Server 2008 partition table based on insert date

My question is about table partitioning in SQL Server 2008.
I have a program that loads data into a table every 10 mins or so. Approx 40 million rows per day.
The data is bcp'ed into the table and needs to be able to be loaded very quickly.
I would like to partition this table based on the date the data is inserted into the table. Each partition would contain the data loaded in one particular day.
The table should hold the last 50 days of data, so every night I need to drop any partitions older than 50 days.
I would like to have a process that aggregates data loaded into the current partition every hour into some aggregation tables. The summary will only ever run on the latest partition (since all other partitions will already be summarised) so it is important it is partitioned on insert_date.
Generally when querying the data, the insert date is specified (or multiple insert dates). The detailed data is queried by drilling down from the summarised data and as this is summarised based on insert date, the insert date is always specified when querying the detailed data in the partitioned table.
Can I create a default column in the table "Insert_date" that gets a value of Getdate() and then partition on this somehow?
OR
I can create a column in the table "insert_date" and put a hard coded value of today's date.
What would the partition function look like?
Would seperate tables and a partitioned view be better suited?
I have tried both, and even though I think partition tables are cooler. But after trying to teach how to maintain the code afterwards it just wasten't justified. In that scenario we used a hard coded field date field that was in the insert statement.
Now I use different tables ( 31 days / 31 tables ) + aggrigation table and there is an ugly union all query that joins togeather the monthly data.
Advantage. Super timple sql, and simple c# code for bcp and nobody has complained about complexity.
But if you have the infrastructure and a gaggle of .net / sql gurus I would choose the partitioning strategy.