Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Sunday, March 25, 2012

Addition of strings

How can i make a sum (concatenation) of strings of one column in a table.

for example i have a table like this

field1 field2

1 abc

1 bcd

2 sdf

2 sdd

I want to get these strings added group by field 1

Thanks

This cannot be done in a single query unless you employ some tricky hack. There is no obvious and reliable solution to do this in a single query. You'll have to use a cursor and a table variable for this purpose.

|||

Hi,

with .Net 2.0 it is very easy. You can write "User defined aggregate" called for example StringConcat and use it in your queries like this

SELECT dbo.StringConcat(field2)FROM MyTableGROUP BY field1

and here is the code for your aggregate

C#

using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsoft.SqlServer.Server;using System.Text;using System.IO;using System.Runtime.InteropServices;[Serializable][SqlUserDefinedAggregate( Format.UserDefined,// user-defined serialization IsInvariantToDuplicates =false,// duplicates matter IsInvariantToNulls =true,// don't care about NULLs IsInvariantToOrder =false,// order matters (ignored) IsNullIfEmpty =false,// don't yield NULL if empty set MaxByteSize = 8000)]// maximum size in bytespublic struct StringConcat : IBinarySerialize{private StringBuilder sb;public void Init() {this.sb =new StringBuilder(); }public void Accumulate(SqlString s) {if (s.IsNull) {return;// skip NULLs }else {this.sb.Append(s.Value); } }public void Merge(StringConcat Group) {this.sb.Append(Group.sb); }public SqlString Terminate() {return new SqlString(this.sb.ToString()); }public void Read(BinaryReader r) { sb =new StringBuilder(r.ReadString()); }public void Write(BinaryWriter w) {if (this.sb.Length > 4000)// limit sb to 8000 bytes w.Write(this.sb.ToString().Substring(0, 4000));else w.Write(this.sb.ToString()); }}// end StringConcat

VB

Imports SystemImports System.DataImports System.Data.SqlTypesImports Microsoft.SqlServer.ServerImports System.TextImports System.IOImports System.Runtime.InteropServices' user-defined serialization' duplicates matter' don't care about NULLs' order matters (ignored) ' don't yield NULL if empty set' maximum size in bytes is 8000<Serializable(), _ SqlUserDefinedAggregate( _ Format.UserDefined, _ IsInvariantToDuplicates:=False, _ IsInvariantToNulls:=True, _ IsInvariantToOrder:=False, _ IsNullIfEmpty:=False, _ MaxByteSize:=8000)> _Public Structure StringConcatImplements IBinarySerializePrivate sbAs StringBuilderPublic Sub Init()Me.sb =New StringBuilder()End Sub Public Sub Accumulate(ByVal sAs SqlString)If s.IsNullThen Return' skip NULLsElse Me.sb.Append(s.Value)End If End Sub Public Sub Merge(ByVal GroupAs StringConcat)Me.sb.Append(Group.sb)End Sub Public Function Terminate()As SqlStringReturn New SqlString(sb.ToString())End Function Public Sub Read(ByVal rAs BinaryReader) _Implements IBinarySerialize.Read sb =New StringBuilder(r.ReadString())End Sub Public Sub Write(ByVal wAs BinaryWriter) _Implements IBinarySerialize.WriteIf Me.sb.Length > 4000Then' limit sb to 8000 bytes w.Write(Me.sb.ToString().Substring(0, 4000))Else w.Write(Me.sb.ToString())End If End SubEnd Structure' end StringConcat

I hope this helps.

Let me know if this worked for you.

|||

thanks, But I am looking for something within the sql

|||

Hi,

After compiling the class into a DLL, you can import the DLL as a SQL Server assembly using either the Visual Studio 2005 Deploy option or manually using the CREATE ASSEMBLY statement and CREATE AGGREGATE statement as is shown in the following listing:

 
CREATE ASSEMBLY StringConcatFROM'C:\StringConcat.dll'GOCREATE AGGREGATE StringConcat(@.inputnvarchar)RETURNSnvarcharEXTERNALNAME StringConcat.StringConcatGO

Using CLR objects (functions, aggregates...) for string manipulations or complex calculations is way faster than using nested queries or Cursors.

I have used this aggregate in my projects and it works fine.

|||

Hi, jiju-kj-

Of course it is possible to achieve this within the SQL. But you will need nested cursors the first cursor will iterate through distinct values in field1 and the inner cursor will iterate through all values from field2 for the current value in field1. I don't recommend you using cursors. But will decide which way is better for you. Let me know if you have problems with creating the cursors.

Cheers,

Paul

sql

Addition of a number to an INT column

Hi,
I have several INT columns in a table that I need to update.
For example, in column 'aa' I need to add 2 to all of the values in that column.
I'm using Query Analyzer - what update statement should I write?Hmm - no answers - should I be in a different forum for this question?

I guess I could do:

UPDATE table_name SET aa = 16 WHERE aa = 14
UPDATE table_name SET aa = 15 WHERE aa = 13
UPDATE table_name SET aa = 14 WHERE aa = 12

and keep going like this until I have all the values updated.

Note that I've done it from highest number first, otherwise all of the data would get adjusted to the two highest numbers.

I was looking for a more elegant solution If anyone can think of one as I have several columns to update all with slightly different increases.|||UPDATE table_name SET aa = aa + 2

:D|||Of course - thank you - how could I miss it??

Woods for the trees and all...

Monday, March 19, 2012

Adding spaces inside text box values

I need to adding spaces inside the expression of a text box (in table header
or in page header).
For example: = "string 1" & " " & "string 2";
this expression returns only one space between string 1 and string 2.
Many thanksHi,
Just use =space(<no of spaces>) e.g space(10)
Amarnath
"Pasquale" wrote:
> I need to adding spaces inside the expression of a text box (in table header
> or in page header).
> For example: = "string 1" & " " & "string 2";
> this expression returns only one space between string 1 and string 2.
> Many thanks|||I have tried this suggest: it functions inside Visual Studio.NET environment
but then
his distribution I have seen only a single space!
How can I solve this issue?
Thanks
"Amarnath" wrote:
> Hi,
> Just use =space(<no of spaces>) e.g space(10)
> Amarnath
> "Pasquale" wrote:
> > I need to adding spaces inside the expression of a text box (in table header
> > or in page header).
> > For example: = "string 1" & " " & "string 2";
> > this expression returns only one space between string 1 and string 2.
> >
> > Many thanks|||Try adding the space inside the string that you need the space like
= "string 1 " & "string 2"; where the space is inserted aftter the 1.
"Pasquale" wrote:
> I need to adding spaces inside the expression of a text box (in table header
> or in page header).
> For example: = "string 1" & " " & "string 2";
> this expression returns only one space between string 1 and string 2.
> Many thanks|||I have used = string1 & " " & string2 and it has worked. But the room
used by the spaces is not the same as you see when you define it. Try to put
much more spaces between the strings and you will see the distance increase.
"Pasquale" wrote:
> I have tried this suggest: it functions inside Visual Studio.NET environment
> but then
> his distribution I have seen only a single space!
> How can I solve this issue?
> Thanks
>
> "Amarnath" wrote:
> > Hi,
> >
> > Just use =space(<no of spaces>) e.g space(10)
> >
> > Amarnath
> >
> > "Pasquale" wrote:
> >
> > > I need to adding spaces inside the expression of a text box (in table header
> > > or in page header).
> > > For example: = "string 1" & " " & "string 2";
> > > this expression returns only one space between string 1 and string 2.
> > >
> > > Many thanks|||I have posted this issue then executing some proofs to put some spaces in a
text box.
I have tried:
- = "string1 " & "string2";
- = "string1" & space(10) & "string2";
- = "string1" & " " & "string2";
- = "string1 " & " " & space(10) & "
string2".
These solutions function inside MS VisualStudio .NET (I see the results by
preview),
BUT NOT FUNCTION AFTER THEIR DISTRIBUTION (I see the results inside Internet
browser).
Many thanks
"PSM" wrote:
> I have used = string1 & " " & string2 and it has worked. But the room
> used by the spaces is not the same as you see when you define it. Try to put
> much more spaces between the strings and you will see the distance increase.
> "Pasquale" wrote:
> > I have tried this suggest: it functions inside Visual Studio.NET environment
> > but then
> > his distribution I have seen only a single space!
> >
> > How can I solve this issue?
> >
> > Thanks
> >
> >
> > "Amarnath" wrote:
> >
> > > Hi,
> > >
> > > Just use =space(<no of spaces>) e.g space(10)
> > >
> > > Amarnath
> > >
> > > "Pasquale" wrote:
> > >
> > > > I need to adding spaces inside the expression of a text box (in table header
> > > > or in page header).
> > > > For example: = "string 1" & " " & "string 2";
> > > > this expression returns only one space between string 1 and string 2.
> > > >
> > > > Many thanks|||The only thing it occur to me is to use non-breaking spaces. You can use
ChrW(160) as nonbreaking space and the Internet browser won't change them.
"Pasquale" wrote:
> I have posted this issue then executing some proofs to put some spaces in a
> text box.
> I have tried:
> - = "string1 " & "string2";
> - = "string1" & space(10) & "string2";
> - = "string1" & " " & "string2";
> - = "string1 " & " " & space(10) & "
> string2".
> These solutions function inside MS VisualStudio .NET (I see the results by
> preview),
> BUT NOT FUNCTION AFTER THEIR DISTRIBUTION (I see the results inside Internet
> browser).
> Many thanks
>
> "PSM" wrote:
> > I have used = string1 & " " & string2 and it has worked. But the room
> > used by the spaces is not the same as you see when you define it. Try to put
> > much more spaces between the strings and you will see the distance increase.
> >
> > "Pasquale" wrote:
> >
> > > I have tried this suggest: it functions inside Visual Studio.NET environment
> > > but then
> > > his distribution I have seen only a single space!
> > >
> > > How can I solve this issue?
> > >
> > > Thanks
> > >
> > >
> > > "Amarnath" wrote:
> > >
> > > > Hi,
> > > >
> > > > Just use =space(<no of spaces>) e.g space(10)
> > > >
> > > > Amarnath
> > > >
> > > > "Pasquale" wrote:
> > > >
> > > > > I need to adding spaces inside the expression of a text box (in table header
> > > > > or in page header).
> > > > > For example: = "string 1" & " " & "string 2";
> > > > > this expression returns only one space between string 1 and string 2.
> > > > >
> > > > > Many thanks|||Excellent!
This is the solution! Many thanks
"PSM" wrote:
> The only thing it occur to me is to use non-breaking spaces. You can use
> ChrW(160) as nonbreaking space and the Internet browser won't change them.
>
> "Pasquale" wrote:
> > I have posted this issue then executing some proofs to put some spaces in a
> > text box.
> >
> > I have tried:
> > - = "string1 " & "string2";
> > - = "string1" & space(10) & "string2";
> > - = "string1" & " " & "string2";
> > - = "string1 " & " " & space(10) & "
> > string2".
> >
> > These solutions function inside MS VisualStudio .NET (I see the results by
> > preview),
> > BUT NOT FUNCTION AFTER THEIR DISTRIBUTION (I see the results inside Internet
> > browser).
> >
> > Many thanks
> >
> >
> >
> > "PSM" wrote:
> >
> > > I have used = string1 & " " & string2 and it has worked. But the room
> > > used by the spaces is not the same as you see when you define it. Try to put
> > > much more spaces between the strings and you will see the distance increase.
> > >
> > > "Pasquale" wrote:
> > >
> > > > I have tried this suggest: it functions inside Visual Studio.NET environment
> > > > but then
> > > > his distribution I have seen only a single space!
> > > >
> > > > How can I solve this issue?
> > > >
> > > > Thanks
> > > >
> > > >
> > > > "Amarnath" wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > Just use =space(<no of spaces>) e.g space(10)
> > > > >
> > > > > Amarnath
> > > > >
> > > > > "Pasquale" wrote:
> > > > >
> > > > > > I need to adding spaces inside the expression of a text box (in table header
> > > > > > or in page header).
> > > > > > For example: = "string 1" & " " & "string 2";
> > > > > > this expression returns only one space between string 1 and string 2.
> > > > > >
> > > > > > Many thanks|||Yeah, I just wasted 2 hours on trying to fix this, excellent :-)
In case you were wondering what is going on I can explain...
I have a SQL statement that returns a string of dates delimited by 8 spaces,
eg.
1-Mar-2006 2-Mar-2006 etc.
When in VS.Net IDE the preview window shows the spaces correctly but when
deployed to a website they are displayed as HTML and hence the multiple
spaces are ignored and displayed as a single space.
Try putting in and Reporting services sees the & and converts it to a html
&, so your source looks like , insert scream here.
CharW(160) gets past this and is rendered into HTML by RS as
Thanks again, I will be able to sleep tonight.
John
"Pasquale" wrote:
> Excellent!
> This is the solution! Many thanks
>
> "PSM" wrote:
> > The only thing it occur to me is to use non-breaking spaces. You can use
> > ChrW(160) as nonbreaking space and the Internet browser won't change them.

Sunday, March 11, 2012

Adding Report Filter on Float field

I am adding a filter on a float datatype field in my dataset.
Example:
=Fields!SharePrice.Value > 50
When running the report, I get the following error:
"...the processing of filter for the data set 'dataset' cannot be performed.
The comparision failed. Please check the data type returned by the data
expression."
I've tried casting the field as decimal with mixed results. Does anyone
know why this error occurs?You'd have to explicitly cast the return value using CSng() function.
--
Ravi Mumulla (Microsoft)
SQL Server Reporting Services
This posting is provided "AS IS" with no warranties, and confers no rights.
"Rod Bautista" <rod.bautista@.adam-us.com> wrote in message
news:uZ9L9oWWEHA.3716@.TK2MSFTNGP11.phx.gbl...
> I am adding a filter on a float datatype field in my dataset.
> Example:
> =Fields!SharePrice.Value > 50
> When running the report, I get the following error:
> "...the processing of filter for the data set 'dataset' cannot be
performed.
> The comparision failed. Please check the data type returned by the data
> expression."
> I've tried casting the field as decimal with mixed results. Does anyone
> know why this error occurs?
>|||Try this:
Filter expression: =CDbl(Fields!SharePrice.Value)
Operator: >
Filter value: =CDbl(50)
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Rod Bautista" <rod.bautista@.adam-us.com> wrote in message
news:uZ9L9oWWEHA.3716@.TK2MSFTNGP11.phx.gbl...
> I am adding a filter on a float datatype field in my dataset.
> Example:
> =Fields!SharePrice.Value > 50
> When running the report, I get the following error:
> "...the processing of filter for the data set 'dataset' cannot be
performed.
> The comparision failed. Please check the data type returned by the data
> expression."
> I've tried casting the field as decimal with mixed results. Does anyone
> know why this error occurs?
>|||Thanks. That did the trick.
"Robert Bruckner [MSFT]" <robruc@.online.microsoft.com> wrote in message
news:eNogP1WWEHA.2844@.TK2MSFTNGP12.phx.gbl...
> Try this:
> Filter expression: =CDbl(Fields!SharePrice.Value)
> Operator: >
> Filter value: =CDbl(50)
> --
> This posting is provided "AS IS" with no warranties, and confers no
rights.
>
> "Rod Bautista" <rod.bautista@.adam-us.com> wrote in message
> news:uZ9L9oWWEHA.3716@.TK2MSFTNGP11.phx.gbl...
> > I am adding a filter on a float datatype field in my dataset.
> >
> > Example:
> >
> > =Fields!SharePrice.Value > 50
> >
> > When running the report, I get the following error:
> >
> > "...the processing of filter for the data set 'dataset' cannot be
> performed.
> > The comparision failed. Please check the data type returned by the data
> > expression."
> >
> > I've tried casting the field as decimal with mixed results. Does anyone
> > know why this error occurs?
> >
> >
>

Thursday, March 8, 2012

Adding optional criteria in a select proc

I would like to write 1 proc that can take additional criteria if its sent in. An example is:

select HA.PriceId, HA.VendorPackageId from Criteria HA Inner Join
(
select VendorPackageId from ValidVendorPackages
where Vendor = @.VENDOR
and Sitecode = @.SITECODE
and PackageType = @.PACKAGETYPE
)HB on HA.VendorPackageId = HB.VendorPackageId
and CriteriaId in
(
select CriteriaID from ValidItemCriteria
where Destination = @.DESTINATION
and LengthOfStay = @.LENGTHOFSTAY
and Ages = @.AGE
and ComponentType = @.COMPONENTTYPE_1
and ValidItemType = @.VALIDITEMTYPE_1
and ItemValue = @.ITEMVALUE_1
)

Multiple @.COMPONENTTYPE, @.VALIDITEMTYPE,@.ITEMVALUE can be sent in.
Instead of making multiple procs or copying the proc multiple times with an if statement at the top checking the number of parameters that aren't =''. Is there a way to exectue:

and CriteriaId in
(
select CriteriaID from ValidItemCriteria
where Destination = @.DESTINATION
and LengthOfStay = @.LENGTHOFSTAY
and Ages = @.AGE
and ComponentType = @.COMPONENTTYPE_1
and ValidItemType = @.VALIDITEMTYPE_1
and ItemValue = @.ITEMVALUE_1
)
and CriteriaId in
(
select CriteriaID from ValidItemCriteria
where Destination = @.DESTINATION
and LengthOfStay = @.LENGTHOFSTAY
and Ages = @.AGE
and ComponentType = @.COMPONENTTYPE_2
and ValidItemType = @.VALIDITEMTYPE_2
and ItemValue = @.ITEMVALUE_2
)
and CriteriaId in
(
select CriteriaID from ValidItemCriteria
where Destination = @.DESTINATION
and LengthOfStay = @.LENGTHOFSTAY
and Ages = @.AGE
and ComponentType = @.COMPONENTTYPE_3
and ValidItemType = @.VALIDITEMTYPE_3
and ItemValue = @.ITEMVALUE_3
)

Ignoring the 2nd 2 selects if @.COMPONENTTYPE_2, @.VALIDITEMTYPE_2,@.ITEMVALUE_2 and @.COMPONENTTYPE_3, @.VALIDITEMTYPE_3,@.ITEMVALUE_3 are = ''

Thanks for your help in advance.Yes, there are ways to do this, but I haven't seen any generic way. It requires knowledge of the conditions that apply (especially how your code needs to handle incomplete sets of criteria, such as when only the second value for @.COMPONENTTYPE is supplied without the second @.VALIDITEMTYPE or @.ITEMVALUE ).

I've never found a satisfactory generic way to handle this kind of problem.

-PatP|||Yes, there are ways to do this, but I haven't seen any generic way. It requires knowledge of the conditions that apply (especially how your code needs to handle incomplete sets of criteria, such as when only the second value for @.COMPONENTTYPE is supplied without the second @.VALIDITEMTYPE or @.ITEMVALUE ).

I've never found a satisfactory generic way to handle this kind of problem.

-PatP

You will never have a 2nd and not a first.

You'll either have 1, 2 or 3 of
@.COMPONENTTYPE
@.VALIDITEMTYPE
@.ITEMVALUE

if they send in all 3 @.ITEMVALUE_1 thourgh 3 will be populated and so will @.VALIDITEMTYPE & @.ITEMVALUE|||Is this close to what you want? I hope you are not expecting any conditional search such as this to run particularly fast...

select Distinct Criteria.PriceId, Criteria.VendorPackageId
from Criteria
Inner Join ValidVendorPackages
on Criteria.VendorPackageId = ValidVendorPackages.VendorPackageId
and ValidVendorPackages.Vendor = @.VENDOR
and ValidVendorPackages.Sitecode = @.SITECODE
and ValidVendorPackages.PackageType = @.PACKAGETYPE
Left Outer Join ValidItemCriteria VIC_1
on Criteria.CriteriaID = VIC_1.CriteriaID
and VIC_1.Destination = @.DESTINATION
and VIC_1.LengthOfStay = @.LENGTHOFSTAY
and VIC_1.Ages = @.AGE
and VIC_1.ComponentType = @.COMPONENTTYPE_1
and VIC_1.ValidItemType = @.VALIDITEMTYPE_1
and VIC_1.ItemValue = @.ITEMVALUE_1
Left Outer Join ValidItemCriteria VIC_2
on Criteria.CriteriaID = VIC_2.CriteriaID
and VIC_2.Destination = @.DESTINATION
and VIC_2.LengthOfStay = @.LENGTHOFSTAY
and VIC_2.Ages = @.AGE
and VIC_2.ComponentType = @.COMPONENTTYPE_2
and VIC_2.ValidItemType = @.VALIDITEMTYPE_2
and VIC_2.ItemValue = @.ITEMVALUE_2
Left Outer Join ValidItemCriteria VIC_3
on Criteria.CriteriaID = VIC_1.CriteriaID
and VIC_3.Destination = @.DESTINATION
and VIC_3.LengthOfStay = @.LENGTHOFSTAY
and VIC_3.Ages = @.AGE
and VIC_3.ComponentType = @.COMPONENTTYPE_3
and VIC_3.ValidItemType = @.VALIDITEMTYPE_3
and VIC_3.ItemValue = @.ITEMVALUE_3
where VIC_1.CriteriaID is not null
or VIC_2.CriteriaID is not null
or VIC_3.CriteriaID is not null|||Are the matches against collections of criteria (for example, a given row needs to match any one of the vendors, any one of the site codes, and any one of the item values in order to qualify), or are the matches against sets of criteria (a row needs to match on vendor N, site code N, and criteria N in order to qualify)? That makes a considerable difference in how the code needs to work.

Do NULL values matter (do you ever need to search for a NULL criteria)? That's a really nasty twist from a performance perspective.

-PatP|||SELECT
...
WHERE
...
and CriteriaID in
(
select CriteriaID from ValidItemCriteria
where Destination = @.DESTINATION
and LengthOfStay = @.LENGTHOFSTAY
and Ages = @.AGE
and
(
coalesce(@.COMPONENTTYPE_1, @.VALIDITEMTYPE_1, @.ITEMVALUE_1) is null
OR
(ComponentType = @.COMPONENTTYPE_1
and ValidItemType = @.VALIDITEMTYPE_1
and ItemValue = @.ITEMVALUE_1)
)
and
(
coalesce(@.COMPONENTTYPE_2, @.VALIDITEMTYPE_2, @.ITEMVALUE_2) is null
OR
(ComponentType = @.COMPONENTTYPE_2
and ValidItemType = @.VALIDITEMTYPE_2
and ItemValue = @.ITEMVALUE_2)
)
and
(
coalesce(@.COMPONENTTYPE_3, @.VALIDITEMTYPE_3, @.ITEMVALUE_3) is null
OR
(ComponentType = @.COMPONENTTYPE_3
and ValidItemType = @.VALIDITEMTYPE_3
and ItemValue = @.ITEMVALUE_3)
)
)|||I'm just curious, but would you please explain what you think that SQL will do?

-PatP|||I'm just curious, but would you please explain what you think that SQL will do?

-PatP

Its useless, I noticed the flaw myself. I realized that I was negating the first criteria if I found criteria on the second. I have changed the question to a new post with a query I belive will complish this:Optional Inner Joins.

Please help if you can.

Tuesday, March 6, 2012

Adding Misc files to Solution Explorer

I assume this is the best forum for this quesiton; if not, please direct me.

I have noticed that I can add (for example) a text file to a misc folder in a SQL Server project by dragging and dropping the file from Windows Explorer onto the Misc folder inside the SQL Server project. Is this the only way to add a file?

I noticed, for example, I could not copy and past to the Misc file folder.
Is there another way? If so, what is it? what is the preferred way?

I noticed that there have bee 55 views of this thread. It was posted Monday morning. would someone take a stab at it?|||The other way to add a file is to right click on the Project, select Add Existing Item, then Browse to the file in question (you need to change the File Type at the bottom to "All" in order to see text files, etc.).

Thanks, MJ

Adding Milliseconds to Time

Hi,
i am trying to add milliseconds to a time. For example if i have a time of 01:01:05:000 and i want to add 0.297 milliseconds to it i use the following simplified query


SELECT CONVERT(nvarchar(20), DATEADD(ms, 0.297, '00:01:05:000'), 14) AS Expr1

However instead of getting 01:01:05:0.297 i get 01:01:05:000. Can somebody please tell me what i am doing wrong.

Thanks in advance.Try:


SELECT CONVERT(nvarchar(20), DATEADD(ms, 297, '00:01:05:000'), 14) AS Expr1

Terri|||hi tmorton, thanks for the reply, however what if the vlaue to add in milliseconds is 0.297? How would i change the sql query?|||0.297 milliseconds, as in 0.000297 seconds? Sorry, SQL Server is not that granular. SQL Server is accurate only down to 3.33 millseconds. You can read up ondatetime and smalldatetime (Transact-SQL Reference (SQL Server)).

Terri|||ok got it working,


SELECT CONVERT(varchar(20), DATEADD(ms, Exec_Time * 1000, CONVERT(varchar(20), [date], 14)), 14)
AS Expr1
FROM llserverlogs
WHERE (Server = 'llkwa001-ukbg')
ORDER BY [date]

thanks for the help

Sunday, February 19, 2012

Adding empty space

I want to add some spaces on the starting feild value. Please see the below example

" " & Fields!Name.Value

This is working on my BIDS. But the spaces are removed automatically when I deployed the report to the report manager. What should be the problem.

Rather than inserting spaces, can you adjust the padding property on the textbox? If you goal is to have the field indented, that should work.|||

Try using StrDup(3, Chr(32)) & Fields!Name.Value or Space(3) & Fields!Name.Value

Shyam

|||

I used padding property to indent the field. its working fine.

Thanks jwelch.

|||

Hi Folks,

I'm attempting to add empty spaces to format my report. I have one subreport that's a part of my report. I used the "Space()" function and it worked nicely within VS2005, however when I moved my report to the Report Server all my heading and detail data had one space in between them. I also tried "strDUP(3, " ")" and it too worked fine in VS2005 but not on the Report Server. Below is a sample of my code in what I trying to accomplish.

="DIR #" & StrDup(5, " ") & "ST" & StrDup(4, " ") &

"DIR NAME" & StrDup(20, " ") & "PUBCO" &

StrDup(7, " ") & "CLOSE" & StrDup(10, " ") & "ISSUE"

|||Spaces don't work very well when you are rendering as HTML, as browsers tend to ignore repeated white space. Any reason you couldn't use a table instead?|||

Hi John,

I'm currently using a table and it consist of one large column because my subreport is a part of it.

Best regards

|||How about using seperate columns for displaying this info, and merging the cells for the subreport?|||

John - I followed your advice and added a table within my table and it works fine now.

Thanks for your help.

adding dummy column in Sql Query

Hi for all

How to Add a Dummy column in my Sqlquery like Serial Numbers
for example my query is
Select OrderNo,OrderDate,OrderValue,DummyColumn as Slno from OrderTable Where OrderValue>4000

( DummyColumn this column was not in Table )

so i want the result like

OrderNo OrderDate OrderValue Slno
---------------
Rd001 10/25/2001 $5000 1
Rd105 12/15/2002 $5657 2
Rd441 10/15/2001 $8000 3
Rd543 05/22/2002 $9040 4
Rd333 09/05/2002 $5170 5
Rd662 11/25/2002 $9556 6

How to Get this result. Is it possible with out using StoredProcedures and Temp Tables.

Please Give some sugessions.hii
please try this code in the northwind database, to get an idea

declare @.cmp varchar(100)
declare @.dc int
set @.dc = 0
declare cur_sor cursor for select companyname from customers
set @.dc = @.dc + 1
open cur_sor
fetch next from cur_sor into @.cmp
while @.@.fetch_status = 0
begin
select @.dc as dummycolmn,@.cmp as company
fetch next from cur_sor into @.cmp
set @.dc = @.dc + 1
end

close cur_sor
deallocate cur_sor

regards
ramki

Thursday, February 16, 2012

adding commas into integer

while selecting the field, I want it to be display with commas.
for example,
10000 become 10,000
without decimal placed at the end.
I have tried using smallmoney format but there is decimal places.Take a look at DECIMAL datatype
"dapdap" <dapdap@.discussions.microsoft.com> wrote in message
news:22FBBC46-4B9D-4377-A806-D4D79EE81013@.microsoft.com...
> while selecting the field, I want it to be display with commas.
> for example,
> 10000 become 10,000
> without decimal placed at the end.
> I have tried using smallmoney format but there is decimal places.|||Formatting is controlled by your client application, not by SQL Server. Ask
this question in a forum for the app or programming language you are using.
Certainly you could get SQL to return a string instead of a number but why
would you want to do that with the obvious divantages for performance,
complexity and special handling at the client?
David Portas
SQL Server MVP
--
"dapdap" wrote:

> while selecting the field, I want it to be display with commas.
> for example,
> 10000 become 10,000
> without decimal placed at the end.
> I have tried using smallmoney format but there is decimal places.|||>> selecting the field, I want it to be display with commas. <<
Let's get back to the basics of an RDBMS. Rows are not records; fields
are not columns; tables are not files. In a tiered architecture, the
display formatting is done in the front end and never in the database.
A good SQL programmer avoids proprietary datatypes.

Monday, February 13, 2012

Adding columns to a Matrix report that don't belong to the matrix columns groups

Can we do this?

Adding more columns in a matrix report that don’t

belong to the columns drilldown dimensions…

That is, for example, having the following report:

Product Family

Product

Country City Number of units sold

Then I

would add some ratios, that is, Units Sold/Months (sold per month) and other that

is the average for Product Family (Units Sold/Number of Product Family), for putting an example… some

columns should be precalculated prior to the report so do not get into it, the

real problem I don’t see how to solve is adding one or two columns for showing

these calculated column that doesn’t depend on the column groups but they do

for the rows groups…

Any guidance

on that?

The only

way I am seeing by now is to set it as two different reports, and that is not

what my client wants…


Many

thanks,
Jose

I have found one way of doing this, yet I have yet to implement it... by now I have eliminated the matrix report and only doing a normal one with drilldown at the row level.

The way of doing this, exposed on some articles through the web is to put a higher level group that is unique and there insert the corresponding columns..

Well if anybody knows of a nicer way to do this, please say so...|||

Hi

Saw this and i dont know if this is what your looking for
but it seemes like you may be able to implement this.

Quote Robert Bruckner MSFT :

" You can use the InScope function to distinguish subtotal cells from other cells. Please check the MSDN documentation about the InScope function:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/RSCREATE/htm/rcr_creating_expressions_v1_0jmt.asp
With InScope you can determine the current scope of a matrix cell (e.g. in subtotal or not). You would use an IIF-expression to set the cell expression based on the InScope return values. Note: a matrix cell is "in scope" of column and row groupings, so you need at least two InScope function calls in the case where you have one dynamic row and one dynamic column grouping. E.g.

=iif(InScope("ColumnGroup1"), iif(InScope("RowGroup1"), "In Cell", "In Subtotal of RowGroup1"), iif(InScope("RowGroup1"), "In Subtotal of ColumnGroup1", "In Subtotal of entire matrix"))

Replace "In Cell" with =Sum(Fields!Amount.Value)
Replace "In Subtotal..." with =Avg(Fields!Amount.Value)

However, note that since the subtotal cells share the same cell definition as the group instance cells, adding two "subtotals" (one for "Total", the other for "AVG") at the same level is not supported. One way of solving this is to add a rectangle into the matrix cell and use two textboxes to show the total and the average. Then use conditional visibility on the average textbox to only have it visible for subtotals."

Sorry Robert for quoting you, I couldn't get the thread linked...

G

|||thanks!!

Sorry I already tried to use InScope... I also tried to set up a higher grouping but that only does more confusion and servers for only one more value, when I need three or four... not quite desirable...

Thanks so much for your answer!
Jose

Sunday, February 12, 2012

Adding an int to a datetime type as minutes pleasse help!

Hi,
I'm trying to add an int type to a datetime type to produce a
datatime type that interprets the int as *minutes*, for example:
2008-03-20 15:36:09.920 + 10 = 2008-03-20 15:46:09.920
Problem is though - I'm stuck. Whenever I try to do this SQL Server
2005 interprets the "int" as days and I get the wrong answer. Can
ayone help me please? Any comments/suggestions/code-samples much, much
appreciated.
Thank,
Al.Use the DATEADD function. Implicit DATETIME math is always in number of
days.
SELECT DATEADD(MINUTE, 10, '2008-03-20 15:36:09.920');
While the default is not entirely intuitive, why would you assume that SQL
Server will know that when you typed "10" you meant minutes? What if I did
the same, and expected seconds, and my co-worker expects months? SQL Server
can't be psychic...
A
<almurph@.altavista.com> wrote in message
news:36124fcf-6486-43cc-8297-90972b9d9817@.h11g2000prf.googlegroups.com...
> Hi,
> I'm trying to add an int type to a datetime type to produce a
> datatime type that interprets the int as *minutes*, for example:
> 2008-03-20 15:36:09.920 + 10 = 2008-03-20 15:46:09.920
>
> Problem is though - I'm stuck. Whenever I try to do this SQL Server
> 2005 interprets the "int" as days and I get the wrong answer. Can
> ayone help me please? Any comments/suggestions/code-samples much, much
> appreciated.
> Thank,
> Al.|||On Mar 20, 3:55=A0pm, "Aaron Bertrand [SQL Server MVP]"
<ten...@.dnartreb.noraa> wrote:
> Use the DATEADD function. =A0Implicit DATETIME math is always in number of=
> days.
> SELECT DATEADD(MINUTE, 10, '2008-03-20 15:36:09.920');
> While the default is not entirely intuitive, why would you assume that SQL=
> Server will know that when you typed "10" you meant minutes? =A0What if I =did
> the same, and expected seconds, and my co-worker expects months? =A0SQL Se=rver
> can't be psychic...
> A
> <almu...@.altavista.com> wrote in message
> news:36124fcf-6486-43cc-8297-90972b9d9817@.h11g2000prf.googlegroups.com...
>
> > Hi,
> > I'm trying to add an int type to a datetime type to produce a
> > datatime type that interprets the int as *minutes*, for example:
> > 2008-03-20 15:36:09.920 + 10 =3D 2008-03-20 15:46:09.920
> > Problem is though - I'm stuck. Whenever I try to do this SQL Server
> > 2005 interprets the "int" as days and I get the wrong answer. Can
> > ayone help me please? Any comments/suggestions/code-samples much, much
> > appreciated.
> > Thank,
> > Al.- Hide quoted text -
> - Show quoted text -
Aaron,
Thank you very much.
Al.

Adding an incremental number to select result

Hi,
I will like to have, along with my quesry result, another
column that will store the number of the row, for example:
1 query results...
2 query results...
3 query results...
how can i do this?
Thanks!Refer to following url
HOW TO: Dynamically Number Rows in a Select Statement
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q186133
if you have any unique field in the table then you can try a query something
like this:
Ex:
use northwind
go
select (select count(customerid) from customers where customerid <=a.customerid) rank,
*
from customers a
go
--
-Vishal
"Juan Carlos" <jcarlos_mn@.hotmail.com> wrote in message
news:0bf201c34249$de23a670$a501280a@.phx.gbl...
> Hi,
> I will like to have, along with my quesry result, another
> column that will store the number of the row, for example:
> 1 query results...
> 2 query results...
> 3 query results...
> how can i do this?
> Thanks!|||http://www.aspfaq.com/2427
--
Aaron Bertrand, SQL Server MVP
http://www.aspfaq.com/
Please reply in the newsgroups, but if you absolutely
must reply via e-mail, please take out the TRASH.
"Juan Carlos" <jcarlos_mn@.hotmail.com> wrote in message
news:0bf201c34249$de23a670$a501280a@.phx.gbl...
> Hi,
> I will like to have, along with my quesry result, another
> column that will store the number of the row, for example:
> 1 query results...
> 2 query results...
> 3 query results...
> how can i do this?
> Thanks!

Adding an Image

Hello:

I have an Image control on my report .

I have a Class for example :

public Class Class1

private _MyPic as System.Drawing.Image

public property MyPic as System.Drawing.Image

......

end

end

the Dataset Of My Report is Class1.

i Drop an Image from toolbar in My Report and set Source to Database and set value to =Fields!MyPic.value

befor showing the Report i Create an object from Class1 and fill MyPic and set DataSet of Report to that Object

but i see a Red X instead of Picture

please help me

thanks alot

Setting the image source = Database is correct, but you have to convert the System.Drawing.Image into the actual image data as byte array.

Here is a C# code snippet to convert an image into a byte[] using a MemoryStream:

// save image to byte[]
System.IO.MemoryStream renderedImage = new MemoryStream();
myImage.Save(renderedImage);
renderedImage.Position = 0;
return renderedImage.ToArray();

-- Robert