Splunk Search

how to convert number to string using tostring function?

LearningGuy
Builder

Hello,
how to convert number to string using tostring function?
I tried using tostring function, but the result is still number
See below.   Thank you!!

| makeresults

| eval num = 1
| eval var_type = typeof('num')

| eval num2 = tostring(num)
| eval var_type2 = typeof('num2')

 

LearningGuy_0-1711035209226.png

 

Labels (3)
0 Karma

LearningGuy
Builder

They both became String.     Num should be number.  Thanks
LearningGuy_0-1711036391407.png

 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @LearningGuy,

the second it's a string, you transformed it using the tostring function, infact you have the commas.

Ciao.

Giuseppe

LearningGuy
Builder

@gcusello 
Yes but why the first one is also string?
The first one is number.
Should I remove " " from typeof?        Thanks

| makeresults
| eval num = 1 
| eval var_type = typeof(num)
| eval num2 = tostring("num")
| eval var_type2 = typeof(num2)

LearningGuy_0-1711036912855.png

 


Thanks

0 Karma

gcusello
SplunkTrust
SplunkTrust

hi @LearningGuy,

as I said, you have to say what kind of string you want:

| makeresults
| eval num = 1 
| eval var_type = typeof(num)
| eval num2 = tostring(num,"commas")
| eval var_type2 = typeof(num2)

Ciao.

Giuseppe

LearningGuy
Builder

Hello @gcusello 

Sorry.. I tried again your last suggestion, but num and num2 still have type as "Number"
I expect num2 has "String" type after using   num2= tostring(num,"commas")  
Please suggest   Thanks again..

LearningGuy_0-1711042200468.png

 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @LearningGuy ,

As I said, using 1000 instead of 1 you have a string.

I don't know why with a different number you have a different type.

You eventually could to try:

| makeresults
| eval num = 1 
| eval var_type = typeof(num)
| eval num2 = tostring(num,"commas")." "
| eval var_type2 = typeof(num2)

Ciao.

Giuseppe

gcusello
SplunkTrust
SplunkTrust

Hi @LearningGuy ,

see at https://docs.splunk.com/Documentation/SCS/current/SearchReference/ConversionFunctions

and try

| makeresults
| eval num = 1
| eval var_type = typeof('num')
| eval num2 = tostring(num, "commas")
| eval var_type2 = typeof('num2')

Ciao.

Giuseppe

LearningGuy
Builder

Hello @gcusello ,

I tried and the same result..  see below..  thank you

LearningGuy_0-1711035919488.png

 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @LearningGuy,

what does it happen running:

| makeresults
| eval num = 1000
| eval var_type = typeof("num")
| eval num2 = tostring(num, "commas")
| eval var_type2 = typeof("num2")

Ciao.

Giuseppe

Get Updates on the Splunk Community!

Modern way of developing distributed application using OTel

Recently, I had the opportunity to work on a complex microservice using Spring boot and Quarkus to develop a ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had 3 releases of new security content via the Enterprise Security ...

Archived Metrics Now Available for APAC and EMEA realms

We’re excited to announce the launch of Archived Metrics in Splunk Infrastructure Monitoring for our customers ...