forming a string with dateparts
am 30.08.2007 17:31:57 von rgintexas
I want to create a string using the week number and year of a date.
For instance, if i have a date of 1/3/2007, which would be week 1 of
2007, i want to create a string that says 'Wk1-200707'.
i know it's basically 'WK' + CAST(DATEPART(WK,'1/3/2007') AS VARCHAR)
+ '-' +CAST(DATEPART(YEAR,'1/3/2007') AS VARCHAR) but it's not quite
working for me. Any idea of what I'm missing?
Thanks in advance.
Richard Garth
Re: forming a string with dateparts
am 30.08.2007 18:12:15 von giorgi.piero
On Aug 30, 8:31 am, rgintexas wrote:
> I want to create a string using the week number and year of a date.
> For instance, if i have a date of 1/3/2007, which would be week 1 of
> 2007, i want to create a string that says 'Wk1-200707'.
> i know it's basically 'WK' + CAST(DATEPART(WK,'1/3/2007') AS VARCHAR)
> + '-' +CAST(DATEPART(YEAR,'1/3/2007') AS VARCHAR) but it's not quite
> working for me. Any idea of what I'm missing?
Besides using the date in the YYYYMMDD format, no.
Works perfectly for me... What's the problem?
P
Re: forming a string with dateparts
am 03.09.2007 15:31:25 von Dan Guzman
You might get different week values than you expect if DATEFORMAT is set to
DMY instead of MDY. As Piero alluded, specifying date literals in the ISO
YYYYMMDD format will ensure the date is interpreted correctly regardless of
DATEFORMAT settings.
Another possibility is that DATEFIRST is set to an unusual value like 2 or 3
(Tuesday or Wednesday) instead of a common value like 7 or 1 (Sunday or
Monday).
--
Hope this helps.
Dan Guzman
SQL Server MVP
"rgintexas" wrote in message
news:1188487917.464338.181530@y42g2000hsy.googlegroups.com.. .
>I want to create a string using the week number and year of a date.
> For instance, if i have a date of 1/3/2007, which would be week 1 of
> 2007, i want to create a string that says 'Wk1-200707'.
> i know it's basically 'WK' + CAST(DATEPART(WK,'1/3/2007') AS VARCHAR)
> + '-' +CAST(DATEPART(YEAR,'1/3/2007') AS VARCHAR) but it's not quite
> working for me. Any idea of what I'm missing?
> Thanks in advance.
> Richard Garth
>
>