Tuesday, February 14, 2012

Breaking column data

DBA's
How do I break a column data into 2 or more columns.
Example, I have a 7 character column that I want to break into 3 columns of 3,2 and 2 character respectively. Let me know how the query would look like.
ThanksYou could use left(), substring() functions:

select left(column,2), substring(column,3,2),...|||Don't forget RIGHT

USE Northwind
GO
CREATE TABLE myTable99 (Col1 char(7))
GO

INSERT INTO myTable99(Col1)
SELECT '1234567' UNION ALL
SELECT 'abcdefg' UNION ALL
SELECT 'qwertyu' UNION ALL
SELECT 'lkjhgfd' UNION ALL
SELECT '1212312'

SELECT LEFT(Col1,2) AS [Left]
, SUBSTRING(Col1,3,3) AS Center
, RIGHT(Col1,2) AS [Right]
FROM myTable99
GO

DROP TABLE myTable99
GO|||Don't forget about your bol - check out string functions.

No comments:

Post a Comment