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

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

Related

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

Sql server database store procedure query

I have two tables as below:
1) UserFavouriteCurrencies
CREATE TABLE [dbo].[UserFavouriteCurrencies](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[UserId] [int] NULL,
[CurrencyId] [int] NULL,
[EntryDate] [datetime] NULL,
CONSTRAINT [PK_UserFavouriteCurrencies] 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]
GO
Above table is holding data of user favorite companies
2) CurrencyRates
CREATE TABLE [dbo].[CurrencyRates](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[CurrencyId] [int] NULL,
[Price] [float] NULL,
[Open_24h] [float] NULL,
[Volume_24h] [float] NULL,
[Low_24h] [float] NULL,
[High_24h] [float] NULL,
[Volume_30d] [float] NULL,
[BestBid] [float] NULL,
[BestAsk] [float] NULL,
[TradeId] [bigint] NULL,
[PriceDate] [datetime] NULL,
[PriceDateTimestamp] [bigint] NULL,
[OpenInterest] [float] NULL,
CONSTRAINT [PK_CoinbaseTickerRatesMaster] 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]
GO
Above table holds rates details of currencies.
Call : EXEC sp_Getdata 1(Here 1 in UserId of UserFavouriteCurrencies table)
I need all records for particular currencies last latest records in one query as below
DECLARE #Today DATETIME = GETDATE(), #PrevDate DATETIME = GETDATE()<br>
SELECT TOP 1 Price, PriceDate, Volume_24h, PriceDateTimestamp
FROM CurrencyRates
WHERE CurrencyId = #CurrencyId AND PriceDate <= #Today
ORDER BY PriceDateTimestamp DESC
Above query is only select data for one particular currency but I need the data related to user which added on UserFavouriteCurrencies table.
Please help me to write store procedure for this query
Thanks in advance.
For each currency in UserFavouriteCurrencies Table, consider the query below
SELECT
uc.Id -- This is the userId
,cr.Price
,cr.PriceDate,
,cr.Volume_24h
,cr.PriceDateTimestamp
FROM
UserFavouriteCurrencies AS uc
INNER JOIN CurrencyRates AS cr -- See Hints : 1
ON cr.CurrencyId = uc.CurrencyId
WHERE
PriceDate <= GETDATE()
AND
uc.Id = #Id
-- You can add other Predicates Here...
Hints:
Consider using LEFT OUTER JOIN if user's currency may not exist in CurrencyRates
!This will however introduce NULLs which you must handle

Force computed column to int

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

How to copy column Data of one Table to another

I am using the code below:
Insert into JV(PurchaseID)
select PurchaseID from PurchaseReturnDetail
But when I execute the query it gives an error:
Cannot insert the value NULL into column 'column1', table 'database.table1'; column does not allow nulls.
Then my INSERT fails. What could be the issue?
The error says the table has a non-null constraint for the column. To change the column to allow nulls, run this query (for Oracle DB):
alter table TABLE_NAME modify (COLUMN_NAME null);
If first columns is identity column, make sure that auto increment is enabled for that column .
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[JV](
[JVID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[JVInvoiceNo] [nvarchar](50) NULL,
[JVDate] [datetime] NULL,
[StoreID] [numeric](18, 0) NULL,
[BusinessID] [numeric](18, 0) NULL,
[UserID] [numeric](18, 0) NULL,
[Currency] [nvarchar](50) NULL,
[Rate] [decimal](18, 10) NULL,
[DueDate] [datetime] NULL,
[Reference] [nvarchar](50) NULL,
[RefID] [numeric](18, 0) NULL,
[Narration] [nvarchar](max) NULL,
[InvoiceRef] [nvarchar](50) NULL,
[Status] [nvarchar](50) NULL,
[Type] [nvarchar](50) NULL,
[PurchaseID] [numeric](18, 0) NULL,
CONSTRAINT [PK_JV] PRIMARY KEY CLUSTERED
(
[JVID] 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

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