Sql Server select a single row as columns for sample data - sql-server

I'd like to be able to select a row of data but have the first column of output be column name and the second column of output be the value of that column for the selected row. How might I do this in SQL Server 2005 SSMS?

Use the PIVOT operator.

Related

Automatically produce aliases for all columns in SELECT statement

I'm using Microsoft Query to pull data from MS SQL Server to Excel. Many of my tables have the same column names, for example:
[task].[active]
[user].[active]
[task].[name]
[user].[name]
When I pivot in Excel, only the column names are shown. A pivot filter might have multiple fields called "active" which is very confusing.
I'd like to alias every column with the table name it's from, so that in the filter it would say "task_active" and "user_active". My Excel SELECT statement would be:
SELECT active AS task_active, name AS task_name FROM task...
Is there a quick way to prepend the table name to an alias using a formatting tool? I have Apex SQL Refactor, and Notepad++ but I haven't found a way to do this without having to manually type all of the column names again.
If you populate resultset to datatable then datatable to excel then it will automatically change duplicate column name to col1,col2 etc.
This is not your requirement.you want it to be specific.
Method 1 . Create temp table with desire column name
Insert the result in #temp table
Return #temp table result set
Method 2 : Use dynamic query.
Wht your real query look like ?

SSRS HEADER CHANGE (Change column name)

I have the following table in which I have already calculated the 95 percentile in SQL SERVER 2012.I want to produce the report in SSRS 2008R2. But when I place [95th%ile] in SSRS table column, it change the header to [ID95th ile]. Is there a way I can change it to [95th%ile] as this is what is in the current excel report and I have been told not to change it to any other name
Below is sample data that I am using.
CREATE TABLE ##PerCent (month Varchar(10), Percentile INT)
INSERT INTO ##PerCent
VALUES
('Jan',153),
('Feb',171),
('Mar',141)
SELECT
month,
Percentile AS '95th%ile'
FROM ##PerCent
You cannot add a field which name starts with numbers or any character different to a letter.
However you can change the name in the tablix header if you are presenting the dataset in that component.
Let me know if this helps.

How to show data in column in SSRS

I'm using SSRS for my reporting, my reporting solution is in Visual Studio 2008 Business Intelligence Development Studio.
I have a report in which the data should be displayed in this format.
I have added a Column Group in my table which is having the values of Customer Name and details, the data is coming fine in the vertical format i.e column after column.
My Issue :
There should be only three columns in each row, after three records the next row should begin and again not more than three records should be displayed as shown in the image above.
My attempts :
I tried to add a row group and in that gave the expression
= Ceiling(Fields!Row_Count.Value/3)
here Row_Count is a field which is coming from my query which holds the serial number of the records.
My SQl Query
SELECT Row_Number() over(order by table_ID) AS Row_Count, Field_1,Field_2 from MyTable
In my Column group i have Customer Name and in my Row Group i have other details of the customer. The data is getting populated column wise but the issue is its not breaking the current row after three records.
Below is my table of report.
You were on the right track. Say you have data like this:
I have created a tablix like this:
The Row Group expression is:
=Ceiling(Fields!Row_Count.Value / 3)
This works together with the Column Group expression to split over three columns:
=(Fields!Row_Count.Value - 1) Mod 3
The other thing to note compared to your tablix is that CustomerName is not in a table header row, but rather there are two row header rows, one for CustomerName and one for Details.
This is looking OK to me, obviously you can format to taste:

sybase/isql 15.7 : query returns more characters than exists in table

table contains varchar(30) field
query: select accID
Using isql/12.5, same query, same Sybase 15 database: isql query returns rows with 30 column field.
Using isql/15.7, same query, same Sybase 15 database: isql query returns rows with 60 column field.
Is there some sort of configuration issue here?
Thanks.
Lori
How the values are displayed via isql does not affect the datatype of the individual fields, nor the value of the fields. Just because isql has padded the column with extra characters, it does not change the value of the field, only the way it displays on the screen. Isql pads fields for readability.

I want to fetch autoincremented varchar values from SQL Server 2008

I want to fetch autoincremented varchar values from SQL Server 2008 into a textbox in asp.net in pageload event.
Suppose I have a column trn_no, data type varchar. If the 1st value inserted manually in the table, like T100, then how would I get the value T101 in the textbox in pageload.
please help me.
If you want to have alphanumeric values in key then use following sql to get last inserted id +1.
SELECT 'T'
+ Cast(Max(Substring(trn_no, 2, Len(trn_no)-1) + 1 ) AS varchar(15))
FROM table
Change Varchar(15) as per possible max length

Resources