|
Vuyiswa Maseko wrote: SELECT @s = @s + IsNull(',' + Cast(Cycletemplate AS varchar), '')
FROM #Temp (NOLOCK)
The #Temp table would be empty there? How about using a real table, instead of a temporary one?
I are Troll
|
|
|
|
|
Hi guys, is there a way to retrieve, for a given record, the names of all the columns where the values of those columns for that record match a given condition?
ID A B C D E F G
A1 1 0 1 1 0 1 0
Like if I want to retrieve, for record "A1", all the column names where the value = 1, it would return "A, C, D, F, G".
Can this be done in SQL directly?
Thanks!!
modified on Friday, January 15, 2010 8:56 PM
|
|
|
|
|
There is no easy way, you will need to write some code of your own. Gotchas that spring to mind are datatype conversion and accuracy when comparing numbers - floats for example are only approximate. Its not difficult, but does need a bit of planning and a lot of testing.
Bob
Ashfield Consultants Ltd
Proud to be a Code Project MVP
|
|
|
|
|
Not the nicest looking solution, but it does the trick;
SELECT ID,
CASE WHEN A=1 THEN 'A'
ELSE ''
END +
CASE WHEN B=1 THEN 'B'
ELSE ''
END +
CASE WHEN C=1 THEN 'C'
ELSE ''
END +
CASE WHEN D=1 THEN 'D'
ELSE ''
END +
CASE WHEN E=1 THEN 'E'
ELSE ''
END
FROM [TestTable]
Good luck
I are Troll
|
|
|
|
|
Thank you Eddy, unfortunately I've got 90 columns for conditional check, so I'll have to improvise. However, I could see myself using this code potentially in the future with tables that have fewer columns.
Many thanks again.
|
|
|
|
|
IMO if you need this, you really should rethink your table design. It probably should have been fewer (two or three) columns and more rows, something like ID, name, value
|
|
|
|
|
Unfortunately so far I couldn't think of a better table design without making it even more complicated.
However, I am going to take an alternate route by putting them into an array first, and check for the conditions from there in a loop. This is currently the best I can come up with.
|
|
|
|
|
Go with Luc's suggestion rethink your data structure, this one will drive you nuts. Try this
CREATE TABLE [dbo].[Attr](
[AttrID] [int] IDENTITY(1,1) NOT NULL,
[CustomerID] int,
[AttrType] [varchar](255) NULL,
[AttrValue] [varchar](255) NULL,
[Modified] [datetime] NULL,
[ModifiedBy] [varchar](50) NULL)
Where AttrType = your column Name and AttrValue = 1/0.
CustomerID is the foreign key to the entity you are applying the attributes to.
You would only have 4 rows representing the ACDF colums (0 deemed to not exist)
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I tried to store the picture present in the MY Picture location of my system
but unable to do so
Qurey
CREATE TABLE StockGifs (StockGifID int NOT NULL, Gif varbinary(max) NOT NULL)
INSERT StockGifs (StockGifID, Gif)
SELECT 1, BulkColumn
FROM OPENROWSET (BULK 'C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills',SINGLE_BLOB) AS x
Error Message :Msg 4860, Level 16, State 1, Line 1
Cannot bulk load. The file "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills" does not exist.
Let me know what is the change need to be done
If the location alone Can be stored then how to convert a jpeg file to binary file.
Thanks in Advance,
Praveen Arokiam
|
|
|
|
|
You need to use full pathname of the file, which includes the extension.
Jpeg files are binary files. No need to convert.
|
|
|
|
|
Is the picture on your pc or the server? When you run this SQL it executes on the server hosting sql server, so directories are located on there, not your PC
Bob
Ashfield Consultants Ltd
Proud to be a Code Project MVP
|
|
|
|
|
|
I created a .mdf file of MS-Access which contains some tables.
Now i want data from two or more table in single table.
So i created queries in .mdf file with in Ms-Access. And querying these from my application.
Suppose Details is query name in .mdf file, then from application
i am using
"Select [nName], [nDate] from [Details]" .
Now i have some question on this:
a) If i use above way i.e. (create query on access then use this virtual table in application) It increases file size.
here question is- Will this going to slow application.
b) If i don't create query and from application with help of joins and sub-queries get data in DataTable object then use this DataTable.
But here question is- this DataTable will grow in size by time. this will also create load on application. what if this DataTable be in MB size?
So, Is creating Query(virtual-table / view) in .mdf file is good ?
Or Directly get data in DataTable object, then use this data ?
I am using C#.
any help/comment !!
|
|
|
|
|
Hum Dum wrote: So, Is creating Query(virtual-table / view) in .mdf file is good ?
Yes, in a real relational database (SQL Server, Oracle, MySQL etc) this is called a view and has no cost in disk space. This is probably NOT true for Access as Access is a POS, however using queries/views is still the correct way to go.
You need to evaluate your use of Access as your datastore and make sure it is valid. If more than one user is to access the data then use SQL Server.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hum Dum wrote: I created a .mdf file of MS-Access which contains some tables.
.mdf is the default extension for SQL Server... you sure you didn't use SQL Server? And if you used Access, renaming it .mdf won't make it SQL Server...
Saving a query in Access will not increase the filesize dramatically, it's not like it's making a copy of the actual data tables...
Access is going to be fine as a database engine if you have up to 10 users that are located on the same local network, and not using wireless network connections.
For database size, Anything up to a couple of hundred meg in Access will still give you reasonable performance (depending on your data structures of course)
|
|
|
|
|
_Damian S_ wrote: you sure you didn't use SQL Server?
Yup i am sure on that ,
Only here i mistype .mdb to .mdf.
I have some more question
a) A .mdb file we can store anywhere we want. Like this can we store .mdf(sql server DB file).
b) How can i transfer .mdb file to .mdf along data inside. Or more like moving to sql server. Is there any feature in SQL server to import and convert.
c)At present i am storing mdb file path in my connection string by OpenFileDialog(built in C#.net). Like this Can i search for .mdf file?
and using this file on different pc(LAN network) with my application.
In sql server where can we find our DB file( i hadn't explore much on myself)
regards
|
|
|
|
|
Yes, you can store your mdb file wherever you like. So long as it can be accessed by your front end software (on the same LAN, as I said earlier).
Yes, you can transfer Access data to SQL Server. You can either use the "upsizing wizard", or simply create a blank SQL Server database and import from Access. (Note - this doesn't work with SQL Express - but you can create SQL Express tables and link to Access, then use queries to get your data across).
Yes, you can do similar for SQL Server. Check out www.connectionstrings.com for comprehensive details on how to connect to SQL Server. I wouldn't worry about letting the user search for the .mdf file though - that's not how it works. They simply connect to the SQL Server and your userid/password/login that is set up handles the rest.
In SQL Server, your default location for data files is under MSSQL\Data, and depending on what version you have installed it may be directly under C:\, or in c:\program files etc... have a look around for it. Of course, you could simply open SQL Server and look at the file details of a created database.
|
|
|
|
|
_Damian S_ wrote: I wouldn't worry about letting the user search for the .mdf file though - that's not how it works. They simply connect to the SQL Server and your userid/password/login that is set up handles the rest.
Well for file search i am doing only for first time. If user installing application for first time then it asks for mdb file then i create a text file on local pc, then from next time onward i simply read this file for getting DB file path.
Now Can SQL express do the work(As its freeware i hope so , SQL server is not). If i install Sql Express on one machine then create DB. After this can i use this DB from LAN ?
By providing userid/pass as u said and it handle rest?
regards
|
|
|
|
|
With SQL all you are doing is providing the Servername and Database name to connect. This is the same no matter where you connect from. Directly connecting to the underlying files is something that is only of use in on-the-fly demos and not to be used for serious applications.
SQL Express will work fine for this, though it has some limitations See this Microsoft article for details[^]. The two main issues will be a reduced functionality Management Studio GUI and the 4GB data limit.
|
|
|
|
|
Hi,
I am working with an SSIS package(2005), which dumps records from a csv file into TABLE1. After making some alterations to thses records, it is gain inserted into another TABLE2 in the DB.
One of the columns is a date column. TABLE2 has all the datatypes fixed and it has a col with datatype "smalldatetime". TABLE1 is created each time the package is run. So at first i made the TABLE1.Rdate col datatype datetime but when i try converting this col to smalldatetime for inserting into TABLE2 it gives an error "The conversion of a datetime data type to a smalldatetime data type resulted in an out-of-range datetime value."
I tried changing the date col datatype to VARCHAR but again at the time of converting this col to smalldatetime for inserting into TABLE2 it gives an error "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.". The date in the TABLE1.Rdate col is in the format '2009-10-13'.
Can anyone help me with this Please?
Thanks & regards,
Payal
|
|
|
|
|
I always just chuck everything into a varchar field when using SSIS and let the down stream stored proc do all the transforms. I can deal with problems better in a stored proc than insinde an SSIS package.
Look in BOL for CONVERT and use one of the modifiers, 111 - Japan looks like the most likely
CONVERT(DATETIME, varcharfield,111)
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
Hi guys, thanks for all the help recently, this is really a great forum!
Today I was trying to write what seems like a simple query statement, but unbelievably after hours of trying I still can't get it to work.
My table looks like the following:
ID Ticker SlopeofLine
1 MSFT 5
2 MSFT 2
3 MSFT 1
4 MSFT 0
5 MSFT -3
6 MSFT 1
1 DELL -2
2 DELL -1
3 DELL 2
4 DELL 4
5 DELL 3
6 DELL 4
...
...
I want to retrieve, for each Ticker, the last/largest ID where SlopeofLine is less than 0. So ideally, the query should retrieve this from the above table:
ID Ticker SlopeofLine
5 MSFT -3
2 DELL -1
Please forgive me if this is a stupid question; I'd greatly appreciate any help.
My query which doesn't work currently look like this:
Select Ticker, ID, SlopeOfLine
From Table
Where SlopeofLine < 0
AND ID = (SELECT MAX(ID) FROM Table t1 Where Table.Ticker = t1.Ticker)
|
|
|
|
|
Hi,
I would try:
Select Ticker, MAX(ID) as MAXID, SlopeOfLine
From Table
Where SlopeofLine < 0 GROUP BY Ticker
Not tested!
[ADDED]
Now tested, and considered incorrect: Ticker and MAX(ID) are fine, SlopeOfLine is not.
conclusion: you need two selects.
[/ADDED]
modified on Wednesday, January 13, 2010 8:22 PM
|
|
|
|
|
You may want to try something like this:
select last(ID),Ticker,last(SlopeofLine) from
(select ID,Ticker,SlopeofLine from YourTableName where SlopeofLine <0 order by Ticker,ID)
group by Ticker;
Happy querying!
|
|
|
|
|