How to change a Date Column into two separate columns - sql-server

i have an excel file that and i want to extract the date from it and load it into a dimension table but as month and year only.
I want to do this through an SSIS package.
for example i have 6/15/2002
and i want to take only 6/2002
because in my dimension table i have only month and year
what should i do. please help me, it's my final project
it something like this question
How to change a Date Column into two separate columns, MONTH and YEAR

You could use Derived Column transformation to do that. The output from the excel source could remains as it is. Take that output through the Derived Column TF, add two new columns as Add new columns, and then the easiest way is to convert the Date to string varchar or char (DT_STR or DT_WSTR based on your table schema), then based on your criteria, using substring function to form each new column.
For example: Derived Column 1, <add as new column>, SUBSTRING( (DT_STR, 20, 1252)Your_Column , 0, 1 ) + SUBSTRING( (DT_STR, 20, 1252)Your_Column , 6, 4 ) for your request
UPDATED
(DT_WSTR, 20)MONTH(YOUR_COLUMN)+ "/" + (DT_WSTR, 20)YEAR(YOUR_COLUMN)
UPDATED 2nd
Just like before, but for the Derived Column 1, <add as new column>, (DT_WSTR, 20)MONTH(YOUR_COLUMN) as MONTH expression.
Derived Column 2, <add as new column>, (DT_WSTR, 20)YEAR(YOUR_COLUMN) as YEAR expression.
You could rename Derived Column 1 or Derived Column 2 to any name you want for later identification.

Related

How to make a computed column a persisted one?

I'm using SQL Server 2008 R2. I want to create an auto-generated customer id whenever a new customer is registered. The customer id should be in the following format
Customer ID =
current year (4 digit) +
current month (2 digit) +
unique number (4 digit).
Example: 2012055001
I have written a query as follows:
create table tb
(
id int identity(1000, 1),
cust_id as CONVERT(VARCHAR(4), DATEPART(YYYY, GETDATE())) +
SUBSTRING(CONVERT(nvarchar(6), GETDATE(), 112), 5, 2) +
CONVERT(VARCHAR(4), id) persisted primary key
);
But I'm getting the following error:
Computed column 'cust_id' in table 'tb' cannot be persisted because the column is non-deterministic.
How do I rectify this problem?
You're confusing structure with data. You're trying to create your table with data as the structure type, and that clearly can't work.
The column name would be cust_id, but the column type has to be one of the valid SQL Server data types (in this case most likely CHAR(10), as it's a fixed width and there are no Unicode characters possible).
CREATE TABLE tb(id int identity(1000, 1), cust_id Char(10));
Your auto-generated data would then be inserted into the column, most likely by an ON INSERT trigger. (How to create that trigger is a totally different topic; you can find information in most SQL tutorials or books, or in SQL Server documentation.)

Appending data in SQL Server

Question: I have a table that has a column called DOB. In it is well... dates of birth, but in a format such as 04/22/1987. I'd like to change them to 19870422, which would entail moving the 4 digit year to the front of the cell and deleting the / marks and leaving no spaces.
Any suggestions in an efficient was to do this would be much appreciated. thanks everyone!
If your table is called Foo and your column name is DOB:
Update Foo
Set DOB = Substring(DOB, 7, 4) + Substring(DOB, 1, 2) + Substring(DOB, 4, 2);
Here's a SQL Fiddle.
Note that this will update the DOB column for all rows in your table. So, be certain that all of your data is in the format MM/DD/YYYY before changing it to YYYYMMDD.
Note that the Substring() function takes three parameters:
Input (often text, but could be binary)
Starting point (which is one-based)
Total number of characters to return.
So, the expression Substring(DOB, 7, 4) starts at the seventh position (the beginning of the year) and takes four characters (the four-character year.)
Select Convert(char(8),dob,112) dob
So you want to update all rows within the date column basically updating the existing columns
UPDATE YourTable SET YourNewColumn = CONVERT(DATETIME,YourOldColumn,112)

how to know the format of a date

hi i am trying to update my tables consisting of dates i am encountering errors due to white spaces the date fields was given a varchar type by the pass programmer i want to convert my dates to this format mm/dd/yyyy but it seems the date fields are littered with this type of date APRIL 8, 2014 the question is how would i know that the date being updated are in this format sample APRIL 8, 2014? before updating it to proper format e.g mm/dd/yyyy
i have tried this SELECT convert(datetime, date_field, 101) but still it gives error
any suggestions?
The date and time datatypes in SQL Server do not have any implicit format. They are stored in an internal representation. The formatting is only applied when they are presented to a user on-screen or in a report etc. To store dates in a varchar() column was a mistake by the previous programmer which you should not repeat. Add a new column to your table of type date (http://msdn.microsoft.com/en-us/library/bb630352.aspx). Copy values from the current varchar to this new column, converting as you go. Both these statements work
select CONVERT(date, 'APRIL 8, 2014')
select CONVERT(date, '04/08/2014')
To be sure these are the only combinations you should do some analysis on the current values using regular expressions (outside of SQL) or SUBSTRING() (inside SQL).
Drop the old varchar and rename the new date column. You may have to drop and re-create constraints, indexes and foreign keys etc.
When passing these values back to the application keep them in date format. Let the user interface or reporting application format the date however it needs to.

SSIS Using Derived column to reference column from same table

I am trying to import an xls spreadsheet into a table, and one of the columns needs to be derived from a different column in the same table. I would like to do this during the import phase rather then creating a SQL Task to do it after the import. I am horrible when it comes to creating expressions in SSIS, so this is probably an easy task - but I just can't get it right.
DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,columndata)+1,0))
That is the expression that I am trying to use.
You want the following:
DATEADD("S",-1,DATEADD("M",DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))+1,(DT_DATE)0) )
1) You need to use " to surround your dateparts.
2) You need to cast your 0 date to an explicit date type.
This expression takes the column dateserial number [dataDate] adds that number of days to the date -2
DATEADD("D",[dataDate],(DT_DATE)-2)
(apparently sql day 0 and excel day 0 are different by 2 days on my versions (sql2008r2 and excel 2007)) and finds the number of months from date 0 to that date,
DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))
then adds 1 to that number of months and adds it back to date 0
DATEADD("M",DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))+1,(DT_DATE)0)
(to get the beginning of the next month following the column date value), Then it subtracts 1 second from the date to get the last second of the month of the column date value in the datetime format.
DATEADD("S",-1,DATEADD("M",DATEDIFF("M", (DT_DATE) 0,DATEADD("D",[dataDate],(DT_DATE)-2))+1,(DT_DATE)0) )

SQL date related

Hi all i want to take record from table named Tblbatch where batch starting date should be from augest 2007 to july 2010...
I want to fetch such records which came in between this two dates
Select * from Tblbatch where startDate between '01-08-2007' and '31-07-2010'
provided you have a datetime column "startDate"
Note : that using between includes both the dates specified.
If you want to avoid the dates either change the boundary dates to + - 1 respectively or use > and < conditions

Resources