|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 4:08 PM
Points: 17,
Visits: 142
|
|
Hi All,
I need to create a process that looks for a table in database with date(YYYYMM) appeneded to it where MM can be any month of current quarter and check if there exists a table and dump that data into a table. I was able to get to the point of getting the last day and first day of current quarter to check for the range. But the problem is iam not sure how to search for a table with date appended for this specific range. Please help me out.
Thanks in advance
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Sunday, March 31, 2013 10:52 AM
Points: 263,
Visits: 69
|
|
Hi Prasad,
In order to search for the table we can use sys.tables as below, also inorder to convert date to month you can use datepart function. Hope this helps.
select * from sys.tables where name like '%YYYYMM%'
Thanks.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 10:39 AM
Points: 2,556,
Visits: 4,398
|
|
Will your table suffix represent just year and month only YYYYMM or day as well YYYYMMDD? What criteria do you want to apply? Month number and year? Date range? Anything else?
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 4:08 PM
Points: 17,
Visits: 142
|
|
Eugene Elutin (9/5/2012) Will your table suffix represent just year and month only YYYYMM or day as well YYYYMMDD? What criteria do you want to apply? Month number and year? Date range? Anything else?
It just uses YYYYMM not date....no other criteria...i just have to check with this date falls under current quarter...the only problem is it is aappended to a table/tables and have to pull all thosee tables out.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 10:39 AM
Points: 2,556,
Visits: 4,398
|
|
That will return all tables with suffix YYYYMM where YYYYMM represent every month of the current calendar quarter.
SELECT t.name FROM sys.tables t JOIN (SELECT '%' + CAST(QFM + m AS VARCHAR) AS QM FROM ( SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) + RIGHT('0' + CAST(DATEPART(QUARTER,GETDATE()) * 3 AS VARCHAR(2)),2) AS QFM ) q CROSS JOIN (SELECT 0 m UNION ALL SELECT 1 m UNION ALL SELECT 2 m) m ) qm3 ON t.name LIKE qm3.QM
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 32,923,
Visits: 26,811
|
|
Another way to get the table names...
SELECT st.Name FROM sys.tables st WHERE RIGHT(st.Name,6) IN ( SELECT CONVERT(CHAR(6), DATEADD(mm,t.N,DATEADD(qq,DATEDIFF(qq,0,GETDATE()),0)), 112) FROM (SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2) t (N) ) ;
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
>> I need to create a process that looks for a table in database with date(YYYYMM) appended to it where MM can be any month of current quarter and check if there exists a table and dump that data into a table. <<
This makes no sense. Rows are appended to a tables, not dates. Why are you PHYSICALLY moving data from table to table? SQL programmers use VIEWs.
>> I was able to get to the point of getting the last day and first day of current quarter to check for the range. <<
A useful idiom is a report period calendar. It gives a name to a range of dates.
CREATE TABLE Report_Periods (report_name CHAR(10) NOT NULL PRIMARY KEY, report_start_date DATE NOT NULL, report_end_date DATE NOT NULL, CONSTRAINT date_ordering CHECK (report_start_date <= report_end_date), etc);
These report periods can overlap; a fiscal quarter will be contained in the range of its fiscal year. There can be gaps between them; we have to wait a year between each “Annual Going out Of Business Sale!” and there might be long stretches of time without any special sales. But we want to know their ranges so that the table is fairly constant once it is created.
I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantage is that it will sort with the ISO-8601 data format required by Standard SQL.
Since SQL is a database language, we prefer to do look ups and not calculations.
Books in Celko Series for Morgan-Kaufmann Publishing Analytics and OLAP in SQL Data and Databases: Concepts in Practice Data, Measurements and Standards in SQL SQL for Smarties SQL Programming Style SQL Puzzles and Answers Thinking in Sets Trees and Hierarchies in SQL
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 32,923,
Visits: 26,811
|
|
CELKO (9/9/2012) >> I need to create a process that looks for a table in database with date(YYYYMM) appended to it where MM can be any month of current quarter and check if there exists a table and dump that data into a table. <<
This makes no sense. Rows are appended to a tables, not dates. Why are you PHYSICALLY moving data from table to table? SQL programmers use VIEWs.
He's not talking about appending rows to a table, Joe. He's talking about finding tables that have table names that are essentially identical with the only difference being that the names of the tables have had a year and month notation appended to them. This type of thing happens a lot when receiving data from 3rd party sources. If you don't have the Enterprise Edition of SQL Server, the tables are typically allowed to persist as they are named as a form of "poor man's partitioning" and are frequently assembled as a complete "entity" by using partitioned views.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|