I have a table with over 70 million rows which I want to partition using the date/time column, however the date/time column has varchar datatype instead of date.
What is the best way of converting the datatype dynamically to be able to use the column as a partition range ?
Thanks
I suggest you add a new PERSISTED COMPUTED column and then create partition on the computed field.
The computed column should be type of date and it is simply conversion of your varchar column.
I have done this before as experiment and it works.
Related
I am running code that creates a record on a table in SQL Server 2014. The record is coming from my PHP backend that has an insert statement with new values. The table contains a 'reference_number' column that is supposed to be a concatenation of the first two letters of the 'type' column and an incrementing 7 digit number.
Now I could have done this easily playing with the column properties if it was just an incrementing integer value. So is there a way in SQL Server 2014 using functions or stored procedures (etc...) to give the column a generated value every time a new record is created?
For example I can use the built-in getDate() function every time I need to fill a datetime valued column. So can I write a function similar that accepts a parameter as the column value I am providing in the insert statement for the 'type' column and let the code do the rest? How?
Notes:
I am new to SQL server, although I know and have a background on the basics of databases in general.
Writing the code in PHP isn't a choice as its a specifically expected requirement I have to achieve.
Would love to hear new approaches/examples or recommendations on how to solve this.
Thanks so much!
you need an Identity column
alter table myTable add column Sequencenumber
int identity (1000000, 1) not null
^ ^
Start at this value | |
| //increment by this every time
Your reference number can then be a computed column using the Identity column and your Type column.
for example
alter table myTable add
Reference_number as
left(type,2) + cast(Sequencenumber as varchar(10))
See the documentation here Identity, and computed columns here computed columns
I'm using Visual Studio connected to my SQL Server to create a new database and populate a table with some mock data for application development testing. I created a table with 5 fields, an auto-increment PK, three nvarchar(50) fields and a date. When I view the table data and attempt to add a row, it doesn't allow me to type into the Date field nor give me any way to insert a date into the field. How can I accomplish this?
I was not descriptive enough and it turns out it was a confusion between a timestamp and a datetime datatype. I was trying to use timestamp thinking when I did an insert it would give the CURRENT_TIMESTAMP. As it turns out the timestamp is really rowversion and has nothing to do with an actual datetime. I have since changed the datatype to datetime and have had no problems.
With libfq and the C API, is there a way to get the nullable metadata of a column from a query?
I'm just talking about the property of a column, not the NULL value from a resultset.
For instance, with MySQL:
mysql_fetch_field_direct()
does the job.
I tried with PQfmod, but without success.
Information about the table definition is not part of the result set data.
You can find out if the value is NULL, but not if the column is nullable or not.
A column in a result set need not be related to a certain column in a table!
To get information about a column's definition, query information_schema.columns. The is_nullable column will contain the information.
I need to convert column which is of data type varchar and it is primary key .Now i need to convert into date data type which has multiple dates (mmddyy) while loading from other column in sql server.
To be clear I created Table B has Column DTB which is date datatype and is primary key now i want to load data into this column DTB from Table A column DTA which is varchar and here the records are of form 12717(mm/dd/yy) 20k records need to be loaded of such different dates.
can you please help me in converting.
Thanks in Advance.
The right date format style of "mm/dd/yy" is 1 for the convert function
SELECT convert(date, '01/15/18', 1)
Now you can insert data into table B from A
INSERT INTO tableB (DTB, ...)
SELECT convert(date, DTA, 1) AS DTB, ... FROM tableA
I use SSIS in SQL Server 2016. I have to create a new column in my DataFlow. I use DrivedColumn component to create this new column. In this case I need a nullable numeric column like below image :
As you can see, I have error in this case.
How can I create null-able numeric in Derived column component?
In the derived column task there are NULL Functions that you can use if you would like to set a value to NULL.
Select the appropriate NULL function that corresponds to the datatype of your column and plug that into your equation.
You can use NULL(DT_CY) if your column is defined as currency.
Example. ([currencyAmount]>0? [currencyAmount]:NULL(DT_CY)
In the Derived Column task Null Functions can be found here: