Can we use MASKED (FUNCTION = random(1,100)) with MONEY datatype? - sql-server-2016

Can anyone please help me with my question?
Can we use MASKED (FUNCTION = random(1,100)) with MONEY datatype? I am sure we can use it with INT data type.

This query worked for me. I think you need to add a WITH after MASKED. I found this article on data masking: https://learn.microsoft.com/en-us/sql/relational-databases/security/dynamic-data-masking?view=sql-server-2017
CREATE TABLE [dbo].[tblTest](
[intTest] [int] MASKED WITH (FUNCTION = 'default()') NULL,
[monTest] [money] MASKED WITH (FUNCTION = 'default()') NULL
)

Related

Numeric value out of range using R, ODBC and DBI

I have a problem that you could help me to solve.
I had been trying to insert some rows into a table.
I show you the table definition:
CREATE TABLE Scc_OrdenSevicioFunerarioPagare(
[NumeroPagare] [int] NOT NULL,
[CodigoEstablecimiento] [tinyint] NOT NULL,
[NumeroOrden] [int] NOT NULL,
[CodigoClienteAvalista] [int] NOT NULL,
[ValorRecibido] [money] NOT NULL,
[ValorPagare] [money] NOT NULL,
[FechaPago] [date] NOT NULL,
[CantidadPago] [int] NOT NULL)
Using R, I had tried to insert the rows with the next code:
dsnDesarrollo <- "TESTSQL";
SCC_OrdenServicioFunerarioPagare <- "Scc_OrdenSevicioFunerarioPagare";
con <- dbConnect(odbc::odbc(), dsnDesarrollo, encoding = 'latin1');
dbWriteTable(con, SCC_OrdenServicioFunerarioPagare, dfPagareFuente, append = TRUE);
dbDisconnect(con);
My dataframe dfPagareFuente only has one row at moment (just for test), the data:
(0 <dbl>, 3 <dbl>, 2214 <dbl>, 56239 <dbl>, 2275 <dbl>, 2600 <dbl>, '2017-01-05' <dttm>, 3 <dbl>)
But when I tried to run my R code, I got the error:
Error in result_insert_dataframe(rs#ptr, values) :
nanodbc/nanodbc.cpp:1587: 22003: [Microsoft][ODBC SQL Server Driver]Valor numérico fuera del intervalo (Numeric value out of range)
Anyone can give me a clue about what am I doing wrong or any solution to this?
Always thank you.
By the way, I'm using the DBI and odbc library for R.
Finally I solved it.
The problem was the FechaPago field. In the table it has date data type, but in R I was trying to insert a datetime value. So I had to cast the values with as.Date in R.
I was able to disccovered when I deleted column by column to identify the problem.
Thank's Steven for your answer.
Not sure if it is relevant in this case - but in my case only date was there but was getting error - I solved by converting from Posxit to normal date format using as.Date()

Use sql function in cakePHP beforeSave

I have been having trouble trying to convert a column from 'varchar' to 'money'.
Apparently the default schema is wrong. When I try to save a field for PriceRule, this is the error I get:
SQL Error: Disallowed implicit conversion from data type varchar to
data type money, 'PriceRule', column
'UnitPrice'. Use the CONVERT function to run this query.
Accordingly, I try to use the convert function to convert the field datatype.
In My Model:
public function beforeSave($options = array()){
$UnitPrice = $this->data["PriceRule"]["UnitPrice"];
$this->data["PriceRule"]["UnitPrice"] = 'CONVERT(money,$UnitPrice)';
}
This is the SQL it generates:
UPDATE [pricerule]
SET [pricepolicyid] = 1,
[producttypeid] = 1,
[unitprice] = 'CONVERT(money,$UnitPrice)',
[minquantity] = 1,
[maxquantity] = 4,
[modifiedby] = 1055,
[modifieddate] = '2016-08-16 11:18:11',
[active] = '1'
WHERE [pricerule].[priceruleid] = 62
This query does not run because the CONVERT function is created as a string, not a function.
I have been continuously trying to find work-arounds and have done endless digging on google and cannot seem to find anything helpful.
If anyone has a way to fix this, that would be great! Even an alternative, any help is awesome!
Thanks in advance!
Just convert to money in php like this
$UnitPriceInMoney = money_format('%.2n',$UnitPrice);
PHP money_format

Why does my Entity Framework turn '2.87' into just '2' (decimal field)?

I verfied that the correct value '2.87' is coming into the service.. and accord to the EF diagram the type for the 'Score' field is 'Decimal'... But in the database it says just '2'
[OperationContract]
public void AddHighScore(string strName, decimal dScore, int iLevel)
{
using (SQL2008R2_789485_punkouterEntities1 dc = new SQL2008R2_789485_punkouterEntities1())
{
HighScore oHighScore = new HighScore();
oHighScore.Level = iLevel;
oHighScore.Name = strName;
//oHighScore.Name = dScore.ToString();
oHighScore.Score = dScore;
dc.AddToHighScores(oHighScore);
dc.SaveChanges();
}
}
-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------
-- Creating table 'HighScores'
CREATE TABLE [dbo].[HighScores] (
[Id] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(max) NOT NULL,
[Score] decimal(18,0) NOT NULL,
[Level] int NOT NULL
);
GO
You need to set the scale on the decimal field. Change the Score field to be decimal(18,2)
See Decimal help File on MSDN
You can set the Scale in EF by first selecting the field, then in the properties window you will see a property for Scale (see image)
decimal(18,0) means a decimal number with 18 digits to the left of the decimal point, and 0 to the right.
Therefore, your value is being stored as 2. I suggest using decimal(18,2) or similar, which allows 18 digits, with up to 2 to the right of the decimal point.
Change your column datatype to Money (instead of decimal) and regen your edmx.

How to create dynamic WHERE select without COALESCE

I have found out that my SQL 2008 R2 database is really struggling with COALESCE function if used within search.
CODE:
where
i.id_categ = COALESCE(#id_categ, i.id_categ )
and i.id_brand = COALESCE(#id_brand , i.id_brand )
and i.id_model = COALESCE(#id_model , i.id_model )
and i.id_type = COALESCE(#id_karoseria, i.id_type )
and i.id_fuel = COALESCE(#id_palivo, i.id_fuel )
and (i.year between #year_from and #year_to)
and (i.price between #price_from and #price_to)
DYNAMIC variables:
ALTER PROCEDURE [dbo].[spInzeratSelect]
#id_categ int = null,
#id_brand int = null,
#id_model int = null,
#id_fuel int = null,
#id_type int = null,
Search should work with or without these variables.
Benchmark:
with COALESCE = Total Execution Time: 3582
without COALESCE conditions = Total Execution Time: 13
You get the difference ...
Is there a nice solution how to ignore COALESCE and create dynamic SQL select with different approch ?
Thanks.
tIn your very specific case you should replace all your COALESCE search parameters with the following pattern:
AND ( (#id_brand IS NULL) OR (i.id_brand = #id_brand) )
etc
The parameter is evaluated as a literal before execution, so doing it this way makes the condition sargable.
This is functionally equivalent to your query except you're first checking against the literal value, which can be optimized away if it is, in fact, null.
EDIT: Apparently this is the approach recommended in #Joe Stefanelli's link as well. I originally poached it from Erland Sommerskog.
EDIT2: And I always forget to mention OPTION (RECOMPILE) too.

SQL Server 2005 Point In Polygon

I have a polygon structure in an sql2005 db as described below.
CREATE TABLE [dbo].[Polygons](
[PolygonID] [int] IDENTITY(1,1) NOT NULL,
[PolygonName] [varchar](255) NOT NULL,
[PolygonColor] [varchar](7) NOT NULL,
[PolygonRuleID] [int] NOT NULL)
CREATE TABLE [dbo].[Polylines](
[LineID] [int] IDENTITY(1,1) NOT NULL,
[LineX1] [float] NOT NULL,
[LineY1] [float] NOT NULL,
[LineX2] [float] NOT NULL,
[LineY2] [float] NOT NULL,
[PolygonID] [int] NOT NULL
)
Now I retrieve whole lines to application and put all to hit testing function.
public static bool PointInPolygon(float pointX, float pointY, PolylineCollection polygon)
{
int nvert = polygon.Count();
int i, j = 0;
bool c = false;
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
if (((polygon[i].LineY1 > pointY) != (polygon[j].LineY1 > pointY)) &&
(pointX < (polygon[j].LineX1 - polygon[i].LineX1) * (pointY - polygon[i].LineY1) / (polygon[j].LineY1 - polygon[i].LineY1) + polygon[i].LineX1))
c = !c;
}
return c;
}
But I need to move this function to sql server. But Sql 2005 doesn't have native spatial functions and I dont want to use any extra spatial functionality libraries. How can I port this function to T-SQL? :) Or anyone have different solution to PointInPolygon check?
Thanks
You can look at this page, it provides SQL code:
SQL code Point In Polygon
You didn't rule out going with sql 2008 which has built-in geospatial types. I haven't used it so I can't offer anything beyond that.
SQL Server 2005 allows you to write native functions for the CLR that can execute server side. You can read the MSDN intro Using CLR Integration in SQL Server 2005. This should allow you to have your function implemented as an addition to sql server and run at native speeds.
You could re-write PointInPolygon as a stored proc with a cursor.
I have to admit that I don't fully get your algorithm to test for point hitting. Anyways, the data structure is odd for a polygon, since X1/Y1 of a line have to be equal to X2/Y2 of the previous line in order to form a polygon. Therefore, I'd store single points only in order to make the data structure guaranteed to be consistent, and the last and the first point are interconnected again.
As for an algorithm for finding whether a point is inside the (2D) polygon or not, I'd first filter the lines which are candidates and create a "cut" (horizontal or vertical) so that I get a list of line intersection points and order them. Then, using a rank function, it is inside the polygon if the rank is odd, if it even we're outside the polygon (or in a "hole").