Converting Numeric to timestamp format - snowflake-cloud-data-platform

I have numeric data in a field and need to convert it to timestamp as like below. Please help on this.
source data:
20220929121001.0000000
20220919085939.0000000
need to convert to below format of timestamp:
yyyymmddhh24miss
Thanks!

try this -
select to_timestamp_ntz(to_char(20220929121001.0000000),'yyyymmddhh24miss') tstmp
to_char - to convert to char and remove training 0s.
to_timestamp_ntz - to convert char to timestamp
You can also try to wrap around to_number() to remove 0s like below.
select to_timestamp_ntz(to_char(to_number( 20220929121001.0000000)),'yyyymmddhh24miss') tstmp

I am able to convert by doing following logic
TO_VARCHAR(to_timestamp(to_char(trunc(20220929121001.0000000)),'yyyymmddhh24miss'),'yyyymmddhh24miss')

Related

conversion failed when converting date from character message

I receive a file monthly that for some reason has service_date as a char(8) data type. The image shows some of the things that are entered and the second column is the length of the service date field. Is there a way for me to clean up the bad dates? I was hoping if the length was shorter or longer I could eliminate them but it doesn't seem like it will work. Any ideas on what the best way to fix this field would be. Thanks.
You can use the try_convert function to attempt to convert your Service_Date values into a date and excluding any where a null is returned.

Convert varchar dd-mm-yyyy to yyyy-mm-dd date field in SQL Server 2008

hopefully the title describes what I'm trying to do.
I have a varchar field in a SQL Server 2008 table that contains text dates in the format dd-mm-yyyy (e.g., 31-12-2009). I am trying to use CONVERT to convert it to a DATE field. I was successful in converting a similar varchar field in the same table using the following:
SELECT DISTINCT(CONVERT(DATE, MYDATEFIELD1, 103)) AS [CONV_MYDATEFIELD1] FROM MYTABLE;
But when I apply the same to MYDATEFIELD2, which appears to have the same type of data values as MYDATEFIELD1, it fails with the following error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I've tried sorting and using LIKE to try to find any characters that might prevent the conversion but I haven't been able to pinpoint anything.
Any help will be greatly appreciated. Thanks!
You may have some invalid dates (e.g. 30-02-2009), try to find them splitting the characters and validating the day and the months, assuring that the days correspond to the month and the month is in the range 01 - 12.
If you can't find which value is causing the conversion error then use a cursor to go through all the records individually and use TRY CATCH to find which record(s) cause the conversion error. You could use a PRINT statement in the CATCH block to identify the records that are erroring.
Find your bad dates with the following:
SET DATEFORMAT dmy;
select MYDATEFIELD1, isdate(MYDATEFIELD1)
from MYDATEFIELD1
I figured out the issue that was causing the CONVERT to fail but I'm not sure of the best way to select an answer (veritable stack noob) so, any help on that would be appreciated. Here are the major steps I took to find the issue:
I used MIN and MAX SUBSTRING to identify that the component parts of the
varchar field were correct (i.e., the 1st two digits min=01 max=31,
middle two min=01 max=12)
I used DISTINCT SUBSTRING to identify that all of the date separators were consistent (i.e., all dashes).
I used MAX(LEN) to determine that my varchar "date" field was 12 characters (vs. the 10 characters I was expecting).
I used CONVERT(VARBINARY, MYDATEFIELD2) to determine what was actually stored in the string.
The last step revealed that the field contained line feeds (00A). I opened the source text file in notepad++, clicked View -> Show Symbol -> Show All Characters and I could see the LF at the end of each line.
So now I'm modifying the DTSX package (fixed width text) to include an extra field for the linefeed that I can drop afterwards. Now that I know what the intended format of the date fields is, I'll try to import them as DT_DATE vs DT_STR. I'm not exactly sure how to specify the correct date style 105 at import (thanks #Panagiotis Kanavos) but I'll figure it out.
Whew! What a learning experience! :D
Thanks to everyone who helped - and if you can give advice on the best way to select the best answer it will be greatly appreciated.

Convert varchar to hexadecimal in sql server

I want a function to convert a varchar to hexadecimal in sql server. Please help me.
P.S. I will use unhex() in Hive to try to get the original value back. This is because my data contains special characters and backslash and the HIVE external table does not recognise it
You can try to use CONVERT function:
SELECT CONVERT(VARBINARY(MAX), 'myText')
Output:
0x6D7954657874
you can use
select try_convert(varbinary,varcharcolumn)
You should use try_convert to avoid errors when the conversion fails.. Also, varbinary(n) is a better alternative to varbinary(max) as the latter would set the page size to 2GB, which might be excessive.
Hope this helps!

How to convert decimal values in tofixed format in postgresql

My table column in decimal format like this
My column value is:
4523
1236.23
1240.5
2535.2
I would like to this format:
4523.00
1236.33
1240.50
2535.20
Thanks.
to_char.
SELECT to_char(1231.123, '9999999999.00');
See: data type formatting functions.
The manual is your friend.

SQL Query Result Problem

I have Two SQL Query Both Return
select round(convert(float,'24367.723'),2)
Result:24367.72
Second:
select convert(varchar(20),round(convert(float,'24367.723'),2))
Result:24367.7
Why the Second Query Return exclude the last digit after converting to varchar
Thanks in Advance
By not specifying a style parameter to the convert function you get the default style (0).
i.e. it is equivalent to doing
select convert(varchar(20),round(convert(float,'24367.723'),2), 0)
The default style for converting from float to varchar displays a maximum of 6 digits.
When working with a float the STR() function usually gives better results according to MSDN as you've more control.
E.g.
select str(convert(float,'24367.723'),8, 2)
Dont use floats, use exact numberics. Something like this
convert(varchar(20), convert(numeric(20,2), '24367.72'))

Resources