• No results found

String and ustring field conversions

In document 8.0 Environment Variables (Page 196-200)

String and ustring field conversions

Usethemodifyoperatortoperformthefollowingmodificationsinvolvingstringandustringfields:

v Extractthelength ofastring.

v Convertlongstringstoshorterstringsbystringextraction.

v Convertstringstoandfromnumericvaluesusinglookuptables(see″StringConversionsandLookup Tables″ ).

modify

output data set input data set

field1 = raw_length(aField);

field2 = aField

schema:

aField:raw[16];

schema:

field1:int32;

field2:raw;

ConversionSpecification Description

stringField=string_trim[character,direction,justify](string) Youcanusethisfunctiontoremovethecharactersused topadvariable-lengthstringswhentheyareconvertedto fixed-lengthstringsofgreaterlength.Bydefault,these charactersareretainedwhenthefixed-lengthstringis thenconvertedbacktoavariable-lengthstring.

Thecharacterargumentisthecharactertoremove.It defaultstoNULL.Thevalueofthedirectionandjustify argumentscanbeeitherbeginorend;directiondefaults toend,andjustifydefaultstobegin.justifyhasnoaffect whenthetargetstringhasvariablelength.

Examples:

name:string=string_trim[NULL,begin](name) removesallleadingASCIINULLcharactersfromthe beginningofnameandplacestheremainingcharacters inanoutputvariable-lengthstringwiththesamename.

hue:string[10]=string_trim[’Z’,end,begin](color) removesalltrailingZcharactersfromcolor,andleft justifiestheresultinghuefixed-lengthstring.

stringField=substring(string,starting_position,length) ustringField=u_substring(ustring,starting_position,length)

Copiespartsofstringsandustringstoshorterstringsby stringextraction.Thestarting_positionspecifiesthe startinglocationofthesubstring;lengthspecifiesthe substringlength.

Theargumentsstarting_positionandlengthareuint16 typesandmustbepositive(>=0).

stringField=lookup_string_from_int16 [tableDefinition](int16Field)

ustringField=lookup_ustring_from_int16 [tableDefinition](int16Field)

Convertsnumericvaluestostringsandustringsby meansofalookuptable.

int16Field=lookup_int16_from_string [tableDefinition](stringField)

int16Field=lookup_int16_from_ustring [tableDefinition](ustringField)

Convertsstringsandustringstonumericvaluesby meansofalookuptable.

Convertsnumericvaluestostringsandustringsby meansofalookuptable.

stringField=string_from_ustring(ustring) Convertsustringstostrings.

ustringField=ustring_from_string(string) Convertsstringstoustrings.

decimalField=decimal_from_string(stringField) Convertsstringstodecimals.

decimalField=decimal_from_ustring(ustringField) Convertsustringstodecimals.

ConversionSpecification Description stringField=string_from_decimal[fix_zero]

[suppress_zero](decimalField)

Convertsdecimalstostrings.

fix_zerocausesadecimalfieldcontainingallzerostobe treatedasavalidzero.

suppress_zerospecifiesthatthereturnedustringvalue willhavenoleadingortrailingzeros.Examples:000.100 ->0.1;001.000->1;-001.100->-1.1

ustringField=ustring_from_decimal[fix_zero]

[suppress_zero](decimalField)

Convertsdecimalstoustrings.

Seestring_from_decimalaboveforadescriptionofthe fix_zeroandsuppress_zeroarguments.

dateField=date_from_string[date_format|date_uformat]

(stringField)

dateField=date_from_ustring[date_format|date_uformat]

(ustringField)

datefromstringorustring

Convertsthestringorustringfieldtoadate representationusingthespecifieddate_formator date_uformat.

Bydefault,thestringformatisyyyy-mm-dd.date_format anddate_uformataredescribedin″dateFormats″.

stringField=string_from_date[date_format|date_uformat]

(dateField)

ustringField=ustring_from_date[date_format| date_uformat](dateField)

stringsandustringsfromdate

Convertsthedatetoastringorustringrepresentation usingthespecifieddate_formatordate_uformat.

Bydefault,thestringformatisyyyy-mm-dd.date_format anddate_uformataredescribedin″dateFormats″. int32Field=string_length(stringField)

int32Field=ustring_length(ustringField)

Returnsanint32containingthelengthofastringor ustring.

stringField=substring[startPosition,len](stringField) ustringField=substring[startPosition,len](ustringField)

Convertslongstrings/ustringstoshorterstrings/

ustringsbystringextraction.ThestartPositionspecifies thestartinglocationofthesubstring;lenspecifiesthe substringlength.

IfstartPositionispositive,itspecifiesthebyteoffsetinto thestringfromthebeginningofthestring.IfstartPosition isnegative,itspecifiesthebyteoffsetfromtheendofthe string.

stringField=uppercase_string(stringField) ustringField=uppercase_ustring(ustringField)

Convertstringsandustringstoalluppercase.

Non-alphabeticcharactersareignoredintheconversion.

stringField=lowercase_string(stringField) ustringField=lowercase_ustring(ustringField)

Convertstringsandustringstoalllowercase.

Non-alphabeticcharactersareignoredintheconversion.

stringField=string_from_time[time_format|time_uformat ](timeField)

ustringField=ustring_from_time[time_format| time_uformat](timeField)

stringandustringfromtime

Convertsthetimetoastringorustringrepresentation usingthespecifiedtime_formatortime_uformat.The time_formatoptionsaredescribedbelow.

ConversionSpecification Description

Convertsthetimestamptoastringorustring representationusingthespecifiedtimestamp_formator timestamp_uformat.

Bydefault,thestringformatis%yyyy-%mm-%dd hh:mm:ss.Thetimestamp_formatandtimestamp_uformat optionsaredescribedin″timestampFormats″. tsField=timestamp_from_string[timestamp_format|

timestamp_uformat](stringField)

tsField=timestamp_from_ustring[timestamp_format| timestamp_uformat](usringField)

timestampfromstringsandustrings

Convertsthestringorustringtoatimestamp representationusingthespecifiedtimestamp_formator timestamp_uformat.

Bydefault,thestringformatisyyyy-mm-ddhh:mm:ss.

Thetimestamp_formatandtimestamp_uformatoptionsare described″timestampFormats″.

timeField=time_from_string[time_format| time_uformat](stringField)

timeField=time_from_ustring[time_format|time_uformat]

(ustringField)

stringandustringfromtime

Convertsthetimetoastringorustringrepresentation usingthespecifiedtime_format.Thetime_uformatoptions aredescribedbelow.

Thefollowingoshcommandconverts astringfieldtolowercase:

osh "... | modify "lname=lowercase_string(lname)" | peek"

Thediagramshowsamodification thatconvertsthenameofaFieldtofield1 andproducesfield2 from bFieldbyextractingthefirsteight bytesofbField:

Thefollowingoshcommandperformsthesubstringextraction:

modify ’field1 = aField; field2 = substring[0,8](bField);’

Thediagramshowstheextractionof thestring_lengthof aField.Thelengthisincludedintheoutputas field1.

Thefollowingoshcommandsextractthelengthof thestringinaFieldandplaceitinfield1of theoutput:

$ modifyspec="field1 = string_length(aField); field2 = aField;"

$ osh " ... | modify ’$modifySpec’ |... "

Noticethata shellvariable(modifySpec)hasbeendefinedcontainingthespecificationspassed tothe operator.

In document 8.0 Environment Variables (Page 196-200)