Need To Count And Rank NVARCHAR Column
Apr 2, 2008
Assuming that I have a table similar to this simplified version:
CREATE TABLE [SomeTable] ([Field1] NVARCHAR(50));
How could I count and rank the instances of specific values in the "field1" column? For example, let's say that I want to show the frequency of the column values like so:
field1 count
some value 24
another word 20
a value 5
(and so on...)
How can I do this?
- - - -
- Will -
- - - -
http://www.strohlsitedesign.com
http://blog.strohlsitedesign.com/
http://skins.strohlsitedesign.com/
View 3 Replies
ADVERTISEMENT
May 11, 2006
I have a query that returns a result that looks like this:
amount count
steve 122000 12
jim 145213 13
paul 62325 7
I need to add 2 columns to this query that rank the employees by count and amount with a number, so for example:
amount amount_rank count count_rank
steve 122000 2 12 2
jim 145213 1 13 1
paul 62325 3 7 3
I was going to paste the entire query in but I thought I'd try this first to see if I could get by with a hint. So essentially I need to evaluate with an order by or something and then insert a number as amount_rank and number as count_rank.
Any ideas?
View 3 Replies
View Related
Dec 20, 2007
Hi,All,
I have one table like this
UserID,Name,GameScore
1 A 25
2 B 23
3 C 22
4 D 25
5 E 23
6 F 26
Now i want the query which return like this
Name Score Rank
F 26 1
A 25 2
D 25 2
B 23 3
E 23 3
C 22 4
Can anyone give me the sql 2000 query for this
View 4 Replies
View Related
Jun 30, 2005
If I have a column like this
View 9 Replies
View Related
Nov 8, 2015
I have a table that has the definition from the picture. Let's suppose I will sort the list based on last column (DATA.ORA - DESC). That means the last row inserted is the first one in the sorted list.
What I would like to do is to get only the records when the value of a specific column is different from the row below.
For instance I would like to get the rows where the column WHITE has different values. When the status for column WHITE changes from 0 to 1 or from 1 to 2 etc., then I need that row no matter if next 5 rows has the same status (1).
In the picture you see what the result of query should be:
Result: only the lines where the value of column WHITE is different than the row below.
View 6 Replies
View Related
Jan 14, 2008
Hi all!
I'm trying to script a view that does a simple query to some tables.
The catch is 2 of the columns are created on run time.
The first column is filled with a calculation with values of other columns. The 2nd column I would like to fill with the ranking of this calculated column
Example: (imagine caculatedcol is Sum(Col1+Col2+Col3)
Col1 Col2 Col3 CalculatedCol Ranking
10 10 10 30 1
9 9 9 27 2
8 8 8 24 3
7 7 7 21 4
How can I get the Ranking column filled based on the calculated column?
I'm desperate.
Thanks for any help.
---
Mário Ramos
View 8 Replies
View Related
Jul 13, 2015
I am looking to add a column to one of my tables that displays a running rank of how many times a customer has ordered in a given period.Â
I currently have such a column however this column ranks against ALL of the orders that a customer has placed and ignores filters, whereas I need one that ranks based on the filters that are active at any given time.Â
The current formula is:
CustOrderCountPersistant=RANKX(FILTER('Q1 Data Set',[k1_customer_id]=EARLIER([k1_customer_id])),[order_id],[order_id],1,DENSE)
For example, if I am looking at a full years worth of data and a customer has placed 10 orders in that period this formula will add a 1 in the column for first order, a 2 for the second and so forth all the way to 10, the last order.Â
However it will give me exactly the same results if I filter the data to just one month of that year where they may have order only 2 orders.Â
In this scenario I want to have another column with a table that is filter sensitive and would show 1 for the first order and 2 for the second order.Â
Now, I do understand that the issue here is probably the FILTER() I have on as, if I understand correctly, that means all other filters are ignored. My attempts at reworking the formula to remove this have been unsuccessful (such as using a CALCUALTE and trying to use filter properties within that forumula).
To explain the context - I want to create a measure that counts how many customers have placed x amount orders in y number of days e.g. how many customers have placed 2 orders in 30 days.
View 3 Replies
View Related
Jan 19, 2008
Hi,
My company is starting to use nvarchar columns in our database products. We just found out that, suppose table T1 has a my_nvarchar_col column, and there is a row containing a unicode text say "some Chinese", if you want to select that row, you have to append "N" in front of the unicode constant in the "WHERE" clause. That is:
select * from T1
where my_nvarchar_col = N'some Chinese'
will return that row, while
select * from T1
where my_nvarchar_col = 'some Chinese'
will return NOTHING.
This brings us a huge problem - we have tens of thousands of such queries in our existing PowerBuilder code base. Do we have to go through all of them to add "N" to the "WHERE" clause? Is there a way we can set some attribute of SQL Server so that we do not need to do that?
Project stalled and I am under extreme pressure so please help ASAP!!
View 1 Replies
View Related
Nov 26, 2007
I use SQL 2000
I have a Column named Bool , the value in this Column is 0�0�1�1�1
I no I can use Count() to count this column ,the result would be "5"
but what I need is "2" and "3" and then I will show "2" and "3" in my DataGrid
as the True is 2 and False is 3
the Query will have some limited by a Where Query.. but first i need to know .. how to have 2 result count
could it be done by Count()? please help.
thank you very much
View 5 Replies
View Related
Jan 25, 2007
Users can approach their userprofile on my site using: www.mysite.com/name=peterName is a unique value within my database (db type: nvarchar(50))Now, I have created a clustered index on the username column.However, IMHO its faster to create a clustered index on the (also unique) usercode column since that is of type int.BUT since a user can approach my site based on username I feel that I HAVE to live with this setback in performance....Is that true or is there a better way to solve this issue?
View 1 Replies
View Related
Nov 7, 2005
Hi, I want to add a newline in a content of sql column (using t-sql and not asp.net textbox) so when content is being rendered in a .net textbox I get separate rows, so insteadcol1 col2 col3 I woule like to havecol1col2col3Thanks
View 3 Replies
View Related
Apr 1, 2014
Is it ok/possible to use and * in a column that is NVARCHAR type?
View 4 Replies
View Related
Jul 20, 2005
Hello,This is a simple question, hopefully with a simple answer. I havean nvarchar column of length 255. In one of the rows I have thefollowing sentance - 'See the brown ball bounce'. Is it possible touse a command to remove all of the spaces in that sentance, so thatthe sentance reads 'Seethebrownballbounce'? As you can see, I am notjust interested in getting rid of the trailing and leading spaces.Thanks,Billy
View 1 Replies
View Related
Jun 3, 2007
Hello,
I have a column that is a currently set as nvarchar(50) and is called DateEmployed.
There are over a hundred rows that contain dates which is in nvarchar format.
This column now needs to be changed to a DateTime datatype. (Don't ask me it was not set
to a dateTime when this was first designed - I wasn't here)
However, I have to change this column to a DateTime without destroying the data.
Is there any easy way to write some script or use studio management to change this.
Currently the data is displayed like this in this column dd/MM/yyyy i.e. 25/8/2007.
The method I am using to try and change this is by going to studio management clicking
modify on the column and changing the datatype from a nvarchar(50) to a DateTime.
I get this following error message:
- Unable to modify table.
Arithmetic overflow error converting expression to data type datetime.
The statement has been terminated.
Any suggestions would be most grateful,
Thanks,
Steve
View 1 Replies
View Related
Jun 5, 2015
The source column is varchar(1) but i need this as nvarchar(1).. How can I cast this as NVARCHAR...
View 5 Replies
View Related
Sep 25, 2006
Our database defines the long_value column as nvarchar(max). I want to find out which rows actually contain non-ASCII characters in that column, but this clause also returns rows with only ASCII characters:where long_value like (N'%[' + nchar(128) + N'-' + nchar(65535) + N']%')
What am I doing wrong?
View 5 Replies
View Related
Jul 20, 2007
I am reading data from another data source and storing it in the sqlce database. Some of the string values I'm trying to insert into the database have single quotes in the string (i.e. Johnny's Company). When I try to insert the values with the single quotes, it throws an exception. The code I use to insert the records is as follows:
cmd.CommandText = "INSERT sy_company " +
" (company_id, company, co_name, companyid) " +
"VALUES(" +
"'" + dtSYCompany.Rows[x]["company_id"] + "'," +
"N'" + dtSYCompany.Rows[x]["company"] + "'," +
"N'" + dtSYCompany.Rows[x]["co_name"] + "'," +
"'" + dtSYCompany.Rows[x]["companyid"] + "')";
cmd.ExecuteNonQuery();
When the company name (co_name) has a single quote in it, I get the error. How do I write the insert statement so it will work even though the value being inserted into co_name has a single quote in it?
Thanks so much!
View 3 Replies
View Related
Aug 31, 2007
A SqlServer 2005 db has a table with an nvarchar(max) column containing text with paragraph format. When displayed in a Windows form textbox each paragraph exhibits CRLF. When the table is opened in Management Studio the paragraph text is separated with two boxes at each CRLF.
I would like to insert an additional CRLF (or whatever is required) so that when viewed in a textbox each paragraph has a blank line separating it from the next paragraph. Much easier to read!!!
Can this be done? If so, how? Thanks in advance for any help you can provide.
View 11 Replies
View Related
Mar 28, 2007
helo..
I want to save file it size about 200MB to the database and after that
I want to get it from the database, the colmun type that i want to
save the file to it is NVARCHAR(Max).
I am using MS sql server 2005.
I was using this code to do this with Image Or Ntext column type but
it dose not working with NVARCHAR(Max) column?
Function SaveFileToDB(ByVal FileName As String, ByVal TblName As String, ByVal FldName As String, ByVal ColumnIDName As String, ByVal RowID As String) As Boolean
Try
Dim addEmp As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT @Pointer = TEXTPTR(" & FldName & ") FROM " & TblName & " WHERE " & ColumnIDName & " = '" & RowID & "'", MyConnection)
Dim trParm As SqlClient.SqlParameter = addEmp.Parameters.Add("@Pointer", SqlDbType.Binary, 16)
trParm.Direction = ParameterDirection.Output
addEmp.ExecuteNonQuery()
'''''''''''''''''''''''''''''''''''''
Dim bufferLen As Integer = 1048576
Dim appendToPhoto As SqlClient.SqlCommand = New SqlClient.SqlCommand("UPDATETEXT " & TblName & "." & FldName & " @Pointer @Offset 0 @Bytes", MyConnection)
Dim ptrParm As SqlClient.SqlParameter = appendToPhoto.Parameters.Add("@Pointer", SqlDbType.Binary, 16)
ptrParm.Value = trParm.Value
Dim photoParm As SqlClient.SqlParameter = appendToPhoto.Parameters.Add("@Bytes", SqlDbType.Image, bufferLen)
Dim offsetParm As SqlClient.SqlParameter = appendToPhoto.Parameters.Add("@Offset", SqlDbType.Int)
offsetParm.Value = 0
Dim fs As IO.FileStream = New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)
Dim br As IO.BinaryReader = New IO.BinaryReader(fs)
Dim buffer() As Byte = br.ReadBytes(bufferLen)
Dim offset_ctr As Long = 0
Do While buffer.Length > 0
photoParm.Value = buffer
appendToPhoto.ExecuteNonQuery()
offset_ctr += bufferLen
offsetParm.Value = offset_ctr
buffer = br.ReadBytes(bufferLen)
My.Application.DoEvents()
Loop
br.Close()
fs.Close()
Return True
Catch ex As Exception
MyErrStr = ex.Message
Return False
End Try
End Function
Public Function ReadFileFromDB(ByVal MyCommandText As String, ByVal FileColumnNumber As Integer, ByVal DSTFileName As String) As Boolean
Try
Dim command As SqlClient.SqlCommand = New SqlClient.SqlCommand(MyCommandText, MyConnection)
Dim stream As IO.FileStream
Dim writer As IO.BinaryWriter
Dim bufferSize As Integer = 1048576
Dim outByte(bufferSize - 1) As Byte
Dim retval As Long
Dim startIndex As Long = 0
Dim reader As SqlClient.SqlDataReader = command.ExecuteReader()
reader.Read()
stream = New IO.FileStream(DSTFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
writer = New IO.BinaryWriter(stream)
startIndex = 0
retval = reader.GetBytes(FileColumnNumber, startIndex, outByte, 0, bufferSize)
Do While retval = bufferSize
writer.Write(outByte)
writer.Flush()
startIndex += bufferSize
retval = reader.GetBytes(FileColumnNumber, startIndex, outByte, 0, bufferSize)
My.Application.DoEvents()
Loop
writer.Write(outByte, 0, retval - 1)
writer.Flush()
writer.Close()
stream.Close()
reader.Close()
Return True
Catch ex As Exception
MyErrStr = ex.Message
Return False
End Try
End Function
View 6 Replies
View Related
Oct 8, 2007
I need to always have a formatting of 999,999,999.00 in a column called PropertyMap9.
PropertyMap9 is always a nvarchar(max)
How do I SET a value using a CONVERT or CAST to accomplish this ?
Why does this give me a syntax error ? The PropertyMap7 set actually works and performs the calculation and sets the new value for that row, however the next line gives an error. I need all 3 columns PropertyMap9, 8, and 7 to always have the above formatting, while still maintaining the nvarchar(max) datatype in the column. HELP!
update PropertyMapValues
set PropertyMap7 = CONVERT(NVARCHAR(MAX),CAST(PropertyMap8 AS money) - (CAST(PropertyMap9 AS money)),1)
set PropertyMap9 = CONVERT(NVARCHAR(MAX),CAST(PropertyMap9 AS money))
END
Msg 102, Level 15, State 1, Procedure UpdatePropertyMap7, Line 59
Incorrect syntax near '='.
View 1 Replies
View Related
Feb 1, 2007
Hi,
i have a table with a nvarchar column,i want to send this column value as unicode content to customer mail box , but when i send it a mail with '?' customer receive , how can i accomplish this?
thanks
View 6 Replies
View Related
Jun 21, 2004
Hey folks, the question is fairly simple, unfortunately the answer has proven rather elusive.
Is it possible to declare a variable which would then be used to identify either a column or table in an SQL statement?
Here's a basic idea of what I'd like to do:
DECLARE @myVar AS NVARCHAR(50)
SELECT *
FROM @myVar
or
DECLARE @myVar AS NVARCHAR(50)
SELECT @myVar
FROM MyTable
I'm probably looking for some sort of built in function that will accept an argument here... like COLUMN(@myVar) or something of the like. I just don't know where to look...
View 1 Replies
View Related
Dec 9, 2014
We have an address table with a house_num field which is a nvarchar.
Most of house numbers are numbers like 1234, 0989
But some of them have a letter behind it like 678 B, 8909 F, even some like this 123/B
We would like to remove any non-numeric letter for this column.
Is there a way to do it?
View 4 Replies
View Related
May 2, 2007
What is the best way of using the Full-Text feature on HTML?
I want to only search the text and omit the html tags.
If that involves storing as a different format, can someone tell me the best way of doing that?
I'm very new to sql and especially full-text.
Thanks.
View 1 Replies
View Related
May 26, 2015
I would like to know the possibility of creating Measure based on the below data.
I don't need to show any aggregate value. I need to show the value as it is.
View 6 Replies
View Related
Sep 7, 2007
Is it possible to store a carriage return value or VbCrLf in an NVARCHAR column ?
I need to display a textbox in Reporting Services using one row, but showing multiple lines as a result of a few carriage returns.
View 3 Replies
View Related
Jan 23, 2007
Hi,
I have a webform with a drop-down listbox, binded to a table and a GridView, binded to another table. My goal is when I select a value (country in my case) in the drop-down listbox to change what is shown in the GridView, based on the ID that I get from the drop-down listbox.
Here is the code (the important parts...):
Select a Country : <asp:DropDownList ID="CountriesDropDownList" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="CountryID" Width="220px" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [CountryID], [Name] FROM [DS_Country]">
</asp:SqlDataSource>
<asp:GridView ID="ProvincesGridView" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProvinceID,CountryID,Name,ShortName,Description" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" HorizontalAlign="Left" >
<Columns>
..............................
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DineSelectConnectionString %>"
DeleteCommand="DELETE FROM DS_Province WHERE ProvinceID=@ProvinceID"
SelectCommand="SELECT ProvinceID,CountryID,Name,ShortName,Description FROM DS_Province WHERE CountryID=@ddlCountryID"
UpdateCommand="UPDATE DS_Province SET CountryID=@CountryID,Name=@Name,ShortName=@ShortName,Description=@Description WHERE ProvinceID=@ddlCountryID"
InsertCommand="INSERT INTO DS_Province (CountryID,Name,ShortName,Description) VALUES (@CountryID,@Name,@ShortName,@Description)">
<SelectParameters>
<asp:ControlParameter Name="ddlCountryID" ControlID="CountriesDropDownList" PropertyName="DataValueField" />
</SelectParameters>
</asp:SqlDataSource>
When I run it I get this error "Syntax error converting the nvarchar value 'CountryID' to a column of data type int.". If I specify the Type="Int" in <asp:ControlParameter>..</> I get "Input string was not in a correct format. "
This is supposed to be easy, but I can't make it work ! Any suggestions will be more than welcome.
Thanks.
View 10 Replies
View Related
Aug 19, 2015
I am trying to convert a column (nvarchar) to date time.
Here is an example of some values:
20110905
20110901
20111003
As expected, I received the following error: 'Conversion failed when converting date and/or time from character string.'
However, I am unsure how I should approach updating the whole column. I would like the format to be '103'.
View 9 Replies
View Related
Aug 21, 2007
Hi,
I have a problem with BULK INSERT. I created the following table:
Code Snippetcreate table Test
(id char(4), name nvarchar(16), last char(1))
I am trying to bulk insert data from ASCII (not unicode) file with only two rows:
0011First name
0018Second name
Since it is a fixed length file, I am using the following format file:
Code Snippet
8.0
3
1 SQLCHAR 0 4 "" 1 ID HEBREW_CI_AS
2 SQLCHAR 0 16 "" 2 NAME HEBREW_CI_AS
3 SQLCHAR 0 0 "
" 3 Last HEBREW_CI_AS
With bcp utility everything works just fine!
Code Snippet
bcp Demo.dbo.test in c: est -T -f c: est.fmt
But when I use BULK INSERT in the following form:
Code Snippet
BULK INSERT Test FROM 'c:Test'
WITH
(
FORMATFILE='c:Test.fmt',
CODEPAGE='OEM'
);
I am getting error
Server: Msg 4863, Level 16, State 1, Line 1
Bulk insert data conversion error (truncation) for row 1, column 2 (name).
Now, one interesting thing: if I change the name field from nvarchar to varchar, it is working with BULK INSERT as well.
Can anybody explain what is going on here?
I am using MS SQL 2000 and MSDE
Thanks in advance,
Eugene.
View 2 Replies
View Related
Jul 7, 2007
HiAm Using ASP.NET With SQL SERVER 2005 Backend
AGENT CODE
3008000003
NAME
agent code dropdownlist values like 1005000006,2009000002,3008000003select dropdownlist value it display corresponding values related to that codewhen i select first 2 values its run properly,But when i select 3008000003 i will get following error messagein SQL SERVER 2005 Agent Code Date Type is "bigint"" The conversion of the nvarchar value '3008000003' overflowed an int
column. Maximum integer value exceeded. Description:
An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error and
where it originated in the code. Exception Details:
System.Data.SqlClient.SqlException: The conversion of the nvarchar value
'3008000003' overflowed an int column. Maximum integer value
exceeded.Source Error:
An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.
Stack Trace: Please Help me to solve this issueThanks With RegardsS.Senthil Nathan
View 4 Replies
View Related
Sep 24, 2015
I am trying to sort my sql resultset by an alphanumeric column of a table which is of NVARCHAR datatype. The sample data is given below:
CREATE TABLE #Activities(activityName NVARCHAR(100))
INSERT INTO #Activities VALUES('Field phase S14-04932-01')
INSERT INTO #Activities VALUES('Phase reporting')
INSERT INTO #Activities VALUES('Phase running')
INSERT INTO #Activities VALUES('RD1')
[Code] ....
The output of the query is like this:
A1
A2
A3
A4
E1 0DAA1
E10
E2 0DAA2
[Code] .....
The output what I require is this:
A1
A2
A3
A4
E1 0DAA1
E2 0DAA2
[Code] ....
View 9 Replies
View Related
Dec 31, 2007
I've looked through msdn and found an example, but it doesn't work. The problem is that when i use such construction
ResultSet rs1 = stmt.executeQuery("SELECT TOP 1 * FROM sys.check_constraints");
rs1.next();
Reader reader = rs1.getCharacterStream(17);
reader is set to null,but it mustn't be null cause i see some data through management studio in 17th column of sys.check_constraints table.
View 3 Replies
View Related
Jul 30, 2015
I'm currently loading a package that does a lookup on a column of data type nvarchar(4).The values itself are (A+, A, B+, B, C, D, /). The strange lookup behaviour is happening for each of the cases, so it's not related to a specific value. After trying to put the cache on NO CACHE, the lookup works perfectly. When using the default FULL CACHE the strange behaviour happens. Could it be related to the data type? I have not yet tried to use a CHAR instead of a NVARCHAR but it looks like people have similar issues using CHAR.
View 2 Replies
View Related