Make Date Function (like In VB)

Dec 11, 2002

I hate manipulating dates in SQL. One of the many things Access and VB handles much better!

I wanted a function like DateSerial(Year,Month,Day) so I created one.

One caveat: The Year must be >= 1800. But all other numbers can be most any int value that results in a valid date.

Which means you can do:

MDate(Year(@d),1,1) (first day of date @d)
MDate(Year(@d), Month(@d) + 1, -1) (last day of month of date @d)
MDate(2000,1,1) (create a date quickly and easily w/o convert)
MDate(Year(@d)+1,1,1) (get first day of next year for date @d)

..etc... whatever you can do with VB's DateSerial() function, except for the year must be >=1800.

Or, does this exist already in SQL and I'm missing it somewhere??

Is there an easier/better way to do this?

* * * *

Create function MDate(@Year int, @Month int, @Day int)
returns datetime
AS
BEGIN
declare @d datetime;
set @d = dateadd(year,(@Year - 1800),'1/1/1800');
set @d = dateadd(month,@Month - 1,@d);
return dateadd(day,@Day - 1,@d)
END

- Jeff

Edited by - jsmith8858 on 12/12/2002 14:04:03

View 15 Replies


ADVERTISEMENT

Want To Make ''Get Latest Status'' Function More Efficient

May 14, 2007

Hey folks, I'm looking at making the following query more efficientpotentially using the ranking functions and I'd like some advice fromthe gurus. The purpose of the following is to have a status for aperson, and also have a historical background as to what they've done,status wise. This was the best way I could come up with to do this afew years back, but I'm wondering if there's a better way with Sql2005.Here's a toned down structure and my query. Any help/critique wouldbe greatly appreciated.CREATE TABLE #Status(StatusID int NOT NULL,StatusName VARCHAR(50) NOT NULL,StatusCategoryID int NOT NULL) ON [PRIMARY]CREATE TABLE #RegStatus([RegistrationID] [uniqueidentifier] NOT NULL,[StatusID] [int] NOT NULL,[StatusTimeStamp] [datetime] NOT NULL,[UniqueRowID] [int] IDENTITY(1,1) NOT NULL) ON [PRIMARY]SET NOCOUNT onINSERT INTO #Status VALUES(200, 'StatusA', 1)INSERT INTO #Status VALUES(210, 'StatusB', 1)INSERT INTO #Status VALUES(115, 'StatusC', 1)INSERT INTO #Status VALUES(112, 'StatusD', 1)INSERT INTO #Status VALUES(314, 'StatusE', 1)INSERT INTO #Status VALUES(15, 'StatusF', 1)INSERT INTO #Status VALUES(22, 'StatusG', 1)INSERT INTO #Status VALUES(300, 'StatusX', 2)INSERT INTO #Status VALUES(310, 'StatusY', 2)INSERT INTO #Status VALUES(320, 'StatusZ', 2)INSERT INTO #RegStatus VALUES('7A6058D0-06CB-4E83-A8C4-B1AFC74B11F0',200, GETDATE())INSERT INTO #RegStatus VALUES('7A6058D0-06CB-4E83-A8C4-B1AFC74B11F0',210, GETDATE())INSERT INTO #RegStatus VALUES('7A6058D0-06CB-4E83-A8C4-B1AFC74B11F0',115, GETDATE())INSERT INTO #RegStatus VALUES('8B94A666-A3DD-4CB1-89A0-9910047AE7A0',112, GETDATE())INSERT INTO #RegStatus VALUES('8B94A666-A3DD-4CB1-89A0-9910047AE7A0',314, GETDATE())INSERT INTO #RegStatus VALUES('8B94A666-A3DD-4CB1-89A0-9910047AE7A0',200, GETDATE())INSERT INTO #RegStatus VALUES('8B94A666-A3DD-4CB1-89A0-9910047AE7A0',22, GETDATE())INSERT INTO #RegStatus VALUES('58AF0FC6-C900-4BD0-B3F7-F9D62701F021',15, GETDATE())INSERT INTO #RegStatus VALUES('58AF0FC6-C900-4BD0-B3F7-F9D62701F021',115, GETDATE())INSERT INTO #RegStatus VALUES('58AF0FC6-C900-4BD0-B3F7-F9D62701F021',200, GETDATE())INSERT INTO #RegStatus VALUES('58AF0FC6-C900-4BD0-B3F7-F9D62701F021',115, GETDATE())SET NOCOUNT Off/*This is a query from within a function that I use to not only get thelateststatus for one registrant, but I can use it to get the latest statusfor everyone as well.*/DECLARE @RegStatusCatID int,@RegID UNIQUEIDENTIFIERSET @RegStatusCatID = 1SET @RegID = nullselect LS.*, S.StatusName, S.StatusCategoryIDfrom #Status Sjoin(select RS.RegistrationID, RS.StatusID, RS.StatusTimeStampfrom #RegStatus RSjoin(SELECT RS.RegistrationID , max(RS.UniqueRowID) UniqueRowIDFROM #RegStatus RSjoin #Status Son RS.StatusID = S.StatusIDand S.StatusCategoryID = @RegStatusCatIDand (@RegID is nullor (@RegID is not nulland RS.RegistrationID = @RegID))group by RS.RegistrationID)LSon RS.UniqueRowID = LS.UniqueRowID) LSon S.StatusID = LS.StatusID--SELECT * FROM #RegStatusDROP TABLE #RegStatusDROP TABLE #Status

View 4 Replies View Related

SQL 2012 :: Use Date Trunc Or Date Function To Select Date Range For Month On Month View

Jul 29, 2015

My goal is to select values from the same date range for a month on month view to compare values month over month. I've tried using the date trunc function but I'm not sure what the best way to attack this is. My thoughts are I need to somehow select first day of every month + interval 'x days' (but I don't know the syntax).In other words, I want to see

Select
Jan 1- 23rd
feb 1-23rd
march 1-23rd
april 1-23rd
,value
from
table

View 9 Replies View Related

Transact SQL :: Best Way To Make A Function For Summing Arbitrary Number Of Times Values

Jun 22, 2015

How is the best way to make a function for summing an arbitrary number of times values (table parm?)- I 've read it's necessary to convert to seconds, sum then convert back, but Im' wondering if there's an alternative.

Here's the example I want to sum:
00:02:01:30
00:01:28:10
00:01:01:50
00:06:50:30
00:00:01:50

View 8 Replies View Related

How To Make A Date Entry

May 27, 2007

First, let me say I'm really new (and probably dangerous) with SQL.

I want to take a column in my database called "publish_up" and add 4 days to the date and put it in a column in the database called "publish_down." The publish_up date is the date to start publishing and the publish_down is the date to stop publishing. A Joomla! mambot will then come by and archive the items with a publish_down date that matches today's date.

I'm pretty sure my server has SQL 5 if that's an issue.

Thanks for any advice or help you can offer.

View 6 Replies View Related

Make Date From Several Strings

Sep 23, 2006

Hello all

Someone gave me this:

Month Day Year Hour Min Sec
9 9 2006 15 9 36

And I need to make a descent date format out of it which looks like this:

Sun Sept 09 15:09:36 CEST 2006

I can concatenate the whole thing but I'm stuck with the "sunday" part.

I'm sure there are some experst out there who know how to do this is in 1 minute;)

Regards

Worf

View 5 Replies View Related

Make Sql Function To Modify Data Like Sql Procedure To Improve Code Structure And Readability.

Aug 27, 2007

Now Sql Function can not modify data as Sql Procedure like other database, it's very troublesome at most case!
in most case, Sql Function is used to improve code structure then it can be maintanced easy! BUT only because it can not modify data, there are 2 troublesome way:

1. Make all callers in the path from Sql Functions to Sql Procedure. and the coder will cry, the code will become very
confusional , and very difficult to maintance.

2. Divide the Sql Function into a thin CLR wrapper to call a Sql Procedure, can only use another connection, BUT can not be in the same transaction context And the code is ugly and slow.

You should not give limitation to Sql Function, should just limit the using context of Sql Function!

The sql code is more difficult to read and maintance than norm code(C#), then the improving code structure and readability should be one of your most important task, that's why microsoft!


View 6 Replies View Related

Date Function - Conversion Failed When Converting Date And / Or Time From Character String

Mar 18, 2014

I have the following

Column Name : [Converted Date]
Data Type : varchar(50)

When I try and do month around the [Converted Date] I get the following error message

“Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.”

My Query is

SELECT
month([Created Date])
FROM [FDMS_PartnerReporting].[Staging].[Salesforce_MarketingReporting]

View 7 Replies View Related

How To Make The Order By Date Fine Enough In SQL

Sep 21, 2006

hi,I was pulling up a report in SQL, and I wanted the records to be ordered by dates descending. However, I found this ordering was only fine enough to order records by dates (not hours or minutes) (within the same date, records were ordered so that the latest entered were at the bottom). I wonder if anyone else has encouted this problem before, or I am doing something wrong.Thanks very much.

View 2 Replies View Related

WHAT DO I USE TO MAKE A TODAY DATE QUERY

Jul 23, 2005

select no_dossier from dbo.membre where date_MAJ = GETDATE()select no_dossier from dbo.membre where date_MAJ = '2005-07-21'Both should give me the same result, yes or no?thanks

View 1 Replies View Related

WHAT DO I USE TO MAKE A TODAY DATE QUERY?

Jul 23, 2005

select no_dossier from dbo.membre where date_MAJ = GETDATE()select no_dossier from dbo.membre where date_MAJ = '2005-07-21'Both should give me the same result, yes or no?thanks

View 11 Replies View Related

Default Date(current Date) Function W/ Update?

Mar 10, 2008

I have a reference table that currently has no web front-end. It's a small table(<10 rows) that's not going to change very often (maybe once every few months).

We manually update rows on the table via the GUI table interface in Enterprise Mgr., not in T-SQL.

What I'd like to do is have SQL Server automatically update the "Last_Modified" column with the current timestamp. I can do it on an Insert using the GetDate() function, but if I update a row, this doesn't work.

Is there a function I can use that can auto-populate for both insert and updates?

View 4 Replies View Related

Certificate Start Date Is Tomorrow - How To Make It Today?

Aug 16, 2006

Hi,

I use MS certificate server to request/make server certs but the "not before", or start date is tomorrow for a 1 year cert. I dont care how long but I want the cert to start immediately (today).

Regards,

Simon.

View 7 Replies View Related

Transact SQL :: Based On Data Convert Datatype And Make Wrong Date As NULL

Apr 21, 2015

In the below scenario we are inserting some time related fields in Temp table.But its data type is varchar. Once data loading is finished in the temp table (Data is loading by source team SQOOP and they are unable to load if the source datatype is having Date or datetime) we have to alter the column datatypes. somehow, some character data in inserted in date columns (look at into 3rd insert statement). while altering the table it is failing. Can we do any alternative for this (Means if any varchar data that is non convertible to date can we make as null)

INSERT INTO ##TEMP_TEST
SELECT '2014-09-30','2017-10-06','Nov  6 2014  6:11AM','Nov  6 2014  6:11AM'
UNION SELECT '2014-09-29','2017-10-06','Nov  6 2014  6:11AM','Nov  6 2014  6:11AM'
UNION SELECT '2014-09-28','2017-10-06','Nov  6 2014  6:11AM','Nov  6 2014  6:11AM'
GO
INSERT INTO ##TEMP_TEST SELECT NULL,NULL,NULL,NULL 

[Code] ....

View 6 Replies View Related

Date Function In C#

Feb 19, 2007

hi experts,
i'm working in a web page for some statistics and i have a calendar where the customer can choose a day a week or a month and according to the date he select i need to query the database according to the date selected.
what i want to know is how can i store the date for a day in a variable so i can call it from a stored procedure, the day actually is easy what i want is how can i store the whole week in a variable so i can give it to the stored procedure and query the data in the database according to the whole week may be with startday and endday
also the same problem for the whole month, any idea how can i implement that in C#?!!!
thanks

View 14 Replies View Related

Date Function.

Apr 9, 2001

I need to format the getdate() function in SQL to return only the date and year and not the timestamp.

View 2 Replies View Related

DATE FUNCTION

Feb 26, 2002

Is there any function avaibale that separates the DATE and TIME from one column having DATETIME in the following format:

2002-02-02 07:33:59.000


Any suggestion is highly appreciated.

Viv

View 2 Replies View Related

Date() Function And SQL

Oct 28, 2004

I am running a query using the MS Query Analyzer. I am inserting a large number of fields into a table, one of the fields is a date field where I enter the date (Hard Coded) that the query is run. I was wondering if it was possible to have the script get the date while it is running via the Date() function. I have tried the following:


Declare @DateVar SmallDateTime
Set @DateVar = Date()


I am getting an error on the second line, so I guess the Date() function is not native to SQL. So is there another way of accomplishing this?


Thanks

View 2 Replies View Related

Date Function

May 26, 2004

Is it possible to retrive the number of days between two dates.

I'm aware of the DateDiff function but I can't figure out how to calculate the number days between two columns containing dates in a table.

All help is welcome

Regrads OIS

View 4 Replies View Related

Date Function

Jun 5, 2008

Dear gurus,

How do get Saturdays & sundays in between the given dates.

Thanks in advance

cool...,

View 4 Replies View Related

Date Function

Feb 17, 2006

I'm trying to create a function that takes a DATETIME field and adds 30 days to it. Does anyone know the syntax for this? Thanks in advance. :)

View 2 Replies View Related

Date() Function

Apr 20, 2006

Hi all, it's me again.

I'm trying to implement a query that will show all records from three months prior to a certain date (that the user will input) and that date.

This is what I wrote:

SELECT Transactions.Date, Transactions.Details,
FROM Transactions
WHERE Transactions.DATE Between [Forms]![FormNAME]![Date] And DateAdd("M",-3,[Forms]![FormNAME]![Date]);


The idea is that the user is presented with a form which has a single text box to input the date. Then he/she clicks a button (which runs the above query) and the list is presented.

However, when I try to run it, I get back ALL records before a certain date...not just for the three months prior to it.


What am I doing wrong?

View 8 Replies View Related

DATE FUNCTION

Jun 8, 2006

HOW TO CONVERT THE VALUES IN DATETIME COLUMN FOR EX

2006-03-17 03:56:00.000
TO
2006-03-17 00:00:00.000

I HAVE TO COMPARE ONLY DATE TO EXTRACT REPORTS.

THANKS IN ADV

View 6 Replies View Related

Date Function

May 2, 2007

Dear Experts,
Actually, ineed a function to display wether the given date is working day or not............

id the given date is a sunday or saturday, then it should display like...holiday.
or else it should say working day..

please help me in this regard.......
one more thing, if the given date is sunday, then it should give the previous sunday date (minus seven days)

thank you in advance......

I'm trying with this function

CREATE FUNCTION ISOweek (@DATE datetime)
RETURNS int
AS
BEGIN
DECLARE @ISOweek int
SET @ISOweek= DATEPART(wk,@DATE)+1
-DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104')
--Special cases: Jan 1-3 may belong to the previous year
IF (@ISOweek=0)
SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1
AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1
--Special case: Dec 29-31 may belong to the next year
IF ((DATEPART(mm,@DATE)=12) AND
((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))
SET @ISOweek=1
RETURN(@ISOweek)
END


Vinod

View 1 Replies View Related

Date Function

May 8, 2007

I need to sort some data by date: my date format is looks like this
5/7/2007 11:38:54 AM. but i need to sort sort by just the first part "5/7/2007" how do i achieve this.?

Melvin Felicien
IT Manager
DCG Properties Limited

View 9 Replies View Related

DATE FUNCTION

Jan 23, 2008

i have just started working on SQL and i am trying to solve this puzzle maybe smoe one could do it.. i have to make a query such that
it will take all the transactions throughout the day sort them with the accnt number and then give total amount in the transaction, also the total amount should be > 10000 if some one can try and locate the problem with hte logic i would appreciate it..

just for instance what i did was i tried using the convert in group by and then sum(deposit) this doesnt work.. i am really confused please help.

i have written this query:

declare @datelastweek as datetime
declare @yesterday as datetime
declare @date as datetime
set @Date = Convert(varchar,GetDate() - 1,1)
SET @datelastweek = DATEADD(Day, 1, (DATEADD(Week, -1, @Date)))
SET @yesterday = dateadd(day,1,(dateadd(day,-1,@date)))


select accountno, sum(amount), convert (varchar, TransactionTime -1,1)
from deposit tb join transaction t on tb.id=t.id
where TxnTime between @datelastweek and @date
group by DATEADD(d,DATEDIFF(d,0,TxnTime),0),t.Accountno, txntime
having sum(tb.amount)>10000

Rahul

View 12 Replies View Related

Date Function

Mar 4, 2008

i have seen that date function which MVJ has created and its really very useful..

but i m getting error while selecting getdate() as @last_date in that function

SELECT
distinct date FROM
dbo.F_TABLE_DATE('1/1/1999', getdate()) AS Date

error is : Incorrect syntax near 'getdate'

what's mistake?

can anyone help me?

thanks in advance.

View 6 Replies View Related

Date Function

Mar 17, 2008

hai to all,
i need a help ,that if i give the UTCdate as parameter in my stored proc i have to get the output as getdate() likewise if i give the getdate() i have to get the o/p as Getutcdate()


Thanks in Advance

View 2 Replies View Related

Date Function

Feb 7, 2007

Hi!I have a report that needs to be run on the seventh of every month forthe dates from 6th of the previous months to the 5th of the currentmonth. For example, I have to run a report on February 7th for the01/06/2007 to 02/05/2007. Right now I am doing it manually but I wascurious if there a function or something that will give me therequired date range on the 7th of every month.Any ideas?Thanks,T.

View 1 Replies View Related

Date Function In DTS Package

Feb 4, 2004

How can I make this work against an Access table through an ODBC dsn in my DTS package? I need to subtract 1 day from the current date.

Tried this, which is incomplete of course, (need to subtract the 1 day):
WHERE (TTDateTimeIn >= DATEDIFF(dd, 1, { fn CURDATE() }))

Got this error:
[Microsoft][ODBC Microsoft Access Driver] Too few Parameters. Expected 1.

View 2 Replies View Related

Problem With The Date Function

Nov 25, 2005

How to calculate the exact date before certain number of days.Say for example want to get the date 50 days before today's date.Thanks

View 1 Replies View Related

Date And Time Function?

Feb 27, 2003

Hi,
As we know all the job system tables store date(run_date) and time(run_time) seperate feild.
I have the situation compare this run_date & run_time with another date feild column.
my datefield = 2003-02-27 08:02:01.000

and my msdb system table has
run_date =20030227
run_time =60002

How I can compare this two value?.
Anybody has any advice about this?.
Thanks,
Ravi

View 4 Replies View Related

Function Or SP For Date Conversion

Nov 9, 2005

i have a field that date stored in it, format of this date is somethin like timestamp. for example today(9Nov2005) saved as 38665.
how can i convert this value to a normal date format?
any function or sp?

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved