dbTalk Databases Forums  

forming a string with dateparts

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss forming a string with dateparts in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
rgintexas
 
Posts: n/a

Default forming a string with dateparts - 08-30-2007 , 10:31 AM






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


Reply With Quote
  #2  
Old   
Piero 'Giops' Giorgi
 
Posts: n/a

Default Re: forming a string with dateparts - 08-30-2007 , 11:12 AM






On Aug 30, 8:31 am, rgintexas <rgarth... (AT) yahoo (DOT) com> wrote:
Quote:
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



Reply With Quote
  #3  
Old   
Dan Guzman
 
Posts: n/a

Default Re: forming a string with dateparts - 09-03-2007 , 08:31 AM



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" <rgarth_98 (AT) yahoo (DOT) com> wrote

Quote:
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




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.