Showing posts with label format. Show all posts
Showing posts with label format. Show all posts

Tuesday, March 27, 2012

Altering column fields with a Stored Procedure

I have some columns of data in SQL server that are of NVARCHAR(420)
format but they are dates. The dates are in DD/MM/YY format. I want to
be able to convert them to our accounting system format which is
YYYYMMDD. I know the format is strange but it will make things easier
in the long run if all of the dates are the same when working between
the 2 different databases. Basically, I need to take a look at the
year portion (with a SUBSTRING function maybe) to see if it is greater
than 50 (there will not be any dates that are less than 1950) and if
it is concatenate 19 with it (ex. 65 = 1965). Then, concatenate the
month and day from the rest to form the date we need in NUMERIC(8).
So, a date of January 17, 2003 (currently in the format of 17/01/03)
would become 20030117. In VB, the function I would write is something
like the following:
/*
Dim sCurrentDate as String
Dim sMon as string
Dim sDay as String
Dim sYear as String
Dim sNewDate as String

sCurrentDate = "17/01/03"
sMon = Mid(sCurrentDate, 4, 2)
sDay = Mid(sCurrentDate, 1, 2)
sYear = Mid(sCurrentDate, 7, 2)

If sYear < 50 Then
sYear = "20" & sYear
ElseIf sYear > 50 Then
sYear = "19" & sYear
End if
sNewDate = sYear & sMon & sDay
*/
I was thinking of doing this in a Stored Procedure but am really rusty
with SQL (it's been since college).

The datatype would end up being NUMERIC(8). How I would write it if I
new how to write it would be: grab the column name prior to the
procedure, create a temp column, format the values, place them into
the temp column, delete the old column, and then rename the temp
column to the name of the column that I grabbed in the beginning of
the procedure. Most likely this is the only way to do it but I have no
idea how to go about it.mwoodward@.quinnpumps.com (Milo Woodward) wrote in message news:<1615a5e3.0308142112.30d6548c@.posting.google.com>...
> I have some columns of data in SQL server that are of NVARCHAR(420)
> format but they are dates. The dates are in DD/MM/YY format. I want to
> be able to convert them to our accounting system format which is
> YYYYMMDD. I know the format is strange but it will make things easier
> in the long run if all of the dates are the same when working between
> the 2 different databases. Basically, I need to take a look at the
> year portion (with a SUBSTRING function maybe) to see if it is greater
> than 50 (there will not be any dates that are less than 1950) and if
> it is concatenate 19 with it (ex. 65 = 1965). Then, concatenate the
> month and day from the rest to form the date we need in NUMERIC(8).
> So, a date of January 17, 2003 (currently in the format of 17/01/03)
> would become 20030117. In VB, the function I would write is something
> like the following:
> /*
> Dim sCurrentDate as String
> Dim sMon as string
> Dim sDay as String
> Dim sYear as String
> Dim sNewDate as String
> sCurrentDate = "17/01/03"
> sMon = Mid(sCurrentDate, 4, 2)
> sDay = Mid(sCurrentDate, 1, 2)
> sYear = Mid(sCurrentDate, 7, 2)
> If sYear < 50 Then
> sYear = "20" & sYear
> ElseIf sYear > 50 Then
> sYear = "19" & sYear
> End if
> sNewDate = sYear & sMon & sDay
> */
> I was thinking of doing this in a Stored Procedure but am really rusty
> with SQL (it's been since college).
> The datatype would end up being NUMERIC(8). How I would write it if I
> new how to write it would be: grab the column name prior to the
> procedure, create a temp column, format the values, place them into
> the temp column, delete the old column, and then rename the temp
> column to the name of the column that I grabbed in the beginning of
> the procedure. Most likely this is the only way to do it but I have no
> idea how to go about it.

I strongly suggest that you rethink your approach, and change the
column to datetime. You can then do date calculations using the
standard functions (DATEADD etc.), compare the values to datetime
variables without conversion, etc. You can use CONVERT() to extract
dates in a particular format for passing to other systems.

Using numeric will give you serious problems in the long run, although
I appreciate that you may have limited control over the data model.
But if you really have no option but to use numeric, then this should
work (assuming that as you said, all dates are 1950 or later):

update dbo.MyTable
set DateColumn = convert(char(8), convert(datetime, DateColumn, 3),
112)

alter table dbo.MyTable
alter column DateColumn numeric(8)

Simon

Wednesday, March 7, 2012

Alter Columns?

Hello,

I am trying to edit several tables that were imported in tab-delimited format from text files. I am trying to generate a script that will alter the data type for several different columns.

I have succesfully edited a single column with the following code:
USE THCIC
ALTER TABLE PudfTest
ALTER COLUMN
DISCHARGE VARCHAR(6) NULL

However, I have need to create a script that will change the data type for over 100 columns. So far, everything I've read tells me that multiple 'alter column' statements cannot be run in a single query. I'm hoping someone can shed some light on this, or at least point me in another direction so that I won't have to manually change the data type for every column in each of the tables.

Any help would be greatly appreciated.
Thanks!Look into this one and elaborate as needed:

select 'alter table ' + table_name + ' alter column ' + column_name + ' ' +
case data_type
when 'int' then 'varchar(25)'
when 'datetime' then 'char(10)'
else data_type
end + ' null'
from information_schema.columns

Friday, February 24, 2012

Allowing Users to Filter on a Report Column

I'm intending to email this report I've built in Excel format. Before this report was switched to SQL Reporting Services, it was hand built by someone who added in filtering in the spreadsheet. Is it possible to add that in with SQL Reporting Services? If so, how?

Thanks!

Interactive filter is not currently supported. However, you can use report parameter to simulate this.

1. Add a parameter to the report that passes in the filter value.

2. Define a drillthrough action on the column header where you want to let the user click to filter. Point the drillthrough report to be the same report.

Thursday, February 9, 2012

Alignment etc...

This is a multi-part message in MIME format.
--=_NextPart_000_0028_01C6A504.9BB3C560
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
How can I align a field with the right margin of the report? With the = Matrix, the report width varies depending on the amount of data. I can = seem to find a way to specify that a control is anchored to the right = margin. Or maybe even centered in the page..
Help?
Jerry
--=_NextPart_000_0028_01C6A504.9BB3C560
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&
How can I align a field with the right = margin of the report? With the Matrix, the report width varies depending on = the amount of data. I can seem to find a way to specify that a control = is anchored to the right margin. Or maybe even centered in the page..

Help?

Jerry
--=_NextPart_000_0028_01C6A504.9BB3C560--Hi Jerry,
Thank you for your posting!
Based on my test, you could just put the textbox next to the right border
of your report. The textbox will align with the right border of the report.
Please try and let me know the result.
Sincerely,
Wei Lu
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================This posting is provided "AS IS" with no warranties, and confers no rights.|||Thanks, Wei... But that doesn't work. Since the report is wider when it's
run than it is in the designer. And there's no way to determine, at design
time, how wide the report will, as the matrix expands dynamically based on
the criteria entered by the user at run time.
So... Is there any way to dynamically position any of the controls at run
time?
Thanks.
Jerry
"Wei Lu [MSFT]" <weilu@.online.microsoft.com> wrote in message
news:TWFXnMYpGHA.4612@.TK2MSFTNGXA01.phx.gbl...
> Hi Jerry,
> Thank you for your posting!
> Based on my test, you could just put the textbox next to the right border
> of your report. The textbox will align with the right border of the
> report.
> Please try and let me know the result.
> Sincerely,
> Wei Lu
> Microsoft Online Community Support
> ==================================================> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>|||Hi Jerry,
Thank you for the update.
You could not control the position of the control dynamically in the
report.
If the text box left border is next to the right border of the matrix, the
textbox will be position on the right of the matrix.
Hope this will be helpful.
Sincerely,
Wei Lu
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================This posting is provided "AS IS" with no warranties, and confers no rights.|||Hello Jerry,
How are you doing on this issue? Does Wei's suggestion in the last reply
helps you some? If you still have any problems or need any further
assistance, please feel free to post here.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.|||I'm not sure I understand his suggestion at all... I have a textbox just
below and aligned with the right border of the matrix...
At run time, the matrix expands to accomodate maybe 3 to 7 columns,
depending on data. But the textbox stays at the position it was at in the
designer.
I want the text box's right edge position to stay aligned with the right
edge of the matrix as it expands.
Thanks.
Jerry
"Steven Cheng[MSFT]" <stcheng@.online.microsoft.com> wrote in message
news:$TtwwejqGHA.2024@.TK2MSFTNGXA01.phx.gbl...
> Hello Jerry,
> How are you doing on this issue? Does Wei's suggestion in the last reply
> helps you some? If you still have any problems or need any further
> assistance, please feel free to post here.
> Sincerely,
> Steven Cheng
> Microsoft MSDN Online Support Lead
>
> =================================================>
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
>