Sunday, March 11, 2012

Dumb Date Format Question

I want the following query to result in "2004/10".
SELECT DATEPART(yyyy, datein) & '/' & DATEPART(mm, datein) AS theDate
From myTable
However, I get the error:
Syntax error converting the varchar value '/' to a column of data type int.
Is there a better way to do this?
Also, I realize I may be asking this in the wrong forum. Is there a place
for MSSQL newbies to ask their dumb questions?
Thank you!
MatthewHi Matthew
Ask whatever you like here, just don't call anything a dumb question. That
way, you give people who have only been using the product a bit longer than
you a chance to answer questions and feel like they have learned something.
But nobody wants to feel like they can only answer DUMB questions. ;-)
& is the bitwise 'AND' operator, and its operands must be integers. Please
read about bit operations in the Books Online.
My guess is that you want to concatenate strings. In TSQL we use '+' for
concatenation.
But, it still won't work because datepart returns a numeric value. Try using
datename. Or converting to character before you concatenate.
HTH
--
Kalen Delaney
SQL Server MVP
www.SolidQualityLearning.com
"Matthew" <turn.deletethis@.alltel.net> wrote in message
news:O6wl$vfvEHA.3276@.TK2MSFTNGP15.phx.gbl...
>I want the following query to result in "2004/10".
> SELECT DATEPART(yyyy, datein) & '/' & DATEPART(mm, datein) AS theDate
> From myTable
> However, I get the error:
> Syntax error converting the varchar value '/' to a column of data type
> int.
> Is there a better way to do this?
> Also, I realize I may be asking this in the wrong forum. Is there a place
> for MSSQL newbies to ask their dumb questions?
> Thank you!
> Matthew
>|||> Ask whatever you like here, just don't call anything a dumb question. That
> way, you give people who have only been using the product a bit longer
> than you a chance to answer questions and feel like they have learned
> something. But nobody wants to feel like they can only answer DUMB
> questions. ;-)
Sorry about that. I'll be more careful in the future.

> My guess is that you want to concatenate strings. In TSQL we use '+' for
> concatenation.
Yup, that's right.

> But, it still won't work because datepart returns a numeric value. Try
> using datename. Or converting to character before you concatenate.
OK, I'll give that a try.
Thank you!
Matthew

No comments:

Post a Comment