Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert "TRUE" to 1 in powershell variable

Tags:

powershell

I am reading a cell from Excel which contains TRUE or FALSE. After reading it I need to insert the value in a table in SQL Server with datatype bit.

I get the value in the cell with this line of code:

   $myvariable =  $WorkbookTotal.Cells.Item($ExcelRowToRead, 10).Text

Is there a way to convert TRUE or FALSE to 1or 0 without writing an if statement? I need $myvariable to become a 1/0 value..

like image 437
xhr489 Avatar asked Oct 27 '25 04:10

xhr489


1 Answers

You could convert the string value to [bool] with bool.Parse, then convert to [int]:

$myvariable = "TRUE"
$bit = [int][bool]::Parse($myvariable)

After which:

PS ~> $bit
1
like image 55
Mathias R. Jessen Avatar answered Oct 30 '25 07:10

Mathias R. Jessen