Showing posts with label dates. Show all posts
Showing posts with label dates. 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

Sunday, February 19, 2012

Allowing null dates

My users want to be able to enter nothing in a date field.

I'm using asp.net v2, vb.net, and VS 2005 for my application. I'm not sure what to do or what code to write to allow the user not to enter a date and keep from hitting the sqldatetime overflow error.

I could use some help.

Thanks

If the application variable/control value for the datatime value is empty (EmptyString), your application should set the input parameter to SqlType.dbNull -NOT the input control.text value.

|||

Thank you.

I figured out I needed to write that code in my business logic layer. I know this is an issue with a lot of people. Here is how I solved my problem.

I set the variable to Nullable (of DateTime)

And made the following check

If Not fldIntake.HasValue Then ClientApp.SetfldIntakeNull() Else ClientApp.fldIntake = fldIntake.Value

Thursday, February 9, 2012

All dates between two dates

Hi everyone,
I have two fields StartDate and EndDate.
I would like to see all dates between these two dates.
Eg.
StartDate = 5/1/2005
EndDate = 5/5/2005
Result should be:
Date
5/1/2005
5/2/2005
5/3/2005
5/4/2005
5/5/2005
Any suggestions on how this can be accompilshed.
TIA,
Martin.
From Itzik Ben-Gan
CREATE FUNCTION fn_dates(@.from AS DATETIME, @.to AS DATETIME)
RETURNS @.Dates TABLE(dt DATETIME NOT NULL PRIMARY KEY)
AS
BEGIN
DECLARE @.rc AS INT
SET @.rc = 1
INSERT INTO @.Dates VALUES(@.from)
WHILE @.from + @.rc * 2 - 1 <= @.to
BEGIN
INSERT INTO @.Dates
SELECT dt + @.rc FROM @.Dates
SET @.rc = @.rc * 2
END
INSERT INTO @.Dates
SELECT dt + @.rc FROM @.Dates
WHERE dt + @.rc <= @.to
RETURN
END
GO
SELECT dt FROM fn_dates('20030901', '20040831')
"martin" <kashaan007@.hotmail.com> wrote in message
news:eyr9yz6XFHA.2884@.tk2msftngp13.phx.gbl...
> Hi everyone,
> I have two fields StartDate and EndDate.
> I would like to see all dates between these two dates.
> Eg.
> StartDate = 5/1/2005
> EndDate = 5/5/2005
> Result should be:
> Date
> 5/1/2005
> 5/2/2005
> 5/3/2005
> 5/4/2005
> 5/5/2005
> Any suggestions on how this can be accompilshed.
> TIA,
> Martin.
>
|||You can do it easely with a calendar table or an auxiliary number table.
Why should I consider using an auxiliary calendar table?
http://www.aspfaq.com/show.asp?id=2519
Why should I consider using an auxiliary numbers table?
http://www.aspfaq.com/show.asp?id=2516
AMB
"martin" wrote:

> Hi everyone,
> I have two fields StartDate and EndDate.
> I would like to see all dates between these two dates.
> Eg.
> StartDate = 5/1/2005
> EndDate = 5/5/2005
> Result should be:
> Date
> 5/1/2005
> 5/2/2005
> 5/3/2005
> 5/4/2005
> 5/5/2005
> Any suggestions on how this can be accompilshed.
> TIA,
> Martin.
>
>
|||Here is much easier way:
declare @.startDate datetime
Declare @.EndDate datetime
set @.startDate = '5/1/2005'
set @.EndDate = '5/5/2005'
while (@.startDate < = @.EndDate)
Begin
print convert(char(12), @.startDate, 101)
set @.StartDate = dateadd(dd, 1, @.StartDate)
end
Thanks
Bindu
"martin" wrote:

> Hi everyone,
> I have two fields StartDate and EndDate.
> I would like to see all dates between these two dates.
> Eg.
> StartDate = 5/1/2005
> EndDate = 5/5/2005
> Result should be:
> Date
> 5/1/2005
> 5/2/2005
> 5/3/2005
> 5/4/2005
> 5/5/2005
> Any suggestions on how this can be accompilshed.
> TIA,
> Martin.
>
>

All dates between two dates

Hi everyone,
I have two fields StartDate and EndDate.
I would like to see all dates between these two dates.
Eg.
StartDate = 5/1/2005
EndDate = 5/5/2005
Result should be:
Date
5/1/2005
5/2/2005
5/3/2005
5/4/2005
5/5/2005
Any suggestions on how this can be accompilshed.
TIA,
Martin.From Itzik Ben-Gan
CREATE FUNCTION fn_dates(@.from AS DATETIME, @.to AS DATETIME)
RETURNS @.Dates TABLE(dt DATETIME NOT NULL PRIMARY KEY)
AS
BEGIN
DECLARE @.rc AS INT
SET @.rc = 1
INSERT INTO @.Dates VALUES(@.from)
WHILE @.from + @.rc * 2 - 1 <= @.to
BEGIN
INSERT INTO @.Dates
SELECT dt + @.rc FROM @.Dates
SET @.rc = @.rc * 2
END
INSERT INTO @.Dates
SELECT dt + @.rc FROM @.Dates
WHERE dt + @.rc <= @.to
RETURN
END
GO
SELECT dt FROM fn_dates('20030901', '20040831')
"martin" <kashaan007@.hotmail.com> wrote in message
news:eyr9yz6XFHA.2884@.tk2msftngp13.phx.gbl...
> Hi everyone,
> I have two fields StartDate and EndDate.
> I would like to see all dates between these two dates.
> Eg.
> StartDate = 5/1/2005
> EndDate = 5/5/2005
> Result should be:
> Date
> 5/1/2005
> 5/2/2005
> 5/3/2005
> 5/4/2005
> 5/5/2005
> Any suggestions on how this can be accompilshed.
> TIA,
> Martin.
>|||You can do it easely with a calendar table or an auxiliary number table.
Why should I consider using an auxiliary calendar table?
http://www.aspfaq.com/show.asp?id=2519
Why should I consider using an auxiliary numbers table?
http://www.aspfaq.com/show.asp?id=2516
AMB
"martin" wrote:
> Hi everyone,
> I have two fields StartDate and EndDate.
> I would like to see all dates between these two dates.
> Eg.
> StartDate = 5/1/2005
> EndDate = 5/5/2005
> Result should be:
> Date
> 5/1/2005
> 5/2/2005
> 5/3/2005
> 5/4/2005
> 5/5/2005
> Any suggestions on how this can be accompilshed.
> TIA,
> Martin.
>
>|||Here is much easier way:
declare @.startDate datetime
Declare @.EndDate datetime
set @.startDate = '5/1/2005'
set @.EndDate = '5/5/2005'
while (@.startDate < = @.EndDate)
Begin
print convert(char(12), @.startDate, 101)
set @.StartDate = dateadd(dd, 1, @.StartDate)
end
Thanks
Bindu
"martin" wrote:
> Hi everyone,
> I have two fields StartDate and EndDate.
> I would like to see all dates between these two dates.
> Eg.
> StartDate = 5/1/2005
> EndDate = 5/5/2005
> Result should be:
> Date
> 5/1/2005
> 5/2/2005
> 5/3/2005
> 5/4/2005
> 5/5/2005
> Any suggestions on how this can be accompilshed.
> TIA,
> Martin.
>
>

All dates between two dates

Hi everyone,
I have two fields StartDate and EndDate.
I would like to see all dates between these two dates.
Eg.
StartDate = 5/1/2005
EndDate = 5/5/2005
Result should be:
Date
5/1/2005
5/2/2005
5/3/2005
5/4/2005
5/5/2005
Any suggestions on how this can be accompilshed.
TIA,
Martin.From Itzik Ben-Gan
CREATE FUNCTION fn_dates(@.from AS DATETIME, @.to AS DATETIME)
RETURNS @.Dates TABLE(dt DATETIME NOT NULL PRIMARY KEY)
AS
BEGIN
DECLARE @.rc AS INT
SET @.rc = 1
INSERT INTO @.Dates VALUES(@.from)
WHILE @.from + @.rc * 2 - 1 <= @.to
BEGIN
INSERT INTO @.Dates
SELECT dt + @.rc FROM @.Dates
SET @.rc = @.rc * 2
END
INSERT INTO @.Dates
SELECT dt + @.rc FROM @.Dates
WHERE dt + @.rc <= @.to
RETURN
END
GO
SELECT dt FROM fn_dates('20030901', '20040831')
"martin" <kashaan007@.hotmail.com> wrote in message
news:eyr9yz6XFHA.2884@.tk2msftngp13.phx.gbl...
> Hi everyone,
> I have two fields StartDate and EndDate.
> I would like to see all dates between these two dates.
> Eg.
> StartDate = 5/1/2005
> EndDate = 5/5/2005
> Result should be:
> Date
> 5/1/2005
> 5/2/2005
> 5/3/2005
> 5/4/2005
> 5/5/2005
> Any suggestions on how this can be accompilshed.
> TIA,
> Martin.
>|||You can do it easely with a calendar table or an auxiliary number table.
Why should I consider using an auxiliary calendar table?
http://www.aspfaq.com/show.asp?id=2519
Why should I consider using an auxiliary numbers table?
http://www.aspfaq.com/show.asp?id=2516
AMB
"martin" wrote:

> Hi everyone,
> I have two fields StartDate and EndDate.
> I would like to see all dates between these two dates.
> Eg.
> StartDate = 5/1/2005
> EndDate = 5/5/2005
> Result should be:
> Date
> 5/1/2005
> 5/2/2005
> 5/3/2005
> 5/4/2005
> 5/5/2005
> Any suggestions on how this can be accompilshed.
> TIA,
> Martin.
>
>|||Here is much easier way:
declare @.startDate datetime
Declare @.EndDate datetime
set @.startDate = '5/1/2005'
set @.EndDate = '5/5/2005'
while (@.startDate < = @.EndDate)
Begin
print convert(char(12), @.startDate, 101)
set @.StartDate = dateadd(dd, 1, @.StartDate)
end
Thanks
Bindu
"martin" wrote:

> Hi everyone,
> I have two fields StartDate and EndDate.
> I would like to see all dates between these two dates.
> Eg.
> StartDate = 5/1/2005
> EndDate = 5/5/2005
> Result should be:
> Date
> 5/1/2005
> 5/2/2005
> 5/3/2005
> 5/4/2005
> 5/5/2005
> Any suggestions on how this can be accompilshed.
> TIA,
> Martin.
>
>

All dates between the start and end date

Table Data/Structure

Name

StartDate

EndDate

HrsWorked

Rick

01/01/2006

08/01/2006

8

Mike

02/01/2006

11/01/2006

8

How can I create a report that will display all dates between the StartDate and the EndDate and in the column underneath the date, put "8 hrs worked."

Jan0106

Jan0206

Jan0306

Jan0406......

8

8

8

8

0

0

0

0......Till Mike's start date in February

Thanks

-Rob

You're talking about generating data thats not contained in but can be derived from the dataset.

Your best bet is to do this in your query and return a row for every day and person. This looks like a job for a stored procedure or table valued function that will populate a temporary / in-memory table and return that as the dataset for the report.