Welcome Guest, Not a member yet? Register   Sign In
Accessing a field name with space of a table in a MSSQL
#1

[eluser]rudal[/eluser]
Hi all,

I'm having a hard time to try to figure out how to access a field name with space in it.
This is in MSSQL environment.

I have a table called "Customer" and there is a field called "Primary Email"
Since Microsoft allows "space" in their field name, I'm having a hard time accessing that via the database library.

I tried to use the following command:

Code:
Select * from Customer where [Primary Email] = '[email protected]' limit 1

But got an error. Is there any other way to do this?

Thanks!
#2

[eluser]Dam1an[/eluser]
just because you can use spaces, doesn't mean you should
It's best to keep field names all lower case, and use underscore as word seperators
#3

[eluser]TheFuzzy0ne[/eluser]
I don't think that's a valid field name. Best practice is to use underscores in the place of spaces, and have your field names all lower case. Table names follow a similar principle. However, it may work if you use backticks around the field name.
#4

[eluser]rudal[/eluser]
[quote author="Dam1an" date="1244839370"]just because you can use spaces, doesn't mean you should
It's best to keep field names all lower case, and use underscore as word seperators[/quote]


Yep totally agree with you. But I am just taking over the current development from someone else. hehehe.

Any inputs? Smile Or I am just sol?
#5

[eluser]Dam1an[/eluser]
Typical, blame it on someone else Tongue
I don't use MSSQL, and have never needed to worry about this, but try every combination of quotes, backticks etc until something works lol

And then scrap it and start over, I mean refactor it lol to get rid of spaces
#6

[eluser]rudal[/eluser]
[quote author="TheFuzzy0ne" date="1244839393"]I don't think that's a valid field name. Best practice is to use underscores in the place of spaces, and have your field names all lower case. Table names follow a similar principle. However, it may work if you use backticks around the field name.[/quote]


Yeah tried that backtick alone and also tried the backtick with square bracket, both still gave me errors.

I am able to access the field name if i were to just use the mssql_query() function directly. I am assuming that CI has some sort of filter or something within the database library. Worst case, I just have to use the function directly. But using that is not a CI standard..:|
#7

[eluser]rudal[/eluser]
[quote author="Dam1an" date="1244839814"]Typical, blame it on someone else Tongue
I don't use MSSQL, and have never needed to worry about this, but try every combination of quotes, backticks etc until something works lol

And then scrap it and start over, I mean refactor it lol to get rid of spaces[/quote]

lol yeah...there are other applications that are using the database. So it'll be a hard sell to try refactor the whole thing. thanks for the inputs Big Grin
#8

[eluser]jedd[/eluser]
A programmer has a problem - they need to use a database. They think "I know, I'll use Microsoft SQL".

Now they have two problems.
#9

[eluser]Dam1an[/eluser]
[quote author="jedd" date="1244840053"]A programmer has a problem - they need to use a database. They think "I know, I'll use Microsoft SQL".

Now they have two problems.[/quote]

Then they put spaces and mixed case in the field names, now they have 3 problems....
(Is that joke of your own making, or did you steal it from somewhere?)
#10

[eluser]rudal[/eluser]
Ahh what a stupid mistake, I've made. :x

MSSQL does not have a "LIMIT" syntax in it.
and I have checked in the mssql driver, and it does not have any filter for spaces.
So my initial query statement:
Code:
Select * from Customer where [Primary Email] = '[email protected]' limit 1

was wrong. What I should have is, without the 'limit 1'.
Code:
Select * from Customer where [Primary Email] = '[email protected]'

Then all is working fine Smile
Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB