Separated string into specific temp table colums - sql

I am using a RIGHT(LEFT()) method of stripping a string as each character needs to be put into its own holder so I can access it and use it for a report (each character needs to be in its own box for some reason).
There are 16 characters usually but for space and to save repition I've slimmed down the code.
What I am trying to do is put the separated character value into the corresponding column of the temp table - how is this best achieved?
I have no other use for this data once used I'll destroy it.
Code
CREATE table #StringSeparate
(
col1 varchar(1),
col2 varchar(1),
col3 varchar(1),
col4 varchar(1),
col5 varchar(1),
col6 varchar(1),
col7 varchar(1),
col8 varchar(1),
)
declare #string varchar(16)
set #string = 'tpg22052015-1204'
SELECT
LEFT(#string,1),
RIGHT(LEFT(#string,2),1),
RIGHT(LEFT(#string,3),1),
RIGHT(LEFT(#string,4),1),
RIGHT(LEFT(#string,5),1),
RIGHT(LEFT(#string,6),1),
RIGHT(LEFT(#string,7),1),
RIGHT(LEFT(#string,8),1)
INTO
#String Separate

Just do it like:
CREATE table #StringSeparate
(
col1 varchar(1),
col2 varchar(1),
col3 varchar(1),
col4 varchar(1),
col5 varchar(1),
col6 varchar(1),
col7 varchar(1),
col8 varchar(1),
)
INSERT INTO #StringSeparate
SELECT
LEFT(#string,1),
RIGHT(LEFT(#string,2),1),
RIGHT(LEFT(#string,3),1),
RIGHT(LEFT(#string,4),1),
RIGHT(LEFT(#string,5),1),
RIGHT(LEFT(#string,6),1),
RIGHT(LEFT(#string,7),1),
RIGHT(LEFT(#string,8),1)
Or don't create temp table and do this:
SELECT
LEFT(#string,1) col1,
RIGHT(LEFT(#string,2),1) col2,
RIGHT(LEFT(#string,3),1) col3,
RIGHT(LEFT(#string,4),1) col4,
RIGHT(LEFT(#string,5),1) col5,
RIGHT(LEFT(#string,6),1) col6,
RIGHT(LEFT(#string,7),1) col7,
RIGHT(LEFT(#string,8),1) col8
INTO
#StringSeparate
It will automatically create that temp table, because INTO creates table.

Depending on you RDBMS I suppose I might prefer SUBSTRING:
INSERT INTO #StringSeparate
SELECT
LEFT(#string,1),
SUBSTRING(#string,2,1),
SUBSTRING(#string,3,1),
...
RIGHT(#string,1)

I made a big insert of your statement.
INSERT INTO #StringSeparate
VALUES
((LEFT(#string,1)),
(RIGHT(LEFT(#string,2),1)),
(RIGHT(LEFT(#string,3),1)),
(RIGHT(LEFT(#string,4),1)),
(RIGHT(LEFT(#string,5),1)),
(RIGHT(LEFT(#string,6),1)),
(RIGHT(LEFT(#string,7),1)),
(RIGHT(LEFT(#string,8),1)))

Related

Perform same set of data manipulations on all tables in an SQLite database

I have an SQLite database with tables having a poor schema. Basically, there is no index column which acts like a primary key, so I wish to add a column that has an auto incremental value , to the existing set of rows.
For achieving the above I applied the following SQL commands to a particular table say table_orig. I create a new table named table1 and insert rows from table_orig into it.
create table table1(id integer primary key autoincrement, col1 varchar, col2 varchar, col3 float, col4 float, col5 float, col6 float, col7 float);
insert into table1(col1, col2, col3, col4, col5, col6, col7) select col1, col2, col3, col4, col5, col6, col7 from table_orig;
drop table table_orig;
alter table table1 rename to table_orig;
Now, there are 100 tables in my database and I wish to apply these commands to each of them. How do I automate this, as I don't want to manually do it for each of them?
Any help will be appreciated. Thank you in advance.

Is it possible to create a new table in SQL Server from an existing one and change the column data types?

I have a staging table from which I want to create a new table.
The staging table is all VARCHAR data types and I want to create a new table where I can specify DATE, INT and FLOAT columns.
You can define the columns as-you-go:
select cast(col1 as date) as col1,
cast(col2 as int) as col2
cast(col3 as float) as col3
into new_table
from staging_table;
Alternatively, you can create new_table explicitly:
create table new_table (
col1 date,
col2 int,
col3 float
);
And use insert. I would still use explicit conversions:
insert into new_table (col1, col2, col3)
select cast(col1 as date) as col1,
cast(col2 as int) as col2
cast(col3 as float) as col3
from staging_table;

In SQL,How to pick columns which doesnt have 0 value?

I have pivot query which will result one row of record. I will have to further filter that one row of record. One row of record has hour based columns and so, i have 24 columns for each hour.
How to pick columns which has only values
Lets say we have 5 columns
Col1 Col2 Col3 Col4 Col5
100 0 0 20 0
Col1, Col4 are eligible. I need total these two columns.
Col1 Col4 Total
100 20 120
create table #t
(
Col0 int,
Col1 int,
Col2 int,
Col3 int,
Col4 int,
Col5 int,
Col6 int,
Col7 int,
Col8 int,
Col9 int,
Col10 int,
Col11 int
)
insert into #t values
(0,100,0,0,0,0,20,10,0,0,0,0)
select * from #t
-- Expected Result
select Col1, Col6, Col7 from #t
You can not do that using a static pivot table, dynamic perhaps. It is not known when a pivot table is defined if all the values are null or not and by the time the data is piped in it is too late. You can either create a reporting project that supports omitting empty column groups or look for a more dynamic query alternative.

How to split 4 columns, one row into 2 columns, two rows?

I am using SSRS 2008R2 and SSMS 2008R2 and I am trying to split 4 columns, one row into two rows, 2 columns. How can I do this?
Here is some sample data:
create table #foo
(col1 int, col2 int, col3 int, col4 int)
insert #foo values(1,2,3,4)
insert #foo values(5,6,7,8)
insert #foo values(9,10,11,12)
select * from #foo
But I want to transform this data to look like this:
create table #goo (col1 int, col2 int)
insert #goo values(1,2)
insert #goo values(3,4)
insert #goo values(5,6)
insert #goo values(7,8)
insert #goo values(9,10)
insert #goo values(11,12)
select * from #goo
How can I do this?
As simple as:
create table #foo(col1 int, col2 int, col3 int, col4 int);
insert #foo values(1,2,3,4),(5,6,7,8),(9,10,11,12);
SELECT col1, col2
FROM #foo
UNION ALL
SELECT col3, col4
FROM #foo;
LiveDemo
First 2 columns UNION ALL with 3rd and 4th columns.
If you need to store in #goo use:
SELECT col1, col2
INTO #goo
FROM #foo
UNION ALL
SELECT col3, col4
FROM #foo;
SELECT * FROM #goo;
I imagine it would look something like this
SELECT CONCAT_WS(" ", col1, col2) FROM #foo UNION ALL
SELECT CONCAT_WS(" ", col3, col4) FROM #foo;
Breakdown
concat_ws - Combines two columns with a word separator. In this case a space.
Union All - Merges with another selector to create multiple rows.
SQLFiddle
http://sqlfiddle.com/#!9/ca24df/3/0

T-SQL Stored Procedure [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a script that is currently importing data from one table and inserting in to another table. Now I have to modify the script in such a way that if the value of the field is A then the code must appear in the column 10 else if B then the code should appear in Column 50.
Table 1:
Id Field 1 Field 2 Field 3 Field 4
1 A
2 B
Table 2:
Id Field 1 Field 2 Field 3 Field 4....Field 10.....Field 50
1 0/Null
2 1
Could anyone suggest me a case statement to implement this requirement.
Any advice is greatly appreciated!
Thank You, New Bee
You could use a case expression when selecting the data: http://technet.microsoft.com/en-us/library/ms181765.aspx
On the insert for Field 10 you can use a CASE statement, i.e.
CASE Field2 WHEN 'A' THEN '0\Null' ELSE '' END
and similarly for your Field 50
You can write the T-SQL with case statement which is:
`
Create table #T(
col1 int, col2 varchar(2), col3 int, col4 int, col5 int, col6 int, col7 int, col8 int,
col9 int, col10 int, col11 int)
GO
Create table #T1(
col1 int, col2 varchar(2), col3 int, col4 int, col5 int, col6 int, col7 int, col8 int,
col9 int, col10 int, col11 int, )
Insert into #t
Select 1,'B',5,null,null,null,null,null,null,null,null union all
Select 2,'A',5,null,null,null,null,null,null,null,null union all
Select 112,'B',5,null,null,null,null,null,null,null,null union all
Select 41,'B',5,null,null,null,null,null,null,null,null union all
Select 15,'A',5,null,null,null,null,null,null,null,null
Select * from #t
Insert into #t1
Select col1, col2, col3,col4,col5,col6,col7, col8
col9,CASE WHEN COL2 ='A' Then Col2 Else 'B' End col10, col11
from #t
`