Im not that familiar with MS Access SQL commands. Can anyone tell me what is wrong with my create statement below?
Create Table Extract( [WeekID] TEXT(10) , [RetailerID] INT , [ItemCode] INT , [SaleType] TEXT(1) , [Multiple] INT , [Store] INT , [DateCollected] DATETIME , [Price] DECIMAL(9,4) )
There are two main problems with this statement.
Firstly, Extract is a reserved word in Access SQL. That means you have to surround it by [] brackets.
Secondly, the DECIMAL data type is only supported in Access SQL when executing SQL in ANSI mode. You can see how to switch your database to ANSI mode in this answer, or you can execute your query using the following line in the immediate window:
CurrentProject.Connection.Execute "Create Table [Extract]( [WeekID] TEXT(10) , [RetailerID] INT , [ItemCode] INT , [SaleType] TEXT(1) , [Multiple] INT , [Store] INT , [DateCollected] DATETIME , [Price] DECIMAL(9,4) )"
Furthermore, I recommend using NVARCHAR over TEXT in queries. There's no functional difference as long as you specify a field size smaller than 255, but TEXT usually refers to the Long Text data type, a harder to work with data type that can take very long strings.
Related
When I run this SQL statement:
SELECT commentor, comment, cdate, ddate
FROM delivery_comments
WHERE Jobno = '93388-01' and ddate = '2016-03-11'
ORDER BY cdate
I get this error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '93388-01' to data type int.
I'm not trying to convert it to an int. I want it to stay varchar. How do I do this?
I am totally agree with #xQbert, why are you comparing type of varchar with int along with '-' (hyphen) when its sure that int never has such value...
you can do the comparison like as follows :
Cast(Jobno As Varchar(20)) = '93388-01'
still you never get the result as '-' (hyphen) never exists in your data.
I have a large table in prod with INT datatype and it is both primary key and identity column(1,1).The number of rows is 2147479257.
During the daily job run the ETL failed because of arithmetic overflow error,as it cant fit any more rows in to the destination table.
Can you please how can i change the column to unsigned int.
Change the datatype to bigint.
bigint -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)
You've overflown an int, which means you have approximately 2B rows in the table. By switching to an unsigned int, you hope to gain another 2 billion rows of addressable space. The problem with this approach is that SQL Server does not support an unsigned int as a data type.
Your gut reaction might be to reach for Greg's approach of changing to a bigint data type. The challenge with this approach is that while your processing is dead in the water, you take the quick fix and change to bigint, any other consumer of that table is now going to fail. I went through this in 2011, by the way. We fixed the database only to have all the reporting and .NET applications fail. At that job, it'd have been far less catastrophic to have queued up the processing for a N days while we gave the appearance of normalcy than to remove all doubt by having every external facing application fail.
With the general implementation of an identity column, you can get an easy another 2B without making a single code change - simply set the identity value to the lower bound and you've bought yourself sufficient time to plan the migration to bigint. The command for this is dbcc checkident
You'll also likely want to ensure the identity column is specified as a unique value. People often set the identity column as a primary key but otherwise, you'd run code similar to the following.
SET NOCOUNT ON;
IF EXISTS
(
SELECT * FROM sys.tables AS T INNER JOIN sys.schemas AS S ON S.schema_id = T.schema_id WHERE S.name = 'dbo' AND T.name = 'IntOverfloweth'
)
BEGIN
DROP TABLE
dbo.IntOverfloweth;
END
CREATE TABLE
dbo.IntOverfloweth
(
IntOverflowethID int IDENTITY(2147483647,1) NOT NULL
, SomeValue varchar(30)
);
INSERT INTO
dbo.IntOverfloweth
(SomeValue)
OUTPUT
Inserted.*
VALUES
('Before');
BEGIN TRY
INSERT INTO
dbo.IntOverfloweth
(SomeValue)
OUTPUT
Inserted.*
VALUES
('Overflow');
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH
-- Push the pointer back around to the begining
DBCC CHECKIDENT('dbo.IntOverfloweth', RESEED, -2147483648);
-- Ensure uniqueness
CREATE UNIQUE INDEX UQ_IntOverfloweth
ON dbo.IntOverfloweth
(
IntOverflowethID
);
INSERT INTO
dbo.IntOverfloweth
(SomeValue)
OUTPUT
Inserted.*
VALUES
('Does not Overflow');
I want user to input only 11 digits , if not, an error should be occured on sql, what type of variable should I use in my sql code.
My current code :
st.executeUpdate("CREATE TABLE User(idUser numeric(11,0) not NULL,uName char(30), uSurname char (30), uAddress varChar(500), primary key(idUser) )");
If you want to make this kind of check on db side in MySQL you can use a trigger
CREATE TRIGGER tg_bi_user
BEFORE INSERT ON user
FOR EACH ROW
SET NEW.idUser = IF(NEW.idUser BETWEEN 10000000000
AND 99999999999, NEW.idUser, NULL);
The trick is to violate NOT NULL when NEW.idUser not in a proper range. If you are on MySQL 5.5 or higher you can also use SIGNAL.
Here is SQLFiddle demo. Try to uncomment the last insert statement and see it won't succeed.
If the variable is numeric, you can use a check constraint:
CREATE TABLE User(idUser numeric(11,0) not NULL,uName char(30), uSurname char (30),
uAddress varChar(500), primary key(idUser),
CHECK(idUser > 9999999999 AND idUser <= 99999999999) )'
Though you'll want to check if the datatype numeric accepts numbers large enough. If not you could to use varchar
I am writing a flexible search mechanism for a customer's website. I am utilizing union clauses to query a number of different fields in the database in search of a string value entered by the user. This works fine except for one issue.
When comparing a string of a text to an integer that is currently set to zero, the match always returns true. In other words, according to MySQL, "email#example.com" is equal to 0.
I have tried utilizing the CAST and CONVERT function to turn this into a standard string to string comparison, but I can't seem to get the syntax right. My attempts either repeat the above issue or return no rows at all when some should match. I am also concerned that doing this would have an effect on performance since I am combining lots of unions.
What I really need is a strict comparison between an entered string and the value in the database, be it an integer or string.
EDIT:
Here is an example.
CREATE TABLE `test_table` (
`id` INT NOT NULL AUTO_INCREMENT ,
`email` VARCHAR(255) NOT NULL ,
`phone` BIGINT(19) NOT NULL DEFAULT '0' ,
PRIMARY KEY (`id`) )
ENGINE = MyISAM;
INSERT INTO `test_table` (`id`, `email`, `phone`) VALUES (1, 'email#example.com', 0);
SELECT * FROM test_table WHERE phone = 'email#example.com';
Execute this and the one row that has been inserted will return. My issue is that it shouldn't!
This query should fail:
SELECT * FROM test_table WHERE cast(phone as char) = 'email#example.com';
The cause of the original problem is that when comparing strings and numbers, it converts the string to a number (so you can write where phone = '123'). You need to use an explicit cast of the field to make it a string-to-string comparison, to prevent this default conversion.
Unfortunately, casting like this is likely to prevent it from using indexes. Even if the field is already char, the cast apparently prevents it from indexing.
You could also solve it during input validation: if phone is an integer, don't allow the user to provide a non-integer value in the search field.
How about replacing:
SELECT * FROM test_table WHERE phone = 'email#example.com'
with:
SELECT * FROM test_table WHERE phone = 'email#example.com' and phone <> 0
<> means different from.
This will work for you because you are using 0 in the phone column to mean there isn't a phone number (although it would be better style to use NULL for no phone number).
I am using sql server 2008. I want to keep my PK's in the form 001,002 etc. MS SQL is not allowing me to do that. How can i stop it from converting 001 to 1 ?
Use varchar datatype instead of Int data type.
or
Keep your Pk as int and when use in a query convert it in varchar and then use Left padding 00X on it.
Declare #id int
Set #id = 1
Select RIGHT(REPLICATE('0',2) + CAST(#id AS VARCHAR(3)),3)