Thursday, April 2, 2020

SQL SERVER - How to align string length to fixed size

With left() function is problem because ending spaces are lost. The solution is little trick with space() function.
select top 1 left( itemid, 25 ) + 'x' from inventitemprice
where
itemid = 'S-144-700-1006'

select top 1 left( itemid + space(100), 25 ) + 'x' from inventitemprice
where
itemid = 'S-144-700-1006'
Output:
--------------------------
S-144-700-1006x

(1 row(s) affected)


--------------------------
S-144-700-1006           x

(1 row(s) affected)

Monday, March 9, 2020

C# - How call stored proc with output/return parameter ?

public static string GetStatistics() {
  string value = "";

  SqlCommand command = DB.SPPrepare( "GetStatisticsAsString" );
            
  command.Parameters.Add( new SqlParameter( "@catalog", "HYG" ) );
  command.Parameters.Add( new SqlParameter( "@catalogDSO", "MYDSO" ) );
  SqlParameter par = new SqlParameter( "@output", System.Data.SqlDbType.NVarChar, 2048 );
  par.Direction = System.Data.ParameterDirection.Output;
  command.Parameters.Add( par );

  command.ExecuteNonQuery();

  value = (string)command.Parameters[ "@output" ].Value;

  return value;
}

Monday, March 2, 2020

C# - How to use generic Min(), Max()

shape.Saved_Width =
 Math.Abs( shape.Points.Max( item => item.Draw_X )-shape.Points.Min( item => item.Draw_X ) );

Friday, February 28, 2020

WIN OS - How to flush your machine DNS cache ?

ipconfig /flushdns

AX - How to make sorting in grid column

Override datasource Init() method:
public void init()
{
    super();

    this.query().dataSourceNo( 1 ).addSortField( fieldNum( CIEB_Torque, cieb_datetime ), 
      SortOrder::Ascending );
}
Output:

Friday, January 31, 2020

SQL SERVER - How to make cumulative sum column

If you need cumulative sum if SQL select, you can use analytical function:
select  cast( mag as int ) as mag, count(*) as count,
sum ( count(*) ) over 
( order by cast( mag as int ) rows between unbounded preceding and current row ) 
as count_cumul
from dso
where
catalog = 'MYDSO'
group by cast( mag as int )
order by cast( mag as int )
Output:
mag         count       count_cumul
----------- ----------- -----------
0           5           5
1           5           10
2           7           17
3           13          30
4           26          56
5           45          101
6           88          189
...

Tuesday, January 14, 2020

C# - How to sort generic list by item value

private List<ConstellationIndex> ConstellationsIndex = new List<ConstellationIndex>();
...
this.ConstellationsIndex.Sort( (x, y) => x.Constellation.CompareTo( y.Constellation ) );

Thursday, January 9, 2020

C# - How to go through files in directory with files copy

DirectoryInfo dir = new DirectoryInfo( this.OverviewMapCopyFromDir );

foreach( FileInfo file in dir.GetFiles( "overview_*.tif" ) ) {
  File.Copy( file.FullName, Path.Combine( m_FullOutputDirectory, file.Name ) );    
}

Friday, January 3, 2020

C# - How to convert first character of string to upper letter

The problem is little complicated, because access to string chars (through indexer) is only read-only.
One way is this:

row.Constellation = arr[2].Trim().ToLower();
char[] ch = row.Constellation.ToCharArray();
ch[0] = char.ToUpper( row.Constellation[0] );
row.Constellation = new string( ch );