Force computed column to int - sql

I have this table created with [TankNumId] as a computed column.
How can I make the data type be forced to int. It keeps setting as nvarchar, which is the data type of the column [TankNum].
The values in [TankNum] column are 100-1, 100-2 100-3, etc. Hence why I am using the computed column to convert the '-' to '.' so that it can be a valid int in the [TankNumId] column. Any suggestions?
CREATE TABLE [dbo].[tblTank9](
[Id] [int] IDENTITY(1,1) NOT NULL,
[TankNumId] AS (replace([TankNum],'-','.' )) PERSISTED NOT NULL,
[TankNum] [nvarchar](10) NULL,
[CompanyName] [nvarchar](30) NULL,
[Ft] [float] NULL,
[Inch] [float] NULL,
[HFt] [smallint] NULL,
[HIn] [smallint] NULL,
[HFx] [smallint] NULL,
[GaPt] [char](100) NULL,
CONSTRAINT [PK_tblTank9] PRIMARY KEY CLUSTERED
(
[TankNumId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

replace function receive nvarchar and return nvarchar
You need CAST or CONVERT your result to float because 100.1 isnt integer
SELECT CAST(YourVarcharCol AS float) FROM Table
SELECT CONVERT(float, YourVarcharCol) FROM Table

Related

Performance issue with geometry table in SQL Server

I am on SQL Server 11. I have the following table that stores the boundary of all LGA in NSW
CREATE TABLE [dbo].[nsw_lga_polygon_shp](
[id] [int] IDENTITY(1,1) NOT NULL,
[geom] [geometry] NULL,
[lg_ply_pid] [nvarchar](15) NULL,
[dt_create] [date] NULL,
[dt_retire] [date] NULL,
[lga_pid] [nvarchar](15) NULL,
[nsw_lga_sh] [date] NULL,
[nsw_lga__1] [date] NULL,
[nsw_lga__2] [nvarchar](100) NULL,
[nsw_lga__3] [nvarchar](100) NULL,
[nsw_lga__4] [date] NULL,
[nsw_lga__5] [nvarchar](15) NULL,
CONSTRAINT [PK_nsw_lga_polygon_shp] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
SET IDENTITY_INSERT [dbo].[nsw_lga_polygon_shp] ON;
The table only have 198 rows
When I try to map the result against one of my table that have 36,531 rows, I notice that it have very serious performance issue, it told 5 mins to generate 500 rows.
select
* ,
(select top 1
nsw_lga__2
from
[nsw_lga_polygon_shp]
where
Geom.Filter(geometry::STGeomFromText('point(' +
convert(varchar(100),longitude_GIS )+
' ' +
cast(latitude_GIS as varchar(100))+ ')',4283)) = 1
) LGA
from
Report_A
Is there anyway I can may it run faster? Can I index the geometry column?
Thanks in advance!

There are more than three data in one of the cells that are bit formats

I need to have Select up to 3 company Types in SQL and use check constrain.
How to do this, or I also accept other suggestions., For the following table:
CREATE TABLE Dbo.[CompanyType](
[TypeID] [bigint] IDENTITY(1,1) NOT NULL,
[Manufacturer] [bit] NULL,
[Trading] [bit] NULL,
[BuyingOffice] [bit] NULL,
[Agent] [bit] NULL,
[Wholesaler] [bit] NULL,
[Commission] [bit] NULL,
[Association] [bit] NOT NULL,
[BusinessService] [bit] NOT NULL,
[Other] [bit] NOT NULL,
[Photo] [image] NULL,
[CreateDate] datetime
CONSTRAINT [PK_CompanyType] PRIMARY KEY CLUSTERED
(
[TypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE dbo.[CompanyType] ADD CONSTRAINT [DF_Company_PersianTax] DEFAULT ((9)) FOR [Taxpercent]
GO
ALTER TABLE dbo.[CompanyType] ADD CONSTRAINT [DF_Company_CreateDate] DEFAULT (getdate()) FOR [CreateDate]
GO
using a check constrain.
How to use check constrain for this model??
I tried by :
In the meantime I use SP to insert. Isn't it better to check for three or more true data there? At the moment of data entry as a parameter check
--parameter validation
if(#Manufacturer+#Trading+#BuyingOffice+#Agent+#Wholesaler+#Commission+#Association+#BusinessService+#Other)<= 3
Begin
Return
End
else
insert statement (...)
Using trigger like following query! check inserted, sum of column <= 3 rolback
CREATE TRIGGER usp_checksum3
ON dbo.companytype
AFTER insert
AS
BEGIN
SET NOCOUNT ON;
if (Select [Manufacturer]+[Trading]+[BuyingOffice]+[Agent]+[Wholesaler]+[Commission]+[Association]+[BusinessService]+[Other]
From inserted) <= 3
Return
END
Thanks for your tips
Consider using the following check constraint:
CREATE TABLE Dbo.[CompanyType](
[TypeID] [bigint] IDENTITY(1,1) NOT NULL,
[Manufacturer] [bit] NULL,
[Trading] [bit] NULL,
[BuyingOffice] [bit] NULL,
[Agent] [bit] NULL,
[Wholesaler] [bit] NULL,
[Commission] [bit] NULL,
[Association] [bit] NOT NULL,
[BusinessService] [bit] NOT NULL,
[Other] [bit] NOT NULL,
[Photo] [image] NULL,
[CreateDate] datetime
CONSTRAINT [PK_CompanyType] PRIMARY KEY CLUSTERED ([TypeID] ASC)
WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY],
CONSTRAINT [PK_Company_Types] CHECK (
CAST([Manufacturer] AS INT)
+ CAST([Trading] AS INT)
+ CAST([BuyingOffice] AS INT)
+ CAST([Agent] AS INT)
+ CAST([Wholesaler] AS INT)
+ CAST([Commission] AS INT)
+ CAST([Association] AS INT)
+ CAST([BusinessService] AS INT)
+ CAST([Other] AS INT)
<= 3)
You don't need to implement additional logic in your stored procedure. The check constraint guarantees integrity, and apply equally to inserts performed from the SP or outside.
If you want to modify the existing table:
ALTER TABLE Dbo.[CompanyType]
ADD CONSTRAINT [Chk_CompanyType] CHECK (
CAST([Manufacturer] AS INT)
+ CAST([Trading] AS INT)
+ CAST([BuyingOffice] AS INT)
+ CAST([Agent] AS INT)
+ CAST([Wholesaler] AS INT)
+ CAST([Commission] AS INT)
+ CAST([Association] AS INT)
+ CAST([BusinessService] AS INT)
+ CAST([Other] AS INT)
<= 3)

performance issue in retrieving data from a varbinary(MAX) field

how i can improve the performance of the sq statement which contains Varbinary(Max) datatype
Table Structure
CREATE TABLE [dbo].[Table_1](
[EmpID] [numeric](18, 0) NOT NULL,
[SrNo] [numeric](18, 0) NOT NULL,
[Type] [varchar](10) NULL,
[FileName] [varchar](100) NULL,
[D1] [varchar](50) NULL,
[D2] [varchar](50) NULL,
[Data] [varbinary](max) NULL,
[CreatedBy] [varchar](50) NULL,
[CreatedOn] [datetime] NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[EmpID] ASC,
[SrNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
the Select Query takes around 2 to 3 seconds to select a single record with
Data
Checked Indexing of the Table

Conversion failed when converting date and/or time from character string. What could be wrong?

INSERT INTO dbo.SaleNew(ENQ_AID,DOCKET_NO,SALE_TYPE,VEHICLE_MODEL,SALE_DATE,BOOKING_DATE,DELIVERY_DATE,
DEALER_NAME,ENQ_GEN_BY,EXEC_NAME,USER_CR,DATE_CR) SELECT '6','2','0','TEST','2016-05-01','2016-05-10','2016-05-15',
'ABC','S I','V B','1',SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
What could be wrong with this query?
Also tried:
INSERT INTO dbo.SaleNew(ENQ_AID,DOCKET_NO,SALE_TYPE,VEHICLE_MODEL,SALE_DATE,BOOKING_DATE,DELIVERY_DATE,
DEALER_NAME,ENQ_GEN_BY,EXEC_NAME,USER_CR,DATE_CR) SELECT '6','2','0','TEST',CONVERT(DATE,'01/05/2016',103),CONVERT(DATE,'10/05/2016',103),
CONVERT(DATE,'15/05/2016',103),'ABC','S I','V B','1',SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
This is the create table query:
CREATE TABLE [dbo].[SaleNew](
[SALE_ID] [int] IDENTITY(1,1) NOT NULL,
[ENQ_AID] [bigint] NOT NULL,
[DOCKET_NO] [varchar](50) NOT NULL,
[VEHICLE_MODEL] [varchar](100) NOT NULL,
[SALE_DATE] [date] NULL,
[BOOKING_DATE] [date] NULL,
[DELIVERY_DATE] [date] NULL,
[DEALER_NAME] [date] NULL,
[ENQ_GEN_BY] [varchar](100) NULL,
[EXEC_NAME] [varchar](100) NULL,
[USER_CR] [int] NULL,
[DATE_CR] [date] NULL,
[USER_UP] [int] NULL,
[DATE_UP] [date] NULL,
[SALE_TYPE] [int] NOT NULL,CONSTRAINT [PK_SaleNew] PRIMARY KEY CLUSTERED (
[SALE_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON ) ON [PRIMARY]
What could be the reason?
DEALER_NAME type is defined as DATE but you want to INSERT a VARCHAR type value ABC in it.
Execute following query in order to edit the mentioned column type, then Execute your INSERT query.
ALTER TABLE [dbo].[SaleNew]
ALTER COLUMN [DEALER_NAME] VARCHAR(50) NULL

Date time index sql server 2005

I know this is a common topic but i stil feel my scenario requires some custom advice. I was selecting from a table the other day and it took me an AGE to select on a datetime column that has not been indexed. I want to index this, only problem ebing is that the "production" 2005 box i am unfamiliar with and how it will handle the index creation. With that in mind i am wondering what the safest way for me to create an index on the table is. All the details i think people will need are below (i hope) :
Index to be created on field 5 (possibly field4 also)
Table 1 definition:
CREATE TABLE [dbo].[TABLE 1](
[ID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_BLA] DEFAULT (newid()),
[field1] [nvarchar](7) NULL,
[field2] [nvarchar](10) NULL,
[field3] [nvarchar](2) NULL,
[field4] [datetime] NULL,
[field5] [datetime] NULL,
[field6] [smallint] NULL,
[field7] [nvarchar](1) NULL,
[field8] [nvarchar](7) NULL,
[field9] [nvarchar](60) NULL,
[field10] [smallint] NULL,
[field11] [nvarchar](15) NULL,
[field12] [datetime] NULL,
CONSTRAINT [PK_ID] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Total count of rows in Table 1 : 2926836