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)
Related
I have two columns in SQL Server Table, one is Id and other one is Number, for example if Ids are as follows: 1189, 758, 756, I want a User defined or system defined function, that can generate numbers as: T01189, T00758, T00756.
I want to keep this function either as Default field or add it to trigger - soon after the record inserted, you can suggest me which one is better also.
You could use a padding trick here:
SELECT
Id,
'T' + RIGHT('0000' + Id, 5) AS Number
FROM yourTable;
Demo
i have a field on sybase for a particular table where i need to select only the first value. See the example below:
Field1
104676;ABC;345776;TEST
2332;ABC;345776;TEST
8765432;ABC;345776;TEST
This particular field have char format. I want to pullout only the first value (starting from the left) that is compost by different number of digits, in particular:
Field1
104676
2332
8765432
in conclusion i need to formart the field in numeric and pullout only the first value.
thanks id advance for your help.
I guess you may easily pull this using SUBSTRING/CHARINDEX function combination -
SELECT SUBSTRING(Field1, 1, CHARINDEX(';', Field1) -1)
FROM YOUR_TABLE
I am trying to add some report logic to save setting up another dataset. First thing I need is to count the number of rows per date, then number of rows per date where another column states = "Yes".
I have a raw data stored procedure which returns the data as a table, on another tab.
Number of records that match date parameter:
=SUM(IIF(Fields.DATE_FIELD.Value = Parameters!DATE_PARAM.Value, 1, 0))
Number of records that match date parameter where another column = YES:
=SUM(IIF(Fields.DATE_FIELD.Value = Parameters!DATE_PARAM.Value AND Fields.OTHER_FIELD.Value = "Yes", 1, 0))
The IIF checks the conditions and if they are TRUE then return 1 for a record else 0 then it sums these values up.
UPDATE:
I think I misunderstood what you were needing.
You want to have a count of all records by date not just for a particular date.
For that you would want a table that groups by date, then you could just use a COUNTROWS or SUM(1) for the number of records for each date.
Then you could use an IIF to check for the Yes in the other field:
=SUM(IIF(Fields.OTHER_FIELD.Value = "Yes", 1, 0))
Since your table is already grouping by date, you don't need the date in the above expression.
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.
I have a Retailer code ,It is combination of varchar and Int =>RT003880 like this
I have create a Retailer code as script-wise every day. So i need the last Retailer code i have inserted.
So I have split the Integer and varchar for finding the Max value.
This is the max value by using Query.
select SUBSTRING(Retailer_code,5,9) as RetailerCode
Into #maxfindtable
from dbo.sample
select MAX(RetailerCode) from #maxfindtable
I need to put in in function or Stored Procedure how to do this
Try:
select max(SUBSTRING(RetailerCode,3,len(RetailerCode)-2))
for RT003880 the integer part is starting from position 3,and
len(RetailerCode)-2 isdefine the length of the substring. i.e: all the
character starting from 3rd position
See SUBSTRING for more clearification.
EDIT 2:
try Using Cast
create table #tab (genId varchar(max))
insert into #tab(genId)
values('RT00031'),('RT00013232'),('RT00034'),('RT00084')
select * from #tab
select max(cast(SUBSTRING(genId,3,len(genId)-2) as int)) from #tab
I'd split the RetailerCode into two columns (one CHAR/VARCHAR and one INT/SMALLINT/NUMERIC) to be able to get some performance out of the table. Possibly use a calculated column to concatenate them if requested. I would never query on the calculated column if it was not persisted, however.
Today I have inserted 10000 new records,its choosing wrong max value. Both The codes Written Same Value,Maximum Value is RT0017898,But It shows RT0009999.
After I have Checked a sample data
RetailerId RetailerCode RetailerName
1 RT00031 mBigBazar
2 RT00034 TBazar
3 RT00084 SaravanaStore It shows Correct Value - 00084
When I have insert a new Record it shows wrongly
RetailerId RetailerCode RetailerName
1 RT00031 mBigBazar
2 RT00034 TBazar
3 RT00084 SaravanaStore
4 RT00013232 NewStore
Now it shows 00084 .why it shows wrong?