Showing posts with label dates. Show all posts
Showing posts with label dates. Show all posts

Thursday, March 29, 2012

Combining the dates

Hi Guys

I have got two columns- one is the year and the other is the month number. I need to combine these two columns so that they form a date

For Ex

Year Month CombinedColumn

2000 11 2000/11

2003 01 2003/01

I am using SQL Server 2005

Thanks

I'll assume that Year and Month are integers and CombinedColumn is of data type DATETIME. If other data types are used, this solution might not apply.

Code Block

CREATE TABLE #Temp

(

[Year] INT NOT NULL,

[Month] INT NOT NULL,

[CombinedCol] DATETIME NULL

)

INSERT INTO #Temp VALUES (2000, 11, NULL)

INSERT INTO #Temp VALUES (2003, 01, NULL)

UPDATE #Temp

SET [CombinedCol] = DATEADD(month, [Month] - 1, DATEADD(year, [Year] - 1900, '1900-01-01 00:00:00.000'))

SELECT * FROM #Temp

|||

you can try this..

select cast( (cast(yearValue as varchar(4))+cast(monthValue as varchar(2)) + '01') as datetime)

you will get the date as the first day of your year and month values...

|||

Harish has the right idea, but it should be noted that 2000/11 is not a valid date. You can just concatenate them for display, but as for making them a date, can you explain more what you will do with the dates once you have them concatenated? (unless just making them the first day of the month suffices, of course Smile

|||

My query would be like select cast('20001101' as datetime) which sql server implicitly converts to datetime value from varchar if it is in yyyymmdd format. The user needs to decide which value he needs for the dd value in the string

|||

Here it is,

Code Block

Create Table #sampledata (

[Year] varchar(4),

[Month] varchar(2)

);

Insert Into #sampledata Values('2000','11');

Insert Into #sampledata Values('2003','01');

DECLARE @.UserDay varchar(2);

Set @.UserDay = '5'

Select Convert(Datetime, Year + Month + substring(cast((cast(@.UserDay as int) + 100) as varchar),2,2),112) [Output] from #sampledata

|||

Another method with fewer keystrokes

Code Block

SELECT CAST(LTRIM([Year] * 10000 + [Month] * 100 + @.UserDay) AS DATETIME)

FROM #sampledata

|||Is it bad to use implicit conversion? I believe it will be faster than explicitly handling the conversion.|||

Well, I think it is better to use explicit conversion. That way you leave no room for anyone for interpretation and avoid any ambiguities.

|||

Just a note: "Partial Dates" like "January, 1968" are not really supported. Basically in SQL, if you really want to work with dates, you must store a full date...."January 1, 1968" for example. SQL will infer the time component as being Midnight and store it as "01 Jan 1968 00:00" (if you're using smalldatetime) and "01 Jan 1968 00:00:00.000" using DateTime.

So, for your query, let's add a day-of-the-month component:

Code Block

select convert(smalldatetime,'01' + '/' + Month + '/' + Year) as ThisIsTheDate from

or...if the values are stored as numbers (which is not suggested in your example of "01" which would probably be "1" if it were stored as a number:

Code Block

select convert(smalldatetime,"01/" + right('00',convert(varchar,month),2) + '/' + convert(varchar,year))

from

|||

Thanks .It works fine

Cheers

|||

Actually, here's one that I like better:

Note that DateAdd(year,50,0) = 'January 1, 1950' and DateAdd(month, 5, 0) = 'June 1, 1900' so....

Code Block

CREATE TABLE #Temp

(

[Year] INT NOT NULL,

[Month] INT NOT NULL,

[CombinedCol] DATETIME NULL

)

INSERT INTO #Temp VALUES (2000, 11, NULL)

INSERT INTO #Temp VALUES (2003, 01, NULL)

select dateadd(month, [Month]-1,dateadd(Year,[year]-1900,0))

from #Temp

|||

I came up with the same approach, but after you. One little thing...rather than specify the "base date" with a string of "1900-01-01 00:00:00.000") you can simply use the numeric 0

Code Block

select DateAdd(month, 10, 0)

select DateAdd(month, 10, "1900-01-01 00:00:00.000")

Thursday, March 22, 2012

Combine two queries - help please

I have a table that has two dates in it, a date opened and a date
closed. I would like to create one query to give me the number of
records that have been opened each month plus, and this is the hard
part the number of those records that have been closed each month. I
can get the result with two seperate queries but have been unable to
get it combined into one query with three values for each month, i.e.,
the month, the number opened and the number of those that were opened
in the month that have been subsequently closed.

Here's my two queries. If anyone can help I'd appreciate.

SELECT COUNT(*) AS [Number Closed], LEFT(DATENAME(m, DateOpened),
3) + '
' + CAST(YEAR(DateOpened) AS Char(5)) AS [Month Opened]
FROM table
WHERE (DateClosed IS NOT NULL)
GROUP BY CONVERT(CHAR(7), DateOpened, 120), LEFT(DATENAME(m,
DateOpened), 3)
+ ' ' + CAST(YEAR(DateOpened) AS Char(5))
ORDER BY CONVERT(CHAR(7), DateOpened, 120)

SELECT COUNT(*) AS [Number Opened], LEFT(DATENAME(m, DateOpened),
3) + '
' + CAST(YEAR(DateOpened) AS Char(5)) AS [Month Opened]
FROM table
GROUP BY CONVERT(CHAR(7), DateOpened, 120), LEFT(DATENAME(m,
DateOpened), 3)
+ ' ' + CAST(YEAR(DateOpened) AS Char(5))
ORDER BY CONVERT(CHAR(7), DateOpened, 120)

TIA

BillTry:

SELECT MIN(dateopened),
COUNT(*),
COUNT(dateclosed)
FROM YourTable
GROUP BY YEAR(dateopened), MONTH(dateopened)

--
David Portas
SQL Server MVP
--|||David;

Thank you very much that works just fine. I appreciate the help.

Cheers;

Bill

Monday, March 19, 2012

combine data from different records with same ID

I have a table contains comments. User scan create as many comments they wa
nt.
my job is to combine and rearrange all comments in order of dates and time.
acct date time Comments
-- -- -- ---
08 01/04/2001 170852 0Conveyed stips.
84 01/04/2001 173740 test!
84 01/04/2001 173812 test2!
02 01/04/2001 180502 spoke to mbr and nd
01 01/05/2001 115548 joint life
01 01/05/2001 115550 Please fund loan.
18 01/05/2001 185220 Sent
18 01/05/2001 185238 Sent completed application
Desired Result:
acct Comments
----
--
08 Conveyed stips. 01/04/2001: 170852
84 test! - Ford 01/04/2001: 173740 test2! 01/04/2001: 173812
02 spoke to mbr and nd 01/04/2001: 180502
01 joint life 01/05/2001: 115548 Please fund loan. 01/05/2001: 1155
50
18 Sent 01/05/2001: 185220 Sent completed application 01/05/2001:
185238
Thanks in Advance,
CulamUse a document management system (textbase)and not SQL system.|||You haven't stated what datatypes these columns are.
Do type conversions as required and use the concatenation operator ( + ) to
achieve the results you want. What seems to be the difficulty in doing so?
Anith|||I converted all the data to VARCHAR and using a operator (+) to combine data
,
but I need to roll up all records with same id into one record. That is
what I need help in.
"Anith Sen" wrote:

> You haven't stated what datatypes these columns are.
> Do type conversions as required and use the concatenation operator ( + ) t
o
> achieve the results you want. What seems to be the difficulty in doing so?
> --
> Anith
>
>|||I see. This does not seem to be a right job for SQL Server. One good
approach to such problems is to retrieve the resultset and leverage the
string concatenation and loop-like functionality of a client programming
language to create the result.
The approaches in SQL are all more or less complex and cumbersome. Some of
the such hacks can be found at:
http://groups.google.ca/groups?selm...FTNGP09.phx.gbl
Anith

Sunday, February 19, 2012

Column Heading in Crosstab

I have a crosstab that contains dates grouped by week as the column. Since it is grouped by week it gives Sunday as the start date of the week. Does anyone know how I can change it so that it will show Monday as the beginning of the week?Create a formula having this code
weekdayname(weekday({datefield})+1)
Now Group the report by this formula|||I tried this formula in my crosstab and it seperated the columns into Monday, Tuesday, Wednesday etc. I need it to show the dates. For example, what it shows across the top now is:

6/5/05 6/12/05 6/19/05 6/26/05

what I need it to show is the week starting on Monday:

6/6/05 6/13/05 6/20/05 6/27/05

Any help would be appreciated.

Sunday, February 12, 2012

Collecting Active Directory dates

Hi,

I've just started using ADSI to pull in info from our Active Directory
tree into SQL Server 2000. I've made the link ok, and can pull in most
fields (cn whenCreated etc) fine. However, there are some date fields
(the one I'm interested in is pwdLastSet) that are represented as a
long numeric string, which throws up an error when SQL tries to pull it
in. Is there an easy way to parse these fields into a standard
datetime field, or if not how do I force SQL to pull the numeric field
in, and convert it later?

TIA,
Ross"Ross Luker" <ross_luker@.hotmail.com> wrote in message
news:1104940631.745926.294600@.c13g2000cwb.googlegr oups.com...
> Hi,
> I've just started using ADSI to pull in info from our Active Directory
> tree into SQL Server 2000. I've made the link ok, and can pull in most
> fields (cn whenCreated etc) fine. However, there are some date fields
> (the one I'm interested in is pwdLastSet) that are represented as a
> long numeric string, which throws up an error when SQL tries to pull it
> in. Is there an easy way to parse these fields into a standard
> datetime field, or if not how do I force SQL to pull the numeric field
> in, and convert it later?
> TIA,
> Ross

It would probably be a good idea to give an example of one of the numeric
strings, and the date it represents - personally, I'm not at all familiar
with ADSI, although others here may be. I'm also not sure how you're pulling
the data - if you're using DTS, you could use a custom ActiveX
transformation, if the existing date transformation won't handle it; if
you're using a linked server, then a UDF might be one solution.

Simon|||Hi Simon,

If I look at AD data using the windows LDIFDE tool, there are some
fields such as the one below, which are retrieved ok:
whenChanged: 20041202105508.0Z - MSSQL formats this fine to 02/12/2004,
10:55

However, most of the date/time fields are in the format:
pwdLastSet: 127463655814071600
which I'm guessing is a counter in (maybe) seconds from some date, but
I can't find any info to prove this! Trying to pull this field in (I'm
using a linked server) results in an error "Could not convert the data
value due to reasons other than sign mismatch or overflow". As I said,
if I knew more about what the data in the fields are, I might be able
to work on transforming it!

Ross|||"Ross Luker" <ross_luker@.hotmail.com> wrote in message
news:1104943553.231483.173640@.c13g2000cwb.googlegr oups.com...
> Hi Simon,
> If I look at AD data using the windows LDIFDE tool, there are some
> fields such as the one below, which are retrieved ok:
> whenChanged: 20041202105508.0Z - MSSQL formats this fine to 02/12/2004,
> 10:55
> However, most of the date/time fields are in the format:
> pwdLastSet: 127463655814071600
> which I'm guessing is a counter in (maybe) seconds from some date, but
> I can't find any info to prove this! Trying to pull this field in (I'm
> using a linked server) results in an error "Could not convert the data
> value due to reasons other than sign mismatch or overflow". As I said,
> if I knew more about what the data in the fields are, I might be able
> to work on transforming it!
> Ross

It looks unlikely to be seconds since an epoch, since the number above would
be more than 4 billion years (I think - very quick calculation). You should
probably follow up on the ADSI side - in an AD newsgroup, perhaps - to find
out what the number represents.

Until you find out more details, you could use ISDATE() to put in a null (or
something else) for your import - it's not always reliable, but in this case
it should be OK:

select cast(case when isdate(pwdLastSet) = 0 then null else pwdLastSet end
as datetime) as pwdLastSet
from ADSI..LinkedTable

Simon