how to pad zero on left with String.Format - vb.net

i tried to use String.Format string with leading zero
Dim formatstring As String = ("{0,0}{1}{2,0:YYYY-MM-DD}{3}{4,0:00000}{5}{6,0:00000}{7}{8}{9}{10}{11}{12}")
with {4,0:00000} it would be fill zero on left
example
014 to 00014
with {6,0:00000} it would be fill zero on left too
example
547 to 00547
but it's not happen just still shown 014 and 547

For what you want to happen, the objects passed in the 4th and 6th indices need to support the IFormattable interface. From the docs:
If you specify formatString, the argument referenced by the format item must implement the IFormattable interface. Types that support format strings include:
All integral and floating-point types. (See Standard Numeric Format Strings and Custom Numeric Format Strings.)
DateTime and DateTimeOffset. (See Standard Date and Time Format Strings and Custom Date and Time Format Strings.)
All enumeration types. (See Enumeration Format Strings.)
TimeSpan values. (See Standard TimeSpan Format Strings and Custom TimeSpan format Strings.)
GUIDs. (See the Guid.ToString(String) method.)
That you have a value of 014, rather than simply 14, implies that what you have is already a string. The String type does not implement IFormattable. To get this to work, you need to first parse the strings to integers or similar type.

Related

bigquery Convert oct/hex/dec codes to its text equivalent

In my bigquery table i have some string values that for some unkown reason to me show up like;
BIQUÃ\215NI or BRASÃ\u008dLIA.
I know Ã\215 and Ã\u008d are equivalent to "Í", but i can't find a way to convert them to i'ts equivalent inside my query, i don't want to do a replace for each value that appears like that inside my bank, and i can't find a way to convert them to it's text equivalent inside bigquery documentation.
I already tried FORMAT('%o', 215) but it only converts octal to byte and it only work`s with numeric tables.
I tried REGEXP_REPLACE too but can`t find a way to refer to all octal forms inside the strings.
By using this online tool, Ã\215 and Ã\u008d are equivalent to "Í". But when you put in BigQuery, both gave an "Ã" value as it reads à only and both \215 and \u008d are not used or simply don't have an equivalent.
The CAST() function can be simply used in converting these UTF-8 encoded values, but the query reads ISO 8859-1(Latin-1) Unicode Mappings, and as I stated earlier, it will only return a null value.
My take on this case, you can convert first using the tool that I mentioned, and find the right Unicode Hex in unicode mappings.
SELECT CAST('BIQU\u00cdNI' AS STRING) AS Converted
Whereas, \u00cd is equivalent to Í.

How to migrate string format function with "!###" from vb6.0 to vb.net?

I'm migrating source from vb6.0 to vb.net and am struggling with this format function:
VB6.Format(text, "!#########")
VB6.Format(text, "00000")
I don't understand the meaning of "!#########" and "00000", and how to do the equivalent in VB.Net. Thanks
This:
VB6.Format(text, "!#########")
indicates that the specified text should be left-aligned within a nine-character string. Using standard .NET functionality, that would look like this:
String.Format("{0,-9}", text)
or, using the newer string interpolation, like this:
$"{text,-9}"
The second on is a little bit trickier. It's indicating that the specified text should be formatted as a number, zero-padded to five digits. In .NET, only actual numbers can be formatted as numbers. Strings containing digit characters are not numbers. You could convert the String to a number and then format it:
String.Format("{0:00000}", CInt(text))
or:
String.Format("{0:D5}", CInt(text))
If you were going to do that then it's simpler to just call ToString on the number:
CInt(text).ToString("D5")
If you don't want to do the conversion then you can pad the String explicitly instead:
text.PadLeft(5, "0"c)

convert date in 2015-05-09T09:00:12.123462000 to unix timestamp in hive

I want to convert the date which is in '2015-05-09T09:00:12.123462000' format to the unix timestamp in hive. The UNIX_TIMESTAMP('2015-05-09T09:00:12.123462000') doesn't work. I am not sure how i can convert this. I need this to compare two dates in different format. I am converting both the dates to unix timestamp but this fails. can someone please help with this.
Thanks
Your input uses the full ISO 8601 format, with a "T" between date and time, and fractional seconds. Hive expects an SQL format (i.e. with a space between date and time) as seen in java.sql.Timestamp and ODBC, with or without fractional seconds, as stated in the Hive documentation.
Just apply some very elementary string massaging -- then "cast" the String to a Hive Timestamp. And pleeease forget that lame roundtrip to and from UNIX_TIMESTAMP:
cast(regexp_replace('2015-05-09T09:00:12.123462000', 'T',' ') as Timestamp)
The Answer by Samson Scharfrichter is correct and should be accepted. I'll just add some words about java.time types.
Converting between String ↔ java.time.Instant
The java.time classes supplant the troublesome old legacy date-time classes such as java.sql.Timestamp.
The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Your input string complies with the ISO 8601 standard. The java.time classes use ISO 8601 formats by default when parsing/generating strings that represent date-time values. So no need to specify a formatting pattern. You can directly parse your string as an Instant object.
Your input string lacks an indication of offset-from-UTC or time zone. If it was intended to be in UTC, append a Z for Zulu which means UTC.
Instant instant = Instant.parse( "2015-05-09T09:00:12.123462000" + "Z" );
You can generate such a string, merely call toString. The default formatter used by toString prints the decimal fraction in groups of three digits as needed. In this example the last three digits are zeros so they are omitted.
String output = instant.toString();
2015-05-09T09:00:12.123462Z
To make this into SQL-style string expected by Hive, replace the T with a SPACE and remove the Z.
String inputForHive = output.replace( "T" , " " ).replace( "Z" , "" );
2015-05-09 09:00:12.123462
Conversion from numbers
Hive also provides for conversions from:
integer numberCount of whole seconds from the Unix/Posix epoch of beginning of 1970 UTC (1970-01-01T00:00:00Z).
floating-point numberSame as above but with a fractional second in up to nanoseconds resolution.
The second one I suggest you avoid. The floating-point types such as float, Float, double, and Double in Java purposely trade off accuracy for faster execution time. This often results in extraneous digits at the end of your decimal fraction. If you need fractional second, stick with the String type & Instant object.
The first one can easily be obtained from an Instant by calling the getEpochSecond method. Of course this means data loss as this method leaves behind any fractional second.
long secondsSinceEpoch = instant.getEpochSecond();
Going the other direction.
Instant instant = Instant.ofEpochSecond( secondsSinceEpoch );
Comparing
Once you have your Instant objects, you can compare with methods such as compareTo, equals, isBefore, isAfter.
Boolean happenedBefore = thisInstant.isBefore( thatInstant );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
If available you can simply use the following syntax
1) check whether what UDFs are available in your hive install?
show functions;
2) if seen from_unixtime() function then:
from_unixtime(your_timestamp_field)
This will solve the problem!
Please add comments, if you like my answer!

Date.parse function error

i am using the date.parse function to format the date input in the textbox.
However if I input a future date the date parse function fails.
Why is it so?
I need to format the date and also input future dates.
If IsDate(TransactionDate.Text) Then
TransactionDate.Text = Date.Parse(TransactionDate.Text)
Else
MsgBox("Enter correct Transaction date")
TransactionDate.Focus()
End If
i used 12/5/2013 and it worked fine.
I suspect it didn't, actually. I suspect it actually parsed it as December 5th 2013, when you meant May 12th 2013.
It seems that it's trying to parse it as a format of MM/dd/yyyy, whereas you mean dd/MM/yyyy.
That could be because the thread you're using is in a US culture, for example.
I suggest that you use DateTime.TryParseExact:
If you want to specify a precise format, you can do that using a custom date/time format pattern
If you want to allow .NET to use an appropriate format for the culture, you can do that using a standard date/time format pattern
Specify the culture you want to use for parsing, using CultureInfo.InvariantCulture if it's meant to parse machine-generated text. You can use null to mean "the culture of the currently executing thread" but personally I'd make that explicit, for readability
Use the return value to determine whether parsing failed. If this indicates a bug somewhere, then it would make sense to use DateTime.ParseExact instead, as that will throw an exception. Basically you need to work out what you want to do on error.
You need to specify the format you are expecting the data string to be in. Dates can be written in many formats - US (mm/dd/yyyy) or European (dd/mm/yyyy), two digit years (yy) or four digit years (yyyy), different separator (/) vs (-) and so on. The list is endless.
You either need to state explicitly what date you expect or write your code to try various formats and cope with incorrect inputs.
The danger with the latter approach is that dates can be ambiguous - is "1/2/2013" the 1st of February or the 2nd of January?
Use an overload of Date.TryParse that takes an IFormatProvider and report an error if it fails.
dateString = "2008-03-01 10:00"
culture = CultureInfo.CreateSpecificCulture("fr-FR")
styles = DateTimeStyles.AdjustToUniversal Or DateTimeStyles.AssumeLocal
If DateTime.TryParse(dateString, culture, styles, dateResult)
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
Use DateTime.ParseExact and pass the format and provide.
dateString = "13/05/2013";
format = "dd-MM-yyyy";
CultureInfo provider = CultureInfo.InvariantCulture;
var result = DateTime.ParseExact(dateString, format, provider);

How to apply formatting string when binding DateTime to MaskedTextBox?

I have a MaskedTextBox using the mask "00/00/\2\000" to restrict input to a format of XX/XX/20XX, with the Text property bound to MyBindingSource.SomeProperty of type DateTime.
Recently, values with a single-digit month or day recently started displaying incorrectly. I expect that the ToString() method is being called on the value at some point in data binding, and I believe the call is not padding month / day with zeroes.
I tried setting the format type of the MaskedTextBox.Text property to DateTime in the advanced data binding properties, but it didn't help.
How can I apply ToString("MMddyyyy") when converting the DateTime object to a string, before the value is bound to the Text property?
You can use the binding's Parse and Format events to do the conversion yourself, as seen in this answer