Converting data for whole table SQL - sql

I'm newbie in SQL and have some questions:
How can I convert columns (in my table with more than 10 000 rows) in my SQL table (I'm using SQL Server 2008):
First column is nvarchar (50) and containing different string values, for e.g. like 20131211142319 and it's a date and time - 2013/12/11 14:23:19. How can I convert this value into date & time and affect this on all rows in this column (more than 10 000).
And also I have column with numbers, all this numbers start from # + number - e.g. #8339274. How can I delete character "#" before all numbers in all rows? Note, that numbers in this column have a different length, from 5 characters to 15 characters.
Thank you in advance.

I couldn't find a more elegant solution for the datetime conversion but here you go:
1. DATETIME conversion
This assumes your value is always in the same format you specified:
Example code for you to run
DECLARE #Value VARCHAR(255) = '20131211142319'
SELECT CONVERT(DATETIME,LEFT(#Value,8) + SPACE(1) + STUFF(STUFF(STUFF(RIGHT(#Value,6), 1, 0, REPLICATE('0', 0)),3,0,':'),6,0,':'))
This splits the field into two sections, the DATE portion LEFT(#Value,8) and then the TIME
STUFF(STUFF(STUFF(RIGHT(#Value,6), 1, 0, REPLICATE('0', 0)),3,0,':'),6,0,':'). The TIME portion is essentially just adding in the colon where applicable (see STUFF on MSDN)so that it returns a value such as:
20131211 14:23:19 This makes it applicable to directly convert to a DATETIME.
2. Removing the # from the numbers
Example code for you to run
DECLARE #ValueNumber VARCHAR(255) = '#8339274'
SELECT SUBSTRING(#ValueNumber,2,LEN(#ValueNumber))
The above statement will take your number and only return data from the 2nd value onwards, excluding the #. See SUBSTRING on MSDN
To make this run on your table, just replace my variable names in the SELECT statement with your column names.
Example using the above on columns in a table:
SELECT CONVERT(DATETIME,LEFT([DATECOLUMNNAME],8) +
SPACE(1) + STUFF(STUFF(STUFF(RIGHT([DATECOLUMNNAME],6),
1, 0,REPLICATE('0',0)),3,0,':'),6,0,':')) AS [Date],
SUBSTRING([NUMBERCOLUMNNAME],2,LEN([NUMBERCOLUMNNAME])) AS [Number]
FROM [TABLENAME]
Replace [DATECOLUMNNAME] with the name of the column that holds your datetime value. Replace the [NUMBERCOLUMNNAME] with the name of the column that holds your number with the #.
Then finally replace [TABLENAME] with your table name that contains those columns.

try this : below answer is also correct
declare #a nvarchar(50)
set #a='20131211142319'
select cast(left(#a,4)+'/'+substring(#a,5,2)+'/'+substring(#a,7,2)+ ' '+ substring(#a,9,2)+':'+substring(#a,11,2)+':' +right(#a,2) as datetime)
output's this --2013-12-11 14:23:19.000
declare #a nvarchar(10)
set #a='#1234567'
select replace(#a,'#','')
outputs this--1234567

Related

How can we read a varchar column, take the integer part out and add new column incrementing that integer part using script

I need to write a SCRIPT for below scenario:
We have a column X with rows value for this column X as X01,X02,X03,X04........
The problem I am stuck with is that I needed to add another row to this table based on the value of the last row that is X04, Well I am able to identify the logic that I need to work which is given below:
I need to read value X04
Take the integer part 04
Increment by 1 => 05
Save column value as X05
I am able to pass with the 1st step which is not very hard. The problem that I am facing is the next steps. I have researched and tried quite a lot commands but none worked.
Any help is highly appreciated. Thanks.
You seem to be describing:
select concat(left(max(x), 1),
right(concat('00', try_convert(int, right(max(x), 2)) + 1), 2)
from t;
This is doing the following:
Taking the left most character.
Converting the two right characters to a number and adding one.
Converting that back to a zero-padded string.
Here is a db<>fiddle.
Now: That you want to increment a string value seems broken. You should just use an identity column or sequence to assign a number. You can format the value as a string when you query the table -- or use a computed column to store that.
Try below Script
CREATE TABLE #table (x varchar(20))
INSERT INTO #table VALUES('X01'),('X02'),('X03'),('X04')
DECLARE #maxno NVARCHAR(20)
DECLARE #maxstring NVARCHAR(20)
DECLARE #finalno NVARCHAR(20)
DECLARE #loopminno INT =1 -- you can change based on the requirement
DECLARE #loopmaxno INT =10 -- how many number we want to increment
WHILE #loopminno < #loopmaxno
BEGIN
select #maxno = MAX(CAST(SUBSTRING(x, PATINDEX('%[0-9]%', x), 100) as INT))
, #maxstring = MAX(SUBSTRING(x, 1, PATINDEX('%[0-9]%',x)-1))
from #table
where PATINDEX('%[1-9]%',x)>0
SELECT #finalno = #maxstring + CASE WHEN CAST(#maxno AS INT)<9 THEN '0' ELSE '' END + CAST(#maxno+1 AS VARCHAR(20))
INSERT INTO #table
SELECT #finalno
SET #loopminno = #loopminno+1
END

SQL Server - Dynamically Filling the column Values with 0's as per the fixed length

I have a column with the given values
MRN
1946
456
27
557
The column values length is fixed.
If at all any value is less than 6characters,then it should concate 0's to the left and make it 6characters length.
The desired output is
MRN
001946
000456
000027
000557
This is called left paddings. In SQL Server, this is typically done with more basic string operations:
select right(replicate('0', 6) + mrn, 6)
If mrn is a number, then use the concat() function:
select right(concat(replicate('0', 6), mrn), 6)
You can also use the FORMAT function for this. (Demo)
SELECT FORMAT(MRN ,'D6')
FROM YourTable
Change the number 6 to whatever your total length needs to be:
SELECT REPLICATE('0',6-LEN(EmployeeId)) + EmployeeId
If the column is an INT, you can use RTRIM to implicitly convert it to a VARCHAR
SELECT REPLICATE('0',6-LEN(RTRIM(EmployeeId))) + RTRIM(EmployeeId)
And the code to remove these 0s and get back the 'real' number:
SELECT RIGHT(EmployeeId,(LEN(EmployeeId) - PATINDEX('%[^0]%',EmployeeId)) + 1)
We can achieve this by adding leading zero's
select RIGHT('0000'+CAST(MRN AS VARCHAR(10)),6)

Need to pad zeros left and right for a string value according to decimal format

So if I have a data (varchar) like say 10.1
I need the value as 0000101000000.
means (000010) whole number and (1000000) decimal value.
Its a 13 character string ,numbers coming before decimal point should be in first 6 characters and numbers coming after decimal point should be in last 7 characters
Maybe..?
DECLARE #d decimal(13,7) = 10.1;
SELECT RIGHT('0000000000000' + CONVERT(varchar(13),CONVERT(bigint,(#d * 10000000))),13);
Using my crystal ball here though.
Edit: As, for some reason, the OP is storing a decimal as a varchar (this is a really bad bad idea on it's own), I have added further logic to attempt to convert the value to a decimal first.
As experience has taught many of us, give a user a non-numeric column to store a numeric value in and they're more than happily store a non-numeric value in it, so i have used TRY_CONVERT and assumed you are using SQL Server 2012+:
DECLARE #d varchar(13) = 10.1;
SELECT RIGHT('0000000000000' + CONVERT(varchar(13),CONVERT(bigint,(TRY_CONVERT(decimal(13,7),#d) * 10000000))),13);
SELECT REPLICATE('0',6-LEN(SUBSTRING(CAST([data] AS VARCHAR), 1,
CHARINDEX('.',CAST([data] AS VARCHAR)) -1)))+SUBSTRING(CAST([data] AS VARCHAR), 1,
CHARINDEX('.',CAST([data] AS VARCHAR)) -1)+
SUBSTRING(CAST([data] AS VARCHAR), CHARINDEX('.',CAST([data] AS VARCHAR)) + 1,
LEN(CAST([data] AS VARCHAR)))+REPLICATE('0',7-LEN(SUBSTRING(CAST([data] AS VARCHAR), CHARINDEX('.',CAST([data] AS VARCHAR)) + 1,
LEN(CAST([data] AS VARCHAR))))) AS Whole
FROM Table1
Output
Whole
0000101000000
Demo
http://sqlfiddle.com/#!18/8649d/16
You can use some math and string operations to do it like below
see live demo
declare #var decimal(10,4)
set #var=10.1
select #var,
right(cast(cast(( floor(#var)+ power(10,7)) as int) as varchar(13)),6)
+
cast(cast(((#var- floor(#var)) * power(10,7)) as int) as varchar(13))
There's a fair amount of string manipulation to be done here. I'll step through what I did.
I used a variable for the base number so I could verify different results:
declare #n decimal(9,3) = 10.1
You need 6 spaces left of the decimal and 7 spaces to the right, so I'm doing all the manipulation on a VARCHAR(13). I didn't create a new variable as a VARCHAR because I'm assuming you want to be able to do this conversion in line on the fly, so I'm using that CAST over and over again.
Start by finding the decimal place.
SELECT CHARINDEX('.',CAST(#n as VARCHAR(13)))
In the sample number, that's a 3, but it could obviously change.
Now, get the portion of the number to the left of the decimal place.
SELECT SUBSTRING(CAST(#n as VARCHAR(13)),1,CHARINDEX('.',CAST(#n as VARCHAR(13)))-1)
Then get the portion to the right of the decimal.
SELECT SUBSTRING(CAST(#n as VARCHAR(13)),CHARINDEX('.',CAST(#n as VARCHAR(13)))+1,LEN(CAST(#n as VARCHAR(13))))
Pad the leading zeroes. Put 6 on, concatenate, and take a RIGHT 6. Accounts for no digits to the left of the decimal.
SELECT RIGHT(REPLICATE(0,6) + SUBSTRING(CAST(#n as VARCHAR(13)),1,CHARINDEX('.',CAST(#n as VARCHAR(13)))-1), 6)
Pad the trailing zeroes. Same idea, but in the other direction.
SELECT LEFT(SUBSTRING(CAST(#n as VARCHAR(13)),CHARINDEX('.',CAST(#n as VARCHAR(13)))+1,LEN(CAST(#n as VARCHAR(13)))) + REPLICATE(0,7),7)
Then put it all together.
SELECT RIGHT(REPLICATE(0,6) + SUBSTRING(CAST(#n as VARCHAR(13)),1,CHARINDEX('.',CAST(#n as VARCHAR(13)))-1), 6)
+
LEFT(SUBSTRING(CAST(#n as VARCHAR(13)),CHARINDEX('.',CAST(#n as VARCHAR(13)))+1,LEN(CAST(#n as VARCHAR(13)))) + REPLICATE(0,7),7)
Results.
0000101000000
declare #var varchar(20) = '10000.112'
SELECT FORMAT (FLOOR(#var), '000000') + left((PARSENAME(#var,1)) + replicate('0',7),7)

Netezza convert char to numeric - arithmetic - and convert again to char

I have a field PersonNumb which is varchar(30) and needs to get converted to numeric, than do some arithmetic(in this case some simple addition Value + 10, just for example) and than convert this field again to varchar.
I do cut off the '|' to convert the field to number, without blanks or other characters, everything fine.
to_number(translate(PersonNumb,'|',''),999999999999999999999999999999) AS NewPersonNumb;
Than i do the arithmetic with
update XX..YY set NewPersonNumb = NewPersonNumb + 10;
But the last step wont work, the field is still numeric and not varchar.
update XX..YY set NewPersonNumb = to_char(NewPersonNumb,'999999999999999999999999999999');
Put the whole statement in one row doesnt work too...
update XX..YY set NewPersonNumb = to_char(NewPersonNumb + 10,'99999999999999999999999999');
I have done the following its working for me .
TEST.ADMIN(ADMIN)=> create table YY (PersonNumb varchar(30)) distribute on random;
CREATE TABLE
TEST.ADMIN(ADMIN)=> insert into YY values('22222222|111111|4');
INSERT 0 1
TEST.ADMIN(ADMIN)=> update yy set PersonNumb = to_number(translate(PersonNumb,'|',''),999999999999999999999999999999)+10 ;
UPDATE 1
TEST.ADMIN(ADMIN)=> select * from YY;
PERSONNUMB
-----------------
222222221111124
Perhaps someone will need it
Put everything in the select
(to_number(translate(PersonNumb,'|',''),99999999999999999999999999999) + 10)::varchar(30) AS NewPersonNumb

How to find repeating numbers in a column in SQL server . Eg 11111, 33333333, 5555555555,7777777 etc

I need to identify repeated numbers( Eg: 1111, 33333333, 5555555555,777777777 etc.) in a column.
How can I do this in sql server without having to hard code every scenario. The max length is 10 of the column. Any help is appreciated.
This will check if the column has all the same value in it.
SELECT *
FROM tablename
WHERE columnname = REPLICATE(LEFT(columnname,1),LEN(columnname))
As Nicholas Cary notes, if the column is numbers you'd need to cast as varchar first:
SELECT *
FROM tablename
WHERE CAST(columnname AS VARCHAR(10)) = REPLICATE(LEFT(CAST(columnname AS VARCHAR(10)),1),LEN(CAST(columnname AS VARCHAR(10))))
Riffing on #Dave.Gugg's excellent answer, here's another way, using patindex() to look for a character different than the first.
select *
from some_table t
where 0 = patindex( '[^' + left(t.some_column,1) + ']' , t.some_column )
Again, this only works for string types (char,varchar, etc.). Numeric types such as int will need to be converted first.