473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Find Columns

I want to do a search on the column, if the month is Jan in the specified date, then it will return the data in the Jan column, when the specified month is Feb, then it will return the data in the Feb column and so forth for the 12 months of the year. How to code the TSQL for this problem? I wonder if it would do with ARRAY of columns.
Dec 31 '07 #1
4 1355
deepuv04
227 Expert 100+
I want to do a search on the column, if the month is Jan in the specified date, then it will return the data in the Jan column, when the specified month is Feb, then it will return the data in the Feb column and so forth for the 12 months of the year. How to code the TSQL for this problem? I wonder if it would do with ARRAY of columns.

Hope this query will help you

the following query will return you the desired result if you are storing the data in dateformat with which you are comparing the month

select * from Table_Name
where month(Column_Name) = month( Parameter/SpecifiedDate )


if you want to include the year also add one more condition


select * from Table_Name
where month(Column_Name) = month( Parameter/SpecifiedDate ) and
year(Column_Name) = year( Parameter/SpecifiedDate )


thanks
Dec 31 '07 #2
Hope this query will help you

the following query will return you the desired result if you are storing the data in dateformat with which you are comparing the month

select * from Table_Name
where month(Column_Name) = month( Parameter/SpecifiedDate )


if you want to include the year also add one more condition


select * from Table_Name
where month(Column_Name) = month( Parameter/SpecifiedDate ) and
year(Column_Name) = year( Parameter/SpecifiedDate )


thanks


Thanks a lot deepuv, however, there is still some issues on my database, since one of the table is like this:

TableName: Animal
ID Name
A Cat
B Dog

TableName: Sales
ID Jan Feb Mar Apr May Jun ....
A 1 2 1 4 4 3 ....
B 2 4 3 2 3 1 ....


In this case, if I specify Jan, the output will be like:

ID Name Sales(Jan)
A Cat 1
B Dog 2


When I specify Feb, the output will be something like:

ID Name Sales(Feb)
A Cat 2
B Dog 4

How can I achieve this result?
Dec 31 '07 #3
deepuv04
227 Expert 100+
Thanks a lot deepuv, however, there is still some issues on my database, since one of the table is like this:

TableName: Animal
ID Name
A Cat
B Dog

TableName: Sales
ID Jan Feb Mar Apr May Jun ....
A 1 2 1 4 4 3 ....
B 2 4 3 2 3 1 ....


In this case, if I specify Jan, the output will be like:

ID Name Sales(Jan)
A Cat 1
B Dog 2


When I specify Feb, the output will be something like:

ID Name Sales(Feb)
A Cat 2
B Dog 4

How can I achieve this result?

hI,


use the following query

since you are getting the coulumns dynamically, you need to build the query as string and then execute the query to get the result...


DECLARE @String varchar(1000)
DECLARE @STR VARCHAR(10)
SELECT @STR = LEFT(DateName(month, Specify_Date),3)
SET @String = 'SELECT ID,Name,' + @STR+ ' AS Sales('+ @STR +') FROM Sales INNER JOIN
Animal ON Animal.Id = Sales.Id '
Print @String -- Prints the query just for your understanding

EXEC (@String) -- Gets the result you want

thanks
Jan 2 '08 #4
hI,


use the following query

since you are getting the coulumns dynamically, you need to build the query as string and then execute the query to get the result...


DECLARE @String varchar(1000)
DECLARE @STR VARCHAR(10)
SELECT @STR = LEFT(DateName(month, Specify_Date),3)
SET @String = 'SELECT ID,Name,' + @STR+ ' AS Sales('+ @STR +') FROM Sales INNER JOIN
Animal ON Animal.Id = Sales.Id '
Print @String -- Prints the query just for your understanding

EXEC (@String) -- Gets the result you want

thanks

Thanks a lot! deepuv! This is really helpful!
Jan 3 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Praveen | last post by:
In the ISPF editor I am using, for a particullar PO Dataset I am getting the result of FIND statement narrowed down to the colums 48-56. i.e. if I give "FIND 'TO' ALL", the result I am getting as...
1
by: Hajime Kusakabe | last post by:
I made a datagrid and it contains several columns. The number of columns depend on what users select from a dropdown list. Therefore the number of columns changes. I would like to find the last...
2
by: Josh | last post by:
Hi Guys, I have been stuck on this problem for several days now, i have a set of nested datagrids. Inside the second datagrid i have a dropdown list, a textbox and a label. I want the textbox...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
11
by: Daniel Billingsley | last post by:
I've got a project I've been working on for a few weeks. I've been using the BindingSource control - I've got four of them on this particular form. This morning I went to view the report in the...
1
by: Lars E | last post by:
Hi all I have a small problem. I have a datatable with 8 columns. But it is only data in 5 of the columns. Data for the remaing 3 columns is in another dataset. I Want to run trough the...
1
by: TheSteph | last post by:
Any way to find the Clicked SubItem in a ListView ? private int GetSubItemAtPos(ListView AListView, Point APoint) { int PositionCounter = 0; int TmpRelativeColIndex = -1; //Browse All Column...
2
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I need to locate a row (or see if it exists) in a table contained in a dataset. The DataView.Find method seems to work OK for one criteria but I need to use 2 criterias. Is there...
0
by: SMH | last post by:
Hi All, I am currently learning .Net 2, studying for 70-528. I've hit a bit of a brick wall with DataColumn.Expression. As I understand it, this can be used to (For example) concatenate two...
10
by: nmehring | last post by:
I am writing some c++ code that interacts with a native C library and have to do some dynamic memory allocation to support it. I am getting memory leaks and I think this piece of code is the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.