Fix datetime string in SQL Server - sql-server

I have a table with 43 000 000 rows. In datetime column data looks 2020.04.22T04:39:29.359 and it's of type VARCHAR.
It looks like an ISO format but there are ., and for ISO I need -.
What is the best way to convert the values in these rows to datetime?
One of my variant was subst . to -:
UPDATE table
SET [Column 3] = REPLACE([Column 3], '.', '-');
but then I need to cut a microseconds from the end.
How to do this cut?
Or maybe you can advice more truish way.

You may use TRY_CONVERT here, after first doing a bit of massaging to bring your raw datetime strings into a format which SQL Server recognizes:
UPDATE yourTable
SET new_dt_col = TRY_CONVERT(
datetime,
REPLACE(LEFT(dt_col, 19), '.', '-') + '.' + RIGHT(dt_col, 3)
);
To be explicit, the replacement logic used above would first convert this:
2020.04.22T04:39:29.359
into this:
2020-04-22T04:39:29.359
You may verify for yourself that the following conversion works correctly:
SELECT TRY_CONVERT(datetime, '2020-04-22T04:39:29.359');

If every single row in the table has the date that follows the format 2020.04.22T04:39:29.359 you can do the following update statement:
UPDATE table
SET [Column 3] = SUBSTRING([Column 3],1,4) + '-' + SUBSTRING([Column 3],6,2) + '-' + SUBSTRING([Column 3],9,15)
To only fix the 5th and 8th character without affecting the "." character from the microseconds.
After that you should be able to do the conversion to datetime.

Related

Create query with new column concatenate based on CASE/IIF criterias

I have four columns in a table. The table identifies the packaging amounts per box. The four columns are -
ID
PerCase
InnerCarton
PerPack
I want query and create 2 columns ID & Condensed
Column condensed should result in "perCase/InnerCarton/PerPack"
there are many IDs where the InnerCarton is null, so I want the condensed column to show "PerCase/PerPack"
I tried this -
SELECT ID,
CAST(IIf([Items_UOM].[InnerCase] Is Not Null
,[Items_UOM].[PerCase] & '/' & [Items_UOM].[InnerCase] & '/' & [Items_UOM].[PerPack]
,[Items_UOM].[PerCase] & '/' & [Items_UOM].[PerPack]) AS varchar(25))
FROM Items_UOM;
I am getting an error message - Conversion failed when converting the varchar value '/' to data type smallint.
You have several things going on here not quite correct. First, to combine strings you use + not &. Second, you have datatype conversion issues because your columns are tinyint and you can't just add characters to add without an explicit conversion. You can also simplify this by using ISNULL or COALESCE instead of IIF and being forced to repeat the logic.
This should work for you.
SELECT ID,
convert(varchar(25), [Items_UOM].[PerCase]) + ISNULL('/' + [Items_UOM].[InnerCase], '') + '/' + convert(varchar(25), [Items_UOM].[PerPack])
FROM Items_UOM;
& is the binary and operator, not a string concatenation operator.
If CONCAT_NULL_YIELDS_NULL is set you could use + to concatenate the values with the slash and concat() to build the overall string. As with + the concatenation gets NULL if one operand is null the slash gets sort of eliminated for NULLs. concat() however replaces NULLs with an empty string, so the overall result isn't NULL if there are NULL values.
If the values are integers you need to convert them to a string them prior using them in the + as otherwise the + is interpreted as an arithmetic plus and results in the engine trying to convert the '/' to a number, which of course fails.
SELECT id,
concat(convert(varchar(max), items_uom.innercase) + '/',
convert(varchar(max), items_uom.percase) + '/',
convert(varchar(max), items_uom.perpack)
FROM items_uom;

how to increment nvarchar type in MSSQL

I have a table Tab1 with one of the column being ID ofNvarchar(80) [MSSQL] . I would like to have a variable as with initial value= 00100000 and then increment it by 00100001,00100002,00100003 and so on .later use it to fetch tab1.id from this variable in a loop. Can any one help me with this?
I'll start by saying that if you want an easy-to-increment variable, NVARCHAR isn't exactly the way to go; but I'll also assume you have a reason for doing this.
That being said, incrementing the value is as easy as:
SELECT CAST(<column_name> AS INT) + 1 FROM Tab1;
To insert the value with leading zeroes, you're going to need to do some concatenation:
SELECT '00' + CAST(CAST(<column_name AS INT) + 1 AS NVARCHAR(80)) FROM Tab1;
Just FYI, SQL Server will automatically try to convert the NVARCHAR value to an INTEGER value when you try adding 1, so you could technically shorten this to:
SELECT '00' + CAST(<column_name> + 1 AS NVARCHAR(80));
If you're eventually expecting to reach values which override those two leading zeroes, you're also going to need to take that into account later on. There are several ways that you could do this. Among the simplest:
SELECT RIGHT('00000' + CAST(<column_name> + 1 AS NVARCHAR(80)),8);

formatting datetime to varchar with padded zeros

I have the following field called "MaterialPrice". It is a data type of -
DECIMAL (18,2)
So a sample values is "10.88"
What I need to change it to is something like below -
0000000000000**1088**0
So the field length is 18, where the last character (to the left is always 0) and the characters in front of the original value are padded with zeros also.
Another example would be
501.02
would be
000000000000**50102**0
Any help would be appreciated.
Thanks
If I understand correctly the requirement, you could as the below:
DECLARE #val DECIMAL(18, 2) = 501.02
SELECT REPLICATE(0, 18 - LEN(#val)) + '**' + REPLACE(CAST(#val AS VARCHAR(50)), '.', '') + '**0'
Result: 000000000000**50102**0
I would:
Multiply by 100,
cast to string,
Measure length,
Concatenate: (17-length) "0"s, "**", the string number and "**0

String of date time convert to date time in SQL Server

I have this string which is a combination of date and time but in string format meaning that it has no space. I parsed it from a very long string but I now have or need to convert it to a standard date and time.
This is my string for date and time:
141007024755
This is how I parsed it from a very long string of data
[date&time] = SUBSTRING(#ProductCode, 27, 12)
This is the format I'm expecting but can't do it.
2014-10-07 02:47:55.000
Can anyone give me a hint on how to do this? An advice perhaps.
Thanks.
If the string is always 12 characters long you could try this:
select cast(stuff(stuff(stuff('141007024755', 7,0,' '), 10,0,':'), 13,0,':') as datetime)
Basically it uses the stuff function to insert a space between the date and time parts and colons between the different time parts producing a string like141007 02:47:55that can be converted todatetimeusingcast.
Assuming the dates are formatted exactly as your sample string, you can just keep chopping up the string and appending it back together and cast the result to date. Simple function like this may help:
CREATE FUNCTION dbo.udf_ReturnDateFromString(#DateString AS VARCHAR(14))
RETURNS DATETIME
AS
BEGIN
SET #DateString = '20' + #DateString
RETURN CAST(LEFT(#DateString, 4) + '-' +
SUBSTRING(#DateString, 5, 2) + '-' +
SUBSTRING(#DateString, 7, 2) + ' ' +
SUBSTRING(#DateString, 9, 2) + ':' +
SUBSTRING(#DateString, 11, 2) + ':' +
SUBSTRING(#DateString, 13, 2) AS DATETIME)
END;
GO
SELECT dbo.udf_ReturnDateFromString('141007024755');
The result is:
2014-10-07 02:47:55.000
You get the idea. This was done on SS 2008 R2.

Convert float into varchar in SQL Server without scientific notation

Convert float into varchar in SQL Server without scientific notation and trimming decimals.
For example:
I have the float value 1000.2324422, and then it would be converted into varchar as same 1000.2324422.
There could be any number of decimal values...the float value comes randomly.
Casting or converting to VARCHAR(MAX) or anything else did not work for me using large integers (in float fields) such as 167382981, which always came out '1.67383e+008'.
What did work was STR().
Neither str() or cast(float as nvarchar(18)) worked for me.
What did end up working was converting to an int and then converting to an nvarchar like so:
convert(nvarchar(18),convert(bigint,float))
The STR function works nice. I had a float coming back after doing some calculations and needed to change to VARCHAR, but I was getting scientific notation randomly as well. I made this transformation after all the calculations:
ltrim(rtrim(str(someField)))
Try CAST(CAST(#value AS bigint) AS varchar)
This works:
CONVERT(VARCHAR(100), CONVERT(DECIMAL(30, 15), fieldname))
Try this:
SELECT REPLACE(RTRIM(REPLACE(REPLACE(RTRIM(REPLACE(CAST(CAST(YOUR_FLOAT_COLUMN_NAME AS DECIMAL(18,9)) AS VARCHAR(20)),'0',' ')),' ','0'),'.',' ')),' ','.') FROM YOUR_TABLE_NAME
Casting as DECIMAL will put decimal point on every value, whether it
had one before or not.
Casting as VARCHAR allows you to use the REPLACE function
First REPLACE zeros with spaces, then RTRIM to get rid of all trailing spaces (formerly zeros), then REPLACE remaining spaces with zeros.
Then do the same for the period to get rid of it for numbers with no decimal values.
This is not relevant to this particular case because of the decimals, but may help people who google the heading.
Integer fields convert fine to varchars, but floats change to scientific notation. A very quick way to change a float quickly if you do not have decimals is therefore to change the field first to an integer and then change it to a varchar.
Below is an example where we can convert float value without any scientific notation.
DECLARE #Floater AS FLOAT = 100000003.141592653
SELECT CAST(ROUND(#Floater, 0) AS VARCHAR(30))
,CONVERT(VARCHAR(100), ROUND(#Floater, 0))
,STR(#Floater)
,LEFT(FORMAT(#Floater, ''), CHARINDEX('.', FORMAT(#Floater, '')) - 1)
SET #Floater = #Floater * 10
SELECT CAST(ROUND(#Floater, 0) AS VARCHAR(30))
,CONVERT(VARCHAR(100), ROUND(#Floater, 0))
,STR(#Floater)
,LEFT(FORMAT(#Floater, ''), CHARINDEX('.', FORMAT(#Floater, '')) - 1)
SET #Floater = #Floater * 100
SELECT CAST(ROUND(#Floater, 0) AS VARCHAR(30))
,CONVERT(VARCHAR(100), ROUND(#Floater, 0))
,STR(#Floater)
,LEFT(FORMAT(#Floater, ''), CHARINDEX('.', FORMAT(#Floater, '')) - 1)
SELECT LEFT(FORMAT(#Floater, ''), CHARINDEX('.', FORMAT(#Floater, '')) - 1)
,FORMAT(#Floater, '')
In the above example, we can see that the format function is useful for us. FORMAT() function returns always nvarchar.
I have another solution since the STR() function would result some blank spaces, so I use the FORMAT() function as folowing example:
SELECT ':' + STR(1000.2324422), ':' + FORMAT(1000.2324422,'##.#######'), ':' + FORMAT(1000.2324422,'##')
The result of above code would be:
: 1000 :1000.2324422 :1000
You can use this code:
STR(<Your Field>, Length, Scale)
Your field = Float field for convert
Length = Total length of your float number with Decimal point
Scale = Number of length after decimal point
For example:
SELECT STR(1234.5678912,8,3)
The result is: 1234.568
Note that the last digit is also round up.
You will have to test your data VERY well. This can get messy. Here is an example of results simply by multiplying the value by 10. Run this to see what happens.
On my SQL Server 2017 box, at the 3rd query I get a bunch of *********. If you CAST as BIGINT it should work every time. But if you don't and don't test enough data you could run into problems later on, so don't get sucked into thinking it will work on all of your data unless you test the maximum expected value.
Declare #Floater AS FLOAT =100000003.141592653
SELECT CAST(ROUND(#Floater,0) AS VARCHAR(30) ),
CONVERT(VARCHAR(100),ROUND(#Floater,0)),
STR(#Floater)
SET #Floater =#Floater *10
SELECT CAST(ROUND(#Floater,0) AS VARCHAR(30) ),
CONVERT(VARCHAR(100),ROUND(#Floater,0)),
STR(#Floater)
SET #Floater =#Floater *100
SELECT CAST(ROUND(#Floater,0) AS VARCHAR(30) ),
CONVERT(VARCHAR(100),ROUND(#Floater,0)),
STR(#Floater)
There are quite a few answers but none of them was complete enough to accommodate the scenario of converting FLOAT into NVARCHAR, so here we are.
This is what we ended up with:
DECLARE #f1 FLOAT = 4000000
DECLARE #f2 FLOAT = 4000000.43
SELECT TRIM('.' FROM TRIM(' 0' FROM STR(#f1, 30, 2))),
TRIM('.' FROM TRIM(' 0' FROM STR(#f2, 30, 2)))
SELECT CAST(#f1 AS NVARCHAR),
CAST(#f2 AS NVARCHAR)
Output:
------------------------------ ------------------------------
4000000 4000000.43
(1 row affected)
------------------------------ ------------------------------
4e+006 4e+006
(1 row affected)
In our scenario the FLOAT was a dollar amount to 2 decimal point was sufficient, but you can easily increase it to your needs.
In addition, we needed to trim ".00" for round numbers.
Try this code
SELECT CONVERT(varchar(max), CAST(1000.2324422 AS decimal(11,2)))
Result:
1000.23
Here decimal(11,2): 11-total digits count (without the decimal point), 2 - for two digits after the decimal point
None of the previous answers for me. In the end I simply used this:
INSERT INTO [Destination_Table_Name]([Field_Name])
SELECT CONCAT('#',CAST([Field_Name] AS decimal(38,0))) [Field_Name]
FROM [dbo].[Source_Table_Name] WHERE ISNUMERIC([CIRCUIT_NUMBER]) = 1
INSERT INTO [Destination_Table_Name]([Field_Name])
SELECT [Field_Name]
FROM [dbo].[Source_Table_Name] WHERE ISNUMERIC([CIRCUIT_NUMBER]) <> 1
select format(convert(float,#your_column),'0.0#########')
Advantage: This solution is independent of the source datatype (float, scientific, varchar, date, etc.)
String is limited to 10 digits, and bigInt gets rid of decimal values.
This works:
Suppose
dbo.AsDesignedBites.XN1E1 = 4016519.564`
For the following string:
'POLYGON(('+STR(dbo.AsDesignedBites.XN1E1, 11, 3)+'...

Resources