Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
let say one table contain only
17-04
18-06
20-12

Another table or just i have
2012-2014

means
it conitas
2012
2013
2014

I want output like
17-04-2012
18-06-2012
20-12-2012
17-04-2013
18-06-2013
20-12-2013
17-04-2014
18-06-2014
20-12-2014


How to get like this
Posted
Updated 19-Feb-13 21:22pm
v2

Hi,

there are a number of ways to do this, depending on what you really want. With no common columns, you need to decide whether you want to introduce a common column or get the product.

A cartesian product will match every row in the first table with every row in the second.

That's probably not what you want since 1000 entries in the table 1 and 100 entries in the table 2 would result in 100,000 rows with lots of duplicated information.

Alternatively, you can use a union to just output the data, though not side-by-side (you'll need to make sure column types are compatible between the two selects, either by making the table columns compatible or coercing them in the select).

If you can be more specific with your question we probably can came up with a valid solution. So, specify if there is a relation between two tables and the number of entries in each one, then we can make a SQL statement that will retrieve the desired result.


Cheers
 
Share this answer
 
this way...
SQL
with FIND_YRS(yr) as
(
    select (select convert(int,left('2012-2014',4))) as yr
    union all
    select yr + 1  from FIND_YRS where yr < (select convert(int,right('2012-2014',4)))
)

select day_Month_Col + '-' + convert(varchar(4),yr) as date 
from DAY_MONTH_TABLE
cross join FIND_YRS

Happy Coding!
:)
 
Share this answer
 
Hi,

Check the following Code
SQL
-- Table Creations
CREATE TABLE #Month (MonthVal VARCHAR(20))
INSERT INTO #Month(MonthVal) VALUES('17-04'),('18-06'),('20-12')
CREATE TABLE #Year(YearRange SMALLINT)
INSERT INTO #Year(YearRange) VALUES(2012),(2013),(2014)
-- Actual Query 
SELECT MonthVal +'-'+CAST(YearRange AS VARCHAR(5))
FROM #Month,#Year 



Regards,
GVPrabu
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900