0

I need to convert this table

id | fieldname | length | fieldtext
---|-----------|--------|--------------
1  | field1    |   12   | sadsadasdfaf
2  | field1    |   10   | asdfasdasd
3  | field1    |   14   | afgsdfgsdgssdf
4  | field1    |   15   | asdfafaaasdfasf
5  | field2    |   10   | afgsagsaga
6  | field2    |   4    | asdf
7  | field2    |   12   | sadfasfsdafg
8  | field3    |   12   | asdsadfgsdas
9  | field3    |   5    | xaadd 

to this table

id |fieldname | s_text        | m_text       | l_text         | vl_text
---|----------|---------------|--------------|----------------|----------
1  | field1   | asdfasdasd    | sadsadasdfaf | afgsdfgsdgssdf | asdfafaaasdfasf
2  | field2   | NULL          | asdf         | afgsagsaga     | sadfasfsdafg  
3  | field3   | NULL          | NULL         | xaadd          | asdsadfgsdas  

The new text columns are filled with the fieldtext from the original table, according to the length of the field. The longest text should go to vl_text (verylarge_text), the smallest text should go to s_text (small text).

If there are < 4 texts, the smaller textcolumns will be set to NULL.

I understand that I can use the pivot function but all examples I looked at used the text in the fields for the column name. Is there any way to create a SELECT statement that will give me the desired result? Is this even possible in SQL or do I have to write a small program to achieve this?

I am using SQL Server 2008.

Original table schema:

CREATE TABLE [dbo].[texttable](
    [id] [int] NOT NULL,
    [fieldname] [varchar](50) NOT NULL,
    [length] [int] NOT NULL,
    [fieldtext] [varchar](50) NOT NULL
) 

INSERT INTO [dbo].[texttable] (id, fieldname, length, fieldtext) values (1, 'field1', 12, 'sadsadasdfaf');
INSERT INTO [dbo].[texttable] (id, fieldname, length, fieldtext) values (2, 'field1', 10, 'asdfasdasd');
INSERT INTO [dbo].[texttable] (id, fieldname, length, fieldtext) values (3, 'field1', 14, 'afgsdfgsdgssdf');
INSERT INTO [dbo].[texttable] (id, fieldname, length, fieldtext) values (4, 'field1', 15, 'asdfafaaasdfasf');

INSERT INTO [dbo].[texttable] (id, fieldname, length, fieldtext) values (5, 'field2', 10, 'afgsagsaga');
INSERT INTO [dbo].[texttable] (id, fieldname, length, fieldtext) values (6, 'field2', 4, 'asdf');
INSERT INTO [dbo].[texttable] (id, fieldname, length, fieldtext) values (7, 'field2', 12, 'sadfasfsdafg');

INSERT INTO [dbo].[texttable] (id, fieldname, length, fieldtext) values (8, 'field3', 12, 'asdsadfgsdas');
INSERT INTO [dbo].[texttable] (id, fieldname, length, fieldtext) values (9, 'field3', 5, 'xaadd');

New table schema:

CREATE TABLE [dbo].[newtexttable](
   [id] [int] NOT NULL,
   [fieldname] [varchar](50) NOT NULL,
   [s_text] [varchar](50),
   [m_text] [varchar](50) ,
   [l_text] [varchar](50) ,
   [vl_text] [varchar](50) 
)

INSERT INTO [dbo].[newtexttable] (id, fieldname, s_text, m_text, l_text, vl_text) values (1, 'field1', 'asdfasdasd', 'sadsadasdfaf', 'afgsdfgsdgssdf', 'asdfafaaasdfasf');
INSERT INTO [dbo].[newtexttable] (id, fieldname, s_text, m_text, l_text, vl_text) values (2, 'field2', NULL, 'asdf', 'afgsagsaga','sadfasfsdafg');
INSERT INTO [dbo].[newtexttable] (id, fieldname, s_text, m_text, l_text, vl_text) values (3, 'field3', NULL, NULL, 'xaadd', 'asdsadfgsdas');
s3b
  • 158
  • 1
  • 10
  • what is the criteria for putting a value into s_text, m_text etc ? – Erran Morad Mar 19 '14 at 17:28
  • I edited my post. "The longest text should go to vl_text (verylarge_text), the smallest text should go to s_text (small text)." – s3b Mar 19 '14 at 17:44

0 Answers0