|
Sure, try this:
<br />
declare @list varchar(1000)<br />
declare @len int<br />
<br />
select @list = isnull(@list, '') + colors + ',', @len = len(colors) from ColorTable<br />
<br />
select @list = substring(@list, 1, len(@list)-1)<br />
select substring(@list, 1, len(@list) - @len - 1) + " or " + substring(@list, len(@list) - @len, @len)<br />
I'm not in front of a SQL Server right now so I can't try it easily, and I may be off by a character (and thus a -1 or +1 in the substring lengths) here and there. I'm sure you can figure out the tweaks, though.
The concept is to have a variable hold the length of the last element. It does this by constantly replacing its value with the length of the current element; at the end of the query, it will contain the length of the last element. Once you know that, it's a matter of string handling to cut your list up and replace text as necessary.
Two more things:
1. If there's a possibility that ColorTable may contain zero, one or two rows, or if you're using a where clause that may return only zero, one or two rows, you may want either some if/then or case when clauses with select count statements to ensure you don't return errors.
2. While it's possible to do this in SQL, as demonstrated, there are some things that are better left to code. I assume you have a really good reason for doing it on the SQL Server instead of in whatever language you're using for your queries, but in general I'd recommend this be done in a code library instead of on the DB server.
|
|
|
|
|
Thanks a lot for your help.
Thanks a lot for taking the time to reply.
|
|
|
|
|
Thanks.It's just what I needed.
|
|
|
|
|
Hi all..
Can any one tell me how i can find second largest value of perticular
attribute. Please give query fetching second largest value.
Rahul Kulkarni
|
|
|
|
|
Try:
<br />
select Max(attr)<br />
from table<br />
Where attr < (select Max(attr) from table)<br />
Wout Louwers
|
|
|
|
|
what is about 7th highest. can you tell me plz
The secret of life is not enjoyment
but education through experience.
- Swami Vivekananda.
|
|
|
|
|
Select *,sal From Emp X Where n =<br />
(<br />
Select Count(Distinct Sal) From Emp Where sal >=X.sal<br />
)
Where n = 7
|
|
|
|
|
Thanks It is the correct answer . Thanks Again.
The secret of life is not enjoyment
but education through experience.
- Swami Vivekananda.
|
|
|
|
|
Hello Friends,
May be I am missing some thing here while trying to build a dynamic WHERE clause.
this is the final version of my Stored pror
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[USP_SearchVehicleOnYMM]
@Year char(4)
,@Make int
,@Model int
AS
BEGIN
SET NOCOUNT ON;
IF LEN(@Year)<=0
begin
select @Year = NULL
end
IF LEN(@Make)<=0
begin
select @Make = NULL
end
IF LEN(@Model)<=0
begin
select @Model = NULL
end
SELECT V.vin
,C.FirstName
,C.LastName
,C.AccountNum
,D.DealerShipName
,Contrt.MaturityDate
,VS.GroundedDate
,V.[Year]
,VMkL.VehicleMakeDesc
,VMdL.ModelDesc
FROM Vehicle V
Inner Join ContractInfo contrt ON Contrt.VehicleID = V.VehicleID
Left Join DealerShips D ON v.ManufacturerID = D.ManufacturerID
Inner Join Customer C ON Contrt.CustomerID = C.CustomerID
Inner Join VehicleStatus VS ON Contrt.VehicleID = VS.VehicleID
Left Join ManufacturerMakeModel mmm ON v.MakeID = mmm.makeID
left Join VehicleMakeLookUp vmkl ON mmm.MakeID =
vmkl.VehicleMakeID
left Join VehicleModelLookup VMdl ON mmm.ModelID =
VMdl.ModelID
WHERE
((@Year IS NULL ) OR (V.Year=@Year))
and
(( @make = 0 ) OR (VMkL.VehicleMakeDesc=@make))
and
(( @Model = 0 ) OR (VMdL.ModelDesc=@Model))
END
And when I exectue (right click on the stored proc in management studio
and pick the exectue option) this with out providing any values to the
Year, Make and Model I am getting this error
**********************************Error*****************************************************
Msg 201, Level 16, State 4, Procedure USP_SearchVehicleOnYMM, Line 0
Procedure or Function 'USP_SearchVehicleOnYMM' expects parameter
'@Year', which was not supplied.
(1 row(s) affected)
***************************************************************************************************
Could some one please fix this error and have me understand where I am doing wrong?
Thanks
-L
|
|
|
|
|
Specifying a default value for parameters
ALTER PROCEDURE [dbo].[USP_SearchVehicleOnYMM]
@Year char(4)=NULL,
@Make int=NULL,
@Model int=NULL
AS
....
|
|
|
|
|
Hey it helped me Thank you.
|
|
|
|
|
Hi
I am trying to Execute an SSIS Package from C# Dot Net.
Below the code i am using.
try
{
Application app = new Application();
Package package = app.LoadPackage("DTSPackage/WinServeMonitoring.dtsx", null);
DTSExecResult result = package.Execute();
label1.Text = "Package Execution " + result.ToString();
}
catch (Exception exp)
{
label1.Text = exp.Message.ToString();
}
I have added reffrence for DTS Run time (using Microsoft.SqlServer.Dts.Runtime;)
But I am getting an Error like this
Error 1 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'Microsoft.SqlServer.Dts.Runtime.Application' C:\Documents and Settings\vahqmd10\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 23 17 WindowsApplication1
Error 2 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'Microsoft.SqlServer.Dts.Runtime.Application' C:\Documents and Settings\vahqmd10\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 23 39 WindowsApplication1
Error 3 The type 'System.Windows.Forms.Application' has no constructors defined C:\Documents and Settings\vahqmd10\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 23 35 WindowsApplication1
Can any one Help me !
Regards
Deepu M.I
|
|
|
|
|
I create a triggers but I need to add the time
how can I get the time NOW from sql server
thanks
dadax
|
|
|
|
|
GetDate() will return the current date and time. It is easy enough to just get the time portion from the returned DateTime
Michael
CP Blog [^] Development Blog [^]
|
|
|
|
|
I have a table contain ( username,password )
and I'll save that password encrypted in the database
how can I do this ?
thanks
dadax
|
|
|
|
|
Encrypt the password first and then save it in the database like anything else.
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
My: Website | Blog
|
|
|
|
|
How can I encrypt it ?
( from SQL )
dadax
|
|
|
|
|
MohamadJaber wrote: How can I encrypt it ?
( from SQL )
As I already said "Encrypt the password first" then put it in SQL. You cannot encrypt it in SQL without using extended stored procedures in SQL Server 2000 or using CLR stored procedures in SQL Server 2005. Either which way the encryption process does not happen in SQL code.
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
My: Website | Blog
|
|
|
|
|
sorry annoyance you but plz can you tell how can I enrypted and decrypted it using CLR stored procedures in SQL Server 2005
thanks anyway for you help
dadax
|
|
|
|
|
You don't want to do that anyway. From a security standpoint, there's practically no value of encrypting passwords on the server if you transmit them over plaintext SQL. Anyone with a packet logger (easy) can get as many passwords as they could by finding an admin password and reading them out of your DB (hard).
|
|
|
|
|
Here u got good example how to hash password
http://www.c-sharpcorner.com/Code/2003/Feb/HashPassword.asp
|
|
|
|
|
You don't need CLR stored procs to encrypt or decrypt. SQL 2005 has encryption built in. Take a look at my blog at http://www.cubido.at/Blog/tabid/176/EntryID/34/Default.aspx. There you can find an example including a mind map describing SQL 2005 encryption functions.
Regards,
Rainer.
Rainer Stropek
Visit my blog at http://www.cubido.at/Blog/tabid/176/BlogID/4/Default.aspx
|
|
|
|
|
hi
What is the query to take value stored in variable
this value comes from textbox control and then datagrid is filtered through this value
Thanks
|
|
|
|
|
Something like this:
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "SELECT * FROM MyTable WHERE filterColumn = @filterValue";
cmd.Parameters.Add("@filterValue", myTextBox.Text);
da.Fill(myDataSet);
Assumptions:
* You have an existing SqlConnection called conn
* The table is called MyTable
* You are filtering on a column called filterColumn
* You have a TextBox called myTextBox
* You have a DataSet called myDataSet
Does this help?
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."
--Charles Babbage (1791-1871)
My: Website | Blog
|
|
|
|
|
Thank You Mackay
It is working ....
Thank You once again..
|
|
|
|